Skip to content

Commit

Permalink
migrating dart:html to package:web (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h-mzd authored Jun 3, 2024
1 parent 2d872eb commit 4fef900
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
12 changes: 10 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ packages:
path: ".."
relative: true
source: path
version: "8.2.3"
version: "8.2.5"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -204,6 +204,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "13.0.0"
web:
dependency: transitive
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
sdks:
dart: ">=3.2.0-0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=1.10.0"
31 changes: 17 additions & 14 deletions lib/fluttertoast_web.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'dart:html' as html;
import 'package:web/web.dart' as web;
import 'dart:ui_web' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
Expand Down Expand Up @@ -69,27 +69,29 @@ class FluttertoastWebPlugin {
/// [injectCssAndJSLibraries] which add the JS and CSS files into DOM
Future<void> injectCssAndJSLibraries() async {
final List<Future<void>> loading = <Future<void>>[];
final List<html.HtmlElement> tags = <html.HtmlElement>[];
final List<web.HTMLElement> tags = <web.HTMLElement>[];

final cssUrl = ui.assetManager.getAssetUrl(
'packages/fluttertoast/assets/toastify.css',
);
final html.LinkElement css = html.LinkElement()
final web.HTMLLinkElement css = web.HTMLLinkElement()
..id = 'toast-css'
..attributes = {"rel": "stylesheet"}
..setAttribute("rel", "stylesheet")
..href = cssUrl;
tags.add(css);

final jsUrl = ui.assetManager.getAssetUrl(
'packages/fluttertoast/assets/toastify.js',
);
final html.ScriptElement script = html.ScriptElement()
final web.HTMLScriptElement script = web.HTMLScriptElement()
..async = true
// ..defer = true
..src = jsUrl;
loading.add(script.onLoad.first);
tags.add(script);
html.querySelector('head')!.children.addAll(tags);
for (final web.HTMLElement tag in tags) {
web.document.querySelector('head')!.append(tag);
}

await Future.wait(loading);
}
Expand All @@ -105,7 +107,7 @@ class FluttertoastWebPlugin {
bool showClose = false,
int? textColor}) {
String m = msg.replaceAll("'", "\\'").replaceAll("\n", "<br />");
html.Element? ele = html.querySelector("#toast-content");
web.Element? ele = web.document.querySelector("#toast-content");
String content = """
var toastElement = Toastify({
text: '$m',
Expand All @@ -117,18 +119,19 @@ class FluttertoastWebPlugin {
});
toastElement.showToast();
""";
if (html.querySelector("#toast-content") != null) {
if (web.document.querySelector("#toast-content") != null) {
ele!.remove();
}
final html.ScriptElement scriptText = html.ScriptElement()
final web.HTMLScriptElement scriptText = web.HTMLScriptElement()
..id = "toast-content"
..innerHtml = content;
html.querySelector('head')!.children.add(scriptText);
..innerHTML = content;
web.document.body!.append(scriptText);
if (textColor != null) {
html.Element toast = html.querySelector('.toastify')!;
web.Element toast = web.document.querySelector('.toastify')!;
String tcRadix = textColor.toRadixString(16);
final String tC = "${tcRadix.substring(2)}${tcRadix.substring(0, 2)}";
toast.style.setProperty('color', "#$tC");
final style = toast.getAttribute('style') ?? '';
toast.setAttribute('style', '$style color: #$tC;');
}
}
}
10 changes: 9 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: "direct main"
description:
name: web
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
sdks:
dart: ">=3.2.0-0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=1.10.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
web: ^0.5.1

flutter:
plugin:
Expand Down

0 comments on commit 4fef900

Please sign in to comment.