Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for keyboard dismiss on drag #84

Open
fabiancrx opened this issue Aug 22, 2021 · 3 comments
Open

Add support for keyboard dismiss on drag #84

fabiancrx opened this issue Aug 22, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@fabiancrx
Copy link

Using the KeyboardDismissOnTap widget I wanted the keyboard to also be dismissed on drag, like Listview does so I made some changes and came up with this widget:

/// Removes the current focus and hides the keyboard when
/// the user drags or taps this widget.
///
/// Place this widget somewhere near the top of your widget
/// tree and when the user drags or taps outside of a focused widget,
/// the focus will be removed and the keyboard will be hidden.
class KeyboardDismiss extends StatelessWidget {
  final Widget child;
  final bool dismissOnTap;
  final bool dismissOnDrag;

  const KeyboardDismiss({Key? key, required this.child, this.dismissOnTap = true, this.dismissOnDrag = true})
      : super(key: key);

  void _hideKeyboard(BuildContext context) {
    final currentFocus = FocusScope.of(context);

    if (!currentFocus.hasPrimaryFocus && currentFocus.hasFocus) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollUpdateNotification>(
      child: GestureDetector(
          onTap: dismissOnTap
              ? () {
                  _hideKeyboard(context);
                }
              : null,
          child: child),
      onNotification: (ScrollUpdateNotification notification) {
        final focusScope = FocusScope.of(context);
        if (dismissOnDrag && notification.dragDetails != null && focusScope.hasFocus) {
          focusScope.unfocus();
        }
        return false;
      },
    );
  }
}

It does the same as KeyboardDismissOnTap but it also dismisses the keyboard on drag. If this may be considered of interest of the package users and maintainers I could make a PR to add this widget to the repo.

@fabiancrx fabiancrx added the enhancement New feature or request label Aug 22, 2021
@MisterJimson
Copy link
Owner

Hmm, very interesting. Thanks for the help.

It feels like this should be the default behaviour for KeyboardDismissOnTap, maybe with a boolean flag to disable the drag dismissal. Would love a PR.

@fabiancrx
Copy link
Author

fabiancrx commented Aug 23, 2021

Nice, should I maintain the Widget as KeyboardDismissOnTap(bool dismissOnDrag,Widget child) or give it the more generic KeyboardDismiss(bool dismissOnDrag,bool dismissOnTap,Widget child) name as in the gist?
My vote would be for the second option as the widget will be more generic but would not want to break versioning/compatibility for such a rather small change

@MisterJimson
Copy link
Owner

Agreed on not wanting to break. Lets keep the same name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants