Last active
October 18, 2024 20:11
-
-
Save mdebbar/5348fbf82be5eeb7d995f953437f0ce6 to your computer and use it in GitHub Desktop.
Showcases a problem with the default hit test behavior on Platform Views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() => runApp(const MyApp()); | |
| final List<Color> colors = <Color>[ | |
| Colors.blue, | |
| Colors.red, | |
| Colors.green, | |
| Colors.purple, | |
| Colors.orange, | |
| Colors.cyan, | |
| Colors.lime, | |
| ]; | |
| class MyApp extends StatefulWidget { | |
| const MyApp({super.key}); | |
| @override | |
| State<MyApp> createState() => _MyAppState(); | |
| } | |
| class _MyAppState extends State<MyApp> { | |
| int colorIndex = 0; | |
| bool includePlatformView = false; | |
| void _changeColor() { | |
| setState(() { | |
| colorIndex = (colorIndex + 1) % colors.length; | |
| }); | |
| } | |
| void _changePlatformView(bool include) { | |
| setState(() { | |
| includePlatformView = include; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: Scaffold( | |
| body: Center( | |
| child: Container( | |
| width: 300, | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| Container( | |
| child: SwitchListTile( | |
| title: const Text('Add Platform View'), | |
| value: includePlatformView, | |
| onChanged: _changePlatformView, | |
| ), | |
| ), | |
| GestureDetector( | |
| onTap: _changeColor, | |
| child: Container( | |
| width: 300, | |
| height: 300, | |
| color: colors[colorIndex], | |
| child: includePlatformView ? _platformView() : null, | |
| ), | |
| ), | |
| Text('Notice when a Platform View is added as a child, taps do not reach the container anymore.'), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| Widget? _platformView() { | |
| if (!includePlatformView) { | |
| return null; | |
| } | |
| final Widget platformView = HtmlElementView.fromTagName(tagName: 'div'); | |
| return platformView; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment