mirror of https://github.com/flutter/samples.git
[ng-flutter] migrate to pkg:web (#2173)
parent
b99602847a
commit
4c93f8356f
@ -1,56 +0,0 @@
|
|||||||
/// This is a little bit of JS-interop code so this Flutter app can dispatch
|
|
||||||
/// a custom JS event (to be deprecated by package:web)
|
|
||||||
library;
|
|
||||||
|
|
||||||
import 'dart:js_interop';
|
|
||||||
|
|
||||||
@JS('CustomEvent')
|
|
||||||
@staticInterop
|
|
||||||
class DomCustomEvent {
|
|
||||||
external factory DomCustomEvent.withType(JSString type);
|
|
||||||
external factory DomCustomEvent.withOptions(JSString type, JSAny options);
|
|
||||||
factory DomCustomEvent._(String type, [Object? options]) {
|
|
||||||
if (options != null) {
|
|
||||||
return DomCustomEvent.withOptions(type.toJS, options.jsify()!);
|
|
||||||
}
|
|
||||||
return DomCustomEvent.withType(type.toJS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatchCustomEvent(DomElement target, String type, Object data) {
|
|
||||||
final DomCustomEvent event = DomCustomEvent._(type, <String, Object>{
|
|
||||||
'bubbles': true,
|
|
||||||
'composed': true,
|
|
||||||
'detail': data,
|
|
||||||
});
|
|
||||||
|
|
||||||
target.dispatchEvent(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@JS()
|
|
||||||
@staticInterop
|
|
||||||
class DomEventTarget {}
|
|
||||||
|
|
||||||
extension DomEventTargetExtension on DomEventTarget {
|
|
||||||
@JS('dispatchEvent')
|
|
||||||
external JSBoolean _dispatchEvent(DomCustomEvent event);
|
|
||||||
bool dispatchEvent(DomCustomEvent event) => _dispatchEvent(event).toDart;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JS()
|
|
||||||
@staticInterop
|
|
||||||
class DomElement extends DomEventTarget {}
|
|
||||||
|
|
||||||
extension DomElementExtension on DomElement {
|
|
||||||
@JS('querySelector')
|
|
||||||
external DomElement? _querySelector(JSString selectors);
|
|
||||||
DomElement? querySelector(String selectors) => _querySelector(selectors.toJS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@JS()
|
|
||||||
@staticInterop
|
|
||||||
class DomDocument extends DomElement {}
|
|
||||||
|
|
||||||
@JS()
|
|
||||||
@staticInterop
|
|
||||||
external DomDocument get document;
|
|
@ -1,10 +1,15 @@
|
|||||||
import 'dom.dart' as dom;
|
import 'dart:js_interop';
|
||||||
|
import 'package:web/web.dart';
|
||||||
|
|
||||||
/// Locates the root of the flutter app (for now, the first element that has
|
/// Locates the root of the flutter app (for now, the first element that has
|
||||||
/// a flt-renderer tag), and dispatches a JS event named [name] with [data].
|
/// a flt-renderer tag), and dispatches a JS event named [name] with [data].
|
||||||
void broadcastAppEvent(String name, Object data) {
|
void broadcastAppEvent(String name, JSObject data) {
|
||||||
final dom.DomElement? root = dom.document.querySelector('[flt-renderer]');
|
final HTMLElement? root = document.querySelector('[flt-renderer]') as HTMLElement?;
|
||||||
assert(root != null, 'Flutter root element cannot be found!');
|
assert(root != null, 'Flutter root element cannot be found!');
|
||||||
|
|
||||||
dom.dispatchCustomEvent(root!, name, data);
|
final eventDetails = CustomEventInit(detail: data);
|
||||||
|
eventDetails.bubbles = true;
|
||||||
|
eventDetails.composed = true;
|
||||||
|
|
||||||
|
root!.dispatchEvent(CustomEvent(name, eventDetails));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue