Skip to content

Dismissing keyboard programatically #7247

Closed

Description

@mikemimik

It's not clear how to accomplish this.

Activity

  1. eseidelGoogle commented on Dec 13, 2016

    @eseidelGoogle
    Contributor

    @Hixie or @mpcomplete presumably this is a docs bug?

  2. Hixie commented on Dec 13, 2016

    @Hixie
    Contributor

    Assuming this means how to do it from code, I guess so.

  3. mikemimik commented on Dec 13, 2016

    @mikemimik
    ContributorAuthor

    When you select a InputFormField the keyboard automatically gets requested and is shown. There is actually no way to make it dismiss from the UI and it's unclear or not possible to do it from current widgets.

  4. Hixie commented on Dec 13, 2016

    @Hixie
    Contributor

    From the UI it's easy, just press the Android OS-level down arrow button (replaces the back button when the keyboard is up).

  5. mikemimik commented on Dec 13, 2016

    @mikemimik
    ContributorAuthor

    I'm working in iOS and there is no such OS-level trigger. I tested in the simulator and on device, I get the same standard keyboard.

  6. chinmaygarde commented on Dec 13, 2016

    @chinmaygarde
    Contributor

    TextInputConnection.close should do what you need.

  7. mikemimik commented on Dec 15, 2016

    @mikemimik
    ContributorAuthor

    @chinmaygarde The .close() method is not static on that class, which means I need to access it on an instance member. However the constructor is private, so I can't create an instance member.

  8. mikemimik commented on Dec 15, 2016

    @mikemimik
    ContributorAuthor

    I figured out how to do it. In discussion with @eseidelGoogle, posed the question if unfocusing might do the trick.

    Inside a section of code which has access to the context you just need to have Focus.clear(context); and it will dismiss the keyboard.

  9. mikemimik commented on Dec 15, 2016

    @mikemimik
    ContributorAuthor

    I'm come across an error with the about implementation. As you'll see from the below images, I'm using code to dismiss the keyboard by calling Focus.clear(context);.

    I'm not sure how unfocusing dismisses the keyboard, as in, precisely what flutter is doing under the hood.

    The effect of being 'focused' is still in effect on the InputFormField widget.

    The code to achieve these images is in this repo.

    Unselected input:

    simulator screen shot dec 15 2016 3 21 07 am

    Selected input:

    simulator screen shot dec 15 2016 3 21 19 am

    Dismissed keyboard:

    simulator screen shot dec 15 2016 3 22 12 am

  10. sethladd commented on Dec 17, 2016

    @sethladd
    Contributor

    @mikemimik did you ever get this to work? What was your recipe?

  11. added this to the milestone on Feb 27, 2017
  12. mikemimik commented on Apr 11, 2017

    @mikemimik
    ContributorAuthor

    @sethladd Will cycle back to this on the weekend. Apologies for being so terribly behind on responding.

  13. pedia commented on Nov 3, 2017

    @pedia

    I found HACK way:

    import 'package:flutter/services.dart';

    and call

    SystemChannels.textInput.invokeMethod('TextInput.hide');

  14. jonasbark commented on Nov 30, 2017

    @jonasbark

    This is a better way, as it also clears the focus:
    FocusScope.of(context).requestFocus(new FocusNode());
    from
    https://stackoverflow.com/a/44991969/1068346

  15. 69 remaining items

  16. jamesdixon commented on Aug 4, 2019

    @jamesdixon

    @jifalops have you tried without currentFocus != WidgetsBinding.instance.focusManager.rootScope?

    I just wrote an article on this topic that contains the full code. I'm curious if there's something different about your approach as this works for me.

  17. jifalops commented on Aug 4, 2019

    @jifalops

    Yes I tried it without checking the rootScope, but from what I gather, calling unfocus() on the root scope causes an exception. My use case involves hiding the keyboard when the text field gains focus, which is a bit more stringent than most use cases.

  18. jifalops commented on Aug 4, 2019

    @jifalops

    @jamesdixon That is a clever way to unfocus any input in the app when tapping outside of them.

  19. jamesdixon commented on Aug 4, 2019

    @jamesdixon

    @jifalops thanks! out of curiosity, what's your use case for hiding the keyboard when the form field gains focus?

  20. jifalops commented on Aug 4, 2019

    @jifalops

    To show the date/time picker dialogs instead of initially going into edit mode. https://pub.dev/packages/datetime_picker_formfield

  21. jamesdixon commented on Aug 4, 2019

    @jamesdixon
  22. JamesMcIntosh commented on Aug 28, 2019

    @JamesMcIntosh

    @jifalops @jamesdixon If unfocus() is not working then pay VERY close attention to which BuildContext you have passed in to the FocusScope.of(context).
    In particular I made the mistake of using the BuildContext passed into a build method rather than the context supplied from a Builder function, my particular case the function was building up a MaterialPageRoute so the context was well out of expected scope by the time the builder was called i.e.

    MaterialPageRoute<void> buildRoute(BuildContext context, Widget child) {
      return MaterialPageRoute<void>(
        builder: (BuildContext builderContext) => GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context); // SHOULD BE builderContext
            if (!currentFocus.hasPrimaryFocus) {
              currentFocus.unfocus();
            }
          },
          child: child,
        ),
      );
    }
    
  23. awkaw commented on Nov 22, 2019

    @awkaw

    import 'package:flutter/services.dart';

    SystemChannels.textInput.invokeMethod('TextInput.hide');

  24. BahijMik commented on Dec 14, 2019

    @BahijMik

    Is there any way to actually manipulate the keyboard hide button on android? just like in android there is onConfigurationChanged, i want to decrease/increase image size when the keyboard is shown/hidden.

  25. malviyaritesh commented on Dec 27, 2019

    @malviyaritesh

    As mentioned in #7247 (comment)
    Now, simply you can unfocus also:

    FocusScope.of(context).unfocus();
  26. m-rahme commented on Jan 29, 2020

    @m-rahme

    If i have a TextField implemented in a Stack with GoogleMaps widget,
    FocusScope.of(context).unfocus(); will not work. Any Solution ?

  27. TKH219 commented on Feb 21, 2020

    @TKH219

    If i have a TextField implemented in a Stack with GoogleMaps widget,
    FocusScope.of(context).unfocus(); will not work. Any Solution ?

    WidgetsBinding.instance.focusManager.primaryFocus?.unfocus();
    You should try it.

  28. Ladriel commented on Mar 2, 2020

    @Ladriel

    There is a few points to mention and put into light here :


    Nobody ever mentioned that the solution proned by everyone to reset the focus basically conflicts with text editing options (the 'copy/paste/select all' menus).

    For exemple :
    Hold a tap on a text to make the contextual text editing options appear on your device and try to use the 'select all' option, your focus is now reseted because of the fix and the text is unselected.

    For this particular concerns the preferred method for me has been :
    SystemChannels.textInput.invokeMethod('TextInput.hide');


    We dropped it since first iterations but i already been experiencing with reactions to long press by wrapping the whole app with a Listener with onPointerDown and onPointerUp event listeners. You can evaluate the timing between down and up events to recognize and differentiate long press/scrollings.

    A way to know which particular Widget is the target of focus on tap would come in really handy too.
    Something like :
    'FocusScope.of(context).componentType' which would answer 'TextFormField'.
    I couldn't find a way to get this informations (from my experimentations at least, correct me if i'm wrong).


    Also the missing Done button on iOs numeric keyboards in Flutter is kind of a problem, your one and only option to dismiss the keyboard is then to use the solutions detailed in this thread. Which does seem kind of hacky-wacky.


    Thought i'd leave these points here if anybody would ever need them.

  29. gspencergoog commented on Mar 11, 2020

    @gspencergoog
    Contributor

    @Ladriel, you can indeed get the runtime type of the widget that has the current focus:

    Type focusedType = Focus.of(context).context.widget.runtimeType;

    But this is a question that would be better asked on Stack Overflow.

    As for your other comments, please file separate bugs for separate issues.

    I'm going to close this bug for comments, since the issue database isn't really the best place for conversation or questions: the bug has been closed for a long time.

  30. locked as resolved and limited conversation to collaborators on Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions