File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed
lib/presentation/widgets/utilities Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import 'package:flutter/gestures.dart' ;
2
+ import 'package:flutter/widgets.dart' ;
3
+
4
+ /// A widget for dismissing the keyboard on tap outside.
5
+ ///
6
+ /// Wrap a widget that contain text inputs with this widget
7
+ /// to enable tap-outside-to-dismiss-keyboard behaviour.
8
+ ///
9
+ /// So instead of this:
10
+ ///
11
+ /// ```dart
12
+ /// GestureDetector(
13
+ /// onTap: () {
14
+ /// final focusScope = FocusScope.of(context);
15
+ /// if (focusScope.hasFocus) {
16
+ /// focusScope.unfocus();
17
+ /// }
18
+ /// },
19
+ /// child: MyPage(),
20
+ /// ),
21
+ /// ```
22
+ ///
23
+ /// You could do this:
24
+ ///
25
+ /// ```dart
26
+ /// BackgroundFocusScopeDismisser(
27
+ /// child: MyPage(),
28
+ /// ),
29
+ /// ```
30
+ class FocusScopeDismissible extends StatelessWidget {
31
+ const FocusScopeDismissible ({
32
+ Key ? key,
33
+ this .excludeFromSemantics = false ,
34
+ this .dragStartBehavior = DragStartBehavior .start,
35
+ required this .child,
36
+ }) : super (key: key);
37
+
38
+ final DragStartBehavior dragStartBehavior;
39
+ final bool excludeFromSemantics;
40
+ final Widget child;
41
+
42
+ @override
43
+ Widget build (BuildContext context) {
44
+ return GestureDetector (
45
+ dragStartBehavior: dragStartBehavior,
46
+ excludeFromSemantics: excludeFromSemantics,
47
+ onTap: () {
48
+ final focusScope = FocusScope .of (context);
49
+
50
+ if (focusScope.hasFocus) {
51
+ focusScope.unfocus ();
52
+ }
53
+ },
54
+ child: child,
55
+ );
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments