Skip to content

Commit b37594b

Browse files
authored
feat: Introduce FocusScopeDismissible (#52)
* Introduce BackgroundFocusScopeDismisser * fix: Rename to FocusScopeDismissible * fix: Rename the folder to utilities * feat: Add dragStartBehaviour, excludeFromSemantics
1 parent df546f2 commit b37594b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

0 commit comments

Comments
 (0)