Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating dart:html to package:web #507

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
Comment on lines +92 to +94

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

querySelector has children, why for loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on how querySelector having children can help us get rid of the for loop? Maybe a code snippet?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, you are right


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