Skip to content
Merged
Changes from all commits
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
57 changes: 57 additions & 0 deletions lib/presentation/widgets/utilities/focus_scope_dismissible.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

/// A widget for dismissing the keyboard on tap outside.
///
/// Wrap a widget that contain text inputs with this widget
/// to enable tap-outside-to-dismiss-keyboard behaviour.
///
/// So instead of this:
///
/// ```dart
/// GestureDetector(
/// onTap: () {
/// final focusScope = FocusScope.of(context);
/// if (focusScope.hasFocus) {
/// focusScope.unfocus();
/// }
/// },
/// child: MyPage(),
/// ),
/// ```
///
/// You could do this:
///
/// ```dart
/// BackgroundFocusScopeDismisser(
/// child: MyPage(),
/// ),
/// ```
class FocusScopeDismissible extends StatelessWidget {
const FocusScopeDismissible({
Key? key,
this.excludeFromSemantics = false,
this.dragStartBehavior = DragStartBehavior.start,
required this.child,
}) : super(key: key);

final DragStartBehavior dragStartBehavior;
final bool excludeFromSemantics;
final Widget child;

@override
Widget build(BuildContext context) {
return GestureDetector(
dragStartBehavior: dragStartBehavior,
excludeFromSemantics: excludeFromSemantics,
onTap: () {
final focusScope = FocusScope.of(context);

if (focusScope.hasFocus) {
focusScope.unfocus();
}
},
child: child,
);
}
}