Created
January 7, 2022 16:05
-
-
Save guitoof/0cc362984e8d60b51064bca6672c0bb5 to your computer and use it in GitHub Desktop.
Allow Multiple Gestures without modifying child widgets
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/gestures.dart'; | |
import 'package:flutter/widgets.dart'; | |
// Custom Gesture Recognizer. | |
// rejectGesture() is overridden. When a gesture is rejected, this is the function that is called. By default, it disposes of the | |
// Recognizer and runs clean up. However we modified it so that instead the Recognizer is disposed of, it is actually manually added. | |
// The result is instead you have one Recognizer winning the Arena, you have two. It is a win-win. | |
class AllowMultipleTapGesturesRecognizer extends TapGestureRecognizer { | |
@override | |
void rejectGesture(int pointer) { | |
acceptGesture(pointer); | |
} | |
} | |
class GlobalGesturesListener extends StatelessWidget { | |
final Widget child; | |
GlobalGesturesListener({required this.child}); | |
@override | |
Widget build(BuildContext context) { | |
return RawGestureDetector( | |
gestures: { | |
AllowMultipleTapGesturesRecognizer: | |
GestureRecognizerFactoryWithHandlers< | |
AllowMultipleTapGesturesRecognizer>( | |
() => AllowMultipleTapGesturesRecognizer(), | |
(AllowMultipleTapGesturesRecognizer instance) { | |
instance.onTap = | |
() => print('🔴 Global Gesture Listener Plugin DETECTED TAP'); | |
}, | |
), | |
}, | |
child: child, | |
); | |
} | |
} |
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'; | |
import 'package:flutter_sandbox/global_gestures_listener_plugin/global_gestures_listener.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
final String title = 'Flutter Sandbox App'; | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int _counter = 0; | |
void _doStuff() { | |
print('🔵 Any child button DETECTED TAP'); | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return GlobalGesturesListener( | |
child: MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text("Here is the result of the stuff you've done:"), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _doStuff, | |
tooltip: 'Do stuff...', | |
child: Icon(Icons.science), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment