Visitar URL original
[web] Add PointerInterceptor widget by ditman · Pull Request #256 · flutter/packages · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dartfmt -w .
  • Loading branch information
ditman committed Dec 15, 2020
commit 895f6bd54f06be45545500d654eebd39c3d93e9d
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,38 @@ void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('Widget', () {
final Finder nonClickableButtonFinder = find.byKey(const Key('transparent-button'));
final Finder clickableButtonFinder = find.byKey(const Key('clickable-button'));

testWidgets('on wrapped elements, the browser hits the interceptor (and not the background-html-view)', (WidgetTester tester) async {
final Finder nonClickableButtonFinder =
find.byKey(const Key('transparent-button'));
final Finder clickableButtonFinder =
find.byKey(const Key('clickable-button'));

testWidgets(
'on wrapped elements, the browser hits the interceptor (and not the background-html-view)',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();

final html.Element element = _getHtmlElementFromFinder(clickableButtonFinder, tester);
final html.Element element =
_getHtmlElementFromFinder(clickableButtonFinder, tester);
expect(element.tagName.toLowerCase(), 'flt-platform-view');

final html.Element platformViewRoot = element.shadowRoot.getElementById('background-html-view');
final html.Element platformViewRoot =
element.shadowRoot.getElementById('background-html-view');
expect(platformViewRoot, isNull);
});

testWidgets('on unwrapped elements, the browser hits the background-html-view', (WidgetTester tester) async {
testWidgets(
'on unwrapped elements, the browser hits the background-html-view',
(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();

final html.Element element = _getHtmlElementFromFinder(nonClickableButtonFinder, tester);
final html.Element element =
_getHtmlElementFromFinder(nonClickableButtonFinder, tester);
expect(element.tagName.toLowerCase(), 'flt-platform-view');

final html.Element platformViewRoot = element.shadowRoot.getElementById('background-html-view');

final html.Element platformViewRoot =
element.shadowRoot.getElementById('background-html-view');
expect(platformViewRoot, isNotNull);
});
});
Expand Down
21 changes: 16 additions & 5 deletions packages/web_pointer_interceptor/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ class _MyHomePageState extends State<MyHomePage> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Last click on: $_lastClick', key: const Key('last-clicked'),),
Text(
'Last click on: $_lastClick',
key: const Key('last-clicked'),
),
Container(
color: Colors.black,
width: _videoWidth,
Expand All @@ -97,21 +100,27 @@ class _MyHomePageState extends State<MyHomePage> {
alignment: Alignment.center,
children: <Widget>[
HtmlElement(
onClick: () { _clickedOn('html-element'); },
onClick: () {
_clickedOn('html-element');
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
key: const Key('transparent-button'),
child: const Text('Never calls onPressed'),
onPressed: () { _clickedOn('transparent-button'); },
onPressed: () {
_clickedOn('transparent-button');
},
),
PointerInterceptor(
child: RaisedButton(
key: const Key('clickable-button'),
child: const Text('Works As Expected'),
onPressed: () { _clickedOn('clickable-button'); },
onPressed: () {
_clickedOn('clickable-button');
},
),
),
],
Expand All @@ -136,7 +145,9 @@ class HtmlElement extends StatelessWidget {

@override
Widget build(BuildContext context) {
htmlElement.onClick.listen((_) { onClick(); });
htmlElement.onClick.listen((_) {
onClick();
});

return const HtmlElementView(
viewType: _htmlElementViewType,
Expand Down
6 changes: 5 additions & 1 deletion packages/web_pointer_interceptor/lib/src/mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import 'package:flutter/widgets.dart';
/// A Widget that prevents clicks from being swallowed by HtmlViewElements.
class PointerInterceptor extends StatelessWidget {
/// Create a `PointerInterceptor` around a `child`.
const PointerInterceptor({@required this.child, this.debug = false, Key key}) : super(key: key);
const PointerInterceptor({
@required this.child,
this.debug = false,
Key key,
}) : super(key: key);

/// The Widget that is being wrapped by this PointerInterceptor.
/// It needs to be properly sized (like a Button).
Expand Down
13 changes: 9 additions & 4 deletions packages/web_pointer_interceptor/lib/src/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ String _getViewType({bool debug = false}) {
void _registerWrapper({bool debug = false}) {
final String viewType = _getViewType(debug: debug);

ui.platformViewRegistry.registerViewFactory(viewType,
(int viewId) {
ui.platformViewRegistry.registerViewFactory(viewType, (int viewId) {
final html.Element wrapper = html.DivElement();
Comment thread
ditman marked this conversation as resolved.
Resolved
Comment thread
ditman marked this conversation as resolved.
Resolved
if (debug) {
wrapper.style.backgroundColor = 'rgba(255, 0, 0, .5)';
Expand All @@ -34,7 +33,11 @@ void _registerWrapper({bool debug = false}) {
class PointerInterceptor extends StatelessWidget {
/// Creates a PointerInterceptor for the web.
/// If the underlying viewFactories are not registered yet, it registers them.
Comment thread
ditman marked this conversation as resolved.
Resolved
PointerInterceptor({@required this.child, this.debug = false, Key key}) : super(key: key) {
PointerInterceptor({
@required this.child,
this.debug = false,
Key key,
}) : super(key: key) {
if (!_registered) {
_register();
}
Expand Down Expand Up @@ -68,7 +71,9 @@ class PointerInterceptor extends StatelessWidget {
return Stack(
children: <Widget>[
Positioned.fill(
child: HtmlElementView(viewType: viewType,),
child: HtmlElementView(
viewType: viewType,
),
),
child,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

library web_pointer_interceptor;

export 'src/mobile.dart'
if (dart.library.html) 'src/web.dart';
export 'src/mobile.dart' if (dart.library.html) 'src/web.dart';