499

I am collecting user input with a TextFormField and when the user presses a FloatingActionButton indicating they are done, I want to dismiss the on screen keyboard.

How do I make the keyboard go away automatically?

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.send),
        onPressed: () {
          setState(() {
            // send message
            // dismiss on screen keyboard here
            _controller.clear();
          });
        },
      ),
      body: new Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

void main() {
  runApp(new MyApp());
}
3

34 Answers 34

912

For Flutter version 2 or latest :

Since Flutter 2 with null safety this is the best way:

FocusManager.instance.primaryFocus?.unfocus();

Note: using old ways leads to some problems like keep rebuild states;


For Flutter version < 2 :

As of Flutter v1.7.8+hotfix.2, the way to go is:

FocusScope.of(context).unfocus();

Comment on PR about that:

Now that #31909 (be75fb3) has landed, you should use FocusScope.of(context).unfocus() instead of FocusScope.of(context).requestFocus(FocusNode()), since FocusNodes are ChangeNotifiers, and should be disposed properly.

-> DO NOT use ̶r̶e̶q̶u̶e̶s̶t̶F̶o̶c̶u̶s̶(̶F̶o̶c̶u̶s̶N̶o̶d̶e̶(̶)̶ anymore.

 F̶o̶c̶u̶s̶S̶c̶o̶p̶e̶.̶o̶f̶(̶c̶o̶n̶t̶e̶x̶t̶)̶.̶r̶e̶q̶u̶e̶s̶t̶F̶o̶c̶u̶s̶(̶F̶o̶c̶u̶s̶N̶o̶d̶e̶(̶)̶)̶;̶

Read more about the FocusScope class in the flutter docs.

5
  • The problem I have been experiencing is when leaving a text field to open up the camera, the keyboard is not always dismissed and overlaps the camera. This solution does in fact remove the focus, BUT then the focus returns to the text field and the problem persists. Moving the focus to an unused FocusNode solves this.
    – dfmiller
    Jan 16, 2020 at 23:28
  • If this solution didn't fix your error, you can add "await Future.delayed(const Duration(milliseconds: 200))" before send to another page. Feb 1, 2022 at 19:52
  • my issue was i put 'focusnode: FocusNode()' in ui. i removed it and put this code. Worked! Sep 29, 2022 at 8:46
  • Thanks for your great solution. But I have a question about it. If I use FocusManager.instance.primaryFocus?.unfocus(); solution, should I dispose of it in dispose method?
    – Aaydin
    Feb 1, 2023 at 7:36
  • @Aaydin Not sure exactly what your use case is. I suggest you to just experiment / test and see the result ;-)
    – Pascal
    Feb 1, 2023 at 9:33
341

Note: This answer is outdated. See the answer for newer versions of Flutter.

You can dismiss the keyboard by taking away the focus of the TextFormField and giving it to an unused FocusNode:

FocusScope.of(context).requestFocus(FocusNode());
7
  • Where would you implement this code? In the TextFormField; maybe after onChanged: or in the action of your custom button?
    – Charles Jr
    Dec 31, 2017 at 0:23
  • @CharlesJr I do it in the action of my button. Jun 23, 2018 at 7:05
  • you can use my package if you want :) pub.dartlang.org/packages/keyboard_actions Dec 9, 2018 at 6:55
  • 31
    This is before Flutter v1.7.8 - see answer stackoverflow.com/a/56946311/8696915 which gives FocusScope.of(context).unfocus() Aug 9, 2019 at 0:31
  • 4
    Don't do this if you have access to .unfocus() since it'll cause a memory leak. That new FocusNode() will never get cleaned up. See comment about .unfocus() Feb 20, 2020 at 2:00
164

Solution with FocusScope doesn't work for me. I found another:

import 'package:flutter/services.dart';

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

It solved my problem.

9
  • 1
    Calling this doesn't hide the keyboard for me. Should it work on both Android and iOS?
    – Jardo
    Dec 28, 2018 at 4:24
  • Worked for me on iOS (12.1, iPhone 5s) and Android (Pixel 3) Jan 25, 2019 at 7:44
  • Where do you put this? I tried just adding it as the first bit of code in the app and it didnt do anything.
    – Justin808
    Jan 30, 2019 at 23:48
  • 1
    What do you mean - "the first bit of code"? Insert it in build method for example. Jan 31, 2019 at 8:33
  • 1
    is there an equivalent for this for the numeric keybaord?
    – onesiumus
    Feb 16, 2020 at 9:09
106

For Flutter 1.17.3 (stable channel as of June 2020), use

FocusManager.instance.primaryFocus.unfocus();
3
  • 2
    Tested and worked for Flutter: 1.24.0. and iOS 14.0 and Android Version 11 simulators.
    – g212gs
    Oct 21, 2020 at 19:52
  • 4
    Works well. With null-safety, use optional chaining on primaryFocus as follows: FocusManager.instance.primaryFocus?.unfocus(); Mar 2, 2021 at 21:02
  • 2
    this work for me for flutter version: 2.0.2
    – Muklas
    Mar 30, 2021 at 9:12
40

Following code helped me to hide keyboard

   void initState() {
   SystemChannels.textInput.invokeMethod('TextInput.hide');
   super.initState();
   }
3
  • 1
    For me, this worked better than the solution to add FocusScope.of(context).unfocus() as that solution put me into a loop of the keyboard being shown and hidden
    – Varun
    Aug 19, 2020 at 1:35
  • This works the best for me. I had a keyboard open from another screen and I think focus was in a weird state so only this method worked. Thank YOU!!!! Feb 28, 2021 at 14:45
  • This is much more elegant that the previous solutions. And most importantly, this can be wrapped in override dispose method to guarantee whenever the widget is disposed (e.g. very useful in dialog) , the on screen keyboard input will be closed appropriately.
    – chungonion
    May 3, 2021 at 6:39
32

To dismiss the keyboard (1.7.8+hotfix.2 and above) just call the method below:

FocusScope.of(context).unfocus();

Once the FocusScope.of(context).unfocus() method already check if there is focus before dismiss the keyboard it's not needed to check it. But in case you need it just call another context method: FocusScope.of(context).hasPrimaryFocus

28

Looks like different approaches for different version. I am using Flutter v1.17.1 and the below works for me.

onTap: () {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
       currentFocus.focusedChild.unfocus();
    }
}
24

As in Flutter everything is a widget, I decided to wrap the FocusScope.of(context).unfocus(); approach in a short utility widget.

Just create the KeyboardHider widget:

import 'package:flutter/widgets.dart';

/// A widget that upon tap attempts to hide the keyboard.
class KeyboardHider extends StatelessWidget {
  /// Creates a widget that on tap, hides the keyboard.
  const KeyboardHider({
    required this.child,
    Key? key,
  }) : super(key: key);

  /// The widget below this widget in the tree.
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => FocusScope.of(context).unfocus(),
      child: child,
    );
  }
}

Now, you can wrap any widget (very convenient when using a good IDE) with the KeyboardHider widget, and then when you tap on something, the keyboard will close automatically. It works well with forms and other tappable areas.

class SimpleWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return KeyboardHider(
      /* Here comes a widget tree that eventually opens the keyboard,
       * but the widget that opened the keyboard doesn't necessarily
       * takes care of hiding it, so we wrap everything in a
       * KeyboardHider widget */
      child: Container(),
    );
  }
}
23
GestureDetector(
          onTap: () {
            FocusScope.of(context).unfocus();
          },
          child:Container(
    alignment: FractionalOffset.center,
    padding: new EdgeInsets.all(20.0),
    child: new TextFormField(
      controller: _controller,
      decoration: new InputDecoration(labelText: 'Example Text'),
    ),
  ), })

try this on tap gesture

0
22

None of the above solutions don't work for me.

Flutter suggests this - Put your widget inside new GestureDetector() on which tap will hide keyboard and onTap use FocusScope.of(context).requestFocus(new FocusNode())

class Home extends StatelessWidget {
@override
  Widget build(BuildContext context) {
    var widget = new MaterialApp(
        home: new Scaffold(
            body: new Container(
                height:500.0,
                child: new GestureDetector(
                    onTap: () {
                        FocusScope.of(context).requestFocus(new FocusNode());
                    },
                    child: new Container(
                        color: Colors.white,
                        child:  new Column(
                            mainAxisAlignment:  MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,

                            children: [
                                new TextField( ),
                                new Text("Test"),                                
                            ],
                        )
                    )
                )
            )
        ),
    );

    return widget;
}}
21

For me, the Listener above App widget is the best approach I've found:

Listener(
  onPointerUp: (_) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      currentFocus.focusedChild.unfocus();
    }
  },
  child: MaterialApp(
    title: 'Flutter Test App',
    theme: theme,
    ...
  ),
)
3
  • This worked for me in iOS, I don't know it is right way to do it.
    – Niraj
    Jan 11, 2021 at 13:23
  • 1
    It worked in iOS, But we loose the paste functionality ! As soon as we tap & hold in textbox, paste option appears but hides when we try to tap on it.
    – Tushar Pol
    Aug 20, 2022 at 17:55
  • Indeed this makes the paste functionality no longer work. Use the approach with GestureDetector to fix this
    – edwardmp
    Jan 16, 2023 at 22:18
19

This may simplify the case. Below code will work only if keyboard is open

if(FocusScope.of(context).isFirstFocus) {
 FocusScope.of(context).requestFocus(new FocusNode());
}
0
14

The easiest way to do this is with the onTapOutside method that TextField provides:

onTapOutside: (PointerDownEvent event) {
   FocusManager.instance.primaryFocus?.unfocus();
},
1
  • Nice one, working fine.
    – Manoranjan
    Mar 26 at 8:56
11

You can use unfocus() method from FocusNode class.

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = new TextEditingController();
  FocusNode _focusNode = new FocusNode(); //1 - declare and initialize variable

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.send),
        onPressed: () {
            _focusNode.unfocus(); //3 - call this method here
        },
      ),
      body: new Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          focusNode: _focusNode, //2 - assign it to your TextFormField
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

void main() {
  runApp(new MyApp());
}
3
  • 1
    Hi! It would be best if you could edit your answer to add just a little more context to your answer.
    – grooveplex
    Jan 31, 2019 at 20:03
  • Yes, it removes the focus if tapping on an specific widget. Can't add it to every widget on my app.
    – Dpedrinha
    Sep 13, 2019 at 1:58
  • Tapping on a widget is just one way to call unfocus method. If you don't want to spam this on your app you can use a different way, like the TextFormField change event or a reactive pattern, it depends on your app's architecture. Sep 13, 2019 at 18:28
10

To dismiss keyboard when TextField or some widget loses focus you can do this:

TextField(
      onTapOutside: (event) {
        FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
          FocusManager.instance.primaryFocus?.unfocus();
        }
      },
);
8

To summarize, this is a working solution for Flutter 1.17:

Wrap your Widget like this:

GestureDetector(
        onTap: FocusScope.of(context).unfocus,
        child: YourWidget(),
);
1
  • Doesn't work for me May 22, 2022 at 11:45
7

if you use CustomScrollView, just put,

keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
7

In flutter 3.7.2 worked, wrap you Scaffold inside GestureDetector to dismiss keyboard when tap somewhere outside.

GestureDetector(
   onTap: () {
      SystemChannels.textInput.invokeMethod<void>('TextInput.hide');
   },
   child: Scaffold(),
),
6

You can wrap your widget with "GestureDetector", then assign "FocusScope.of(context).unfocus()" to its onTap function

GestureDetector(
 onTap: () => FocusScope.of(context).unfocus(),
 child: child,
);
2
  • Doesn't work for me May 22, 2022 at 11:45
  • You should debug and see whether onTap function is triggered or not. Having other tap listeners fore/back ground of GestureDetector, may be the case for you. May 23, 2022 at 12:31
6

Call this function when you needed

  void hideKeyboard(BuildContext context) {
      FocusScopeNode currentFocus = FocusScope.of(context);
      if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
        FocusManager.instance.primaryFocus?.unfocus();
      }
    }
4

====== Dismiss the keyboard after clicking out of the TextField =======

 @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),  //this will dismiss keyboard
      child: Scaffold(
        body: SafeArea(
               .........

====== Dismiss the keyboard when scrolling the screen =======

ListView(
            keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag, //this will dismiss
            children: [
               ..........

The SingleChildScrollView widget also have this property.

4

add this code inside build widget

    FocusScope.of(context).requestFocus(FocusNode());
1
  • this solution is not recommended as it will cause a mess if you want to give textfield focus again. below solution is prefect with if statement. closeKeyboard(BuildContext context) { var currentFocus = FocusScope.of(context); if (!currentFocus.hasPrimaryFocus) { currentFocus.unfocus(); } } Mar 3, 2023 at 13:38
3
_dismissKeyboard(BuildContext context) {
   FocusScope.of(context).requestFocus(new FocusNode());
}

@override
Widget build(BuildContext context) {

return new GestureDetector(
    onTap: () {
    this._dismissKeyboard(context);
    },
    child: new Container(
    color: Colors.white,
    child: new Column(
        children: <Widget>[/*...*/],
    ),
    ),
 );
}
3

If your keyboard still won't turn off , don't forget add focusNode to TextField. The above information was helpful, but forgetting to add focusNode bothered me a bit. Here an example.

TextField(
          focusNode: FocusNode(),
          textController: _controller,
          autoFocus: false,
          textStyle: TextStyle(fontSize: 14),
          onFieldSubmitted: (text) {},
          onChanged: (text) {},
          hint: 'Enter the code',
          hintColor: CustomColors.mediumGray,
          suffixAsset: _voucherController.text.length == 7
              ? Assets.ic_approved_voucher
              : null,
          isIcon: false,
          isObscure: false,
          maxLength: 7,
        )



closeKeyboard(BuildContext context) {
    var currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    _keyboardVisible = MediaQuery.of(context).viewInsets.bottom != 0;
    size = MediaQuery.of(context).size;
    return GestureDetector(
      onTap: () {
        closeKeyboard(context);
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Container(
            width: double.maxFinite,
            height: double.maxFinite,
            child: _buildUI(vm)),
      ),
    );
  }
3

You can use this:

FocusScope.of(context).requestFocus(FocusNode());

And you can use this onTap of GestureDetector or InkWell like this:

GestureDetector(
  onTap: () {
    // THIS FOCUS SCOPE WILL CLOSE THE KEYBOARD
    FocusScope.of(context).requestFocus(FocusNode());
    forgotPasswordAPI(emailController.text);
  },
}
 
2

You can also declare a focusNode for you textfield and when you are done you can just call the unfocus method on that focusNode and also dispose it

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = new TextEditingController();

/// declare focus
  final FocusNode _titleFocus = FocusNode();

  @override
  void dispose() {
    _titleFocus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.send),
        onPressed: () {
          setState(() {
            // send message
            // dismiss on screen keyboard here

            _titleFocus.unfocus();
            _controller.clear();
          });
        },
      ),
      body: new Container(
        alignment: FractionalOffset.center,
        padding: new EdgeInsets.all(20.0),
        child: new TextFormField(
          controller: _controller,
          focusNode: _titleFocus,
          decoration: new InputDecoration(labelText: 'Example Text'),
        ),
      ),
    );
  }
}
2

I have created this function to my base code, so far works well!!

void hideKeyword(BuildContext context) {
  FocusScopeNode currentFocus = FocusScope.of(context);
  if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
    currentFocus.focusedChild.unfocus();
  }
}
2

Use SystemChannels.textInput.invokeMethod('TextInput.hide');. It will close/dismiss the keyboard when the screen loads.

void initState() {
  super.initState();
  SystemChannels.textInput.invokeMethod('TextInput.hide');
}
0
2

First Connect the TextField with focusNode

 TextField(
focusNode: focusNode),

now for show/open the keyboard call

focusNode.requestFocus();

and for hide/dismiss keyboard call

 focusNode.unfocus();

and if you want to know keyboard is visible or not at that time check the belew condition

final keyBoardVisible = MediaQuery.of(context).viewInsets.bottom != 0;

here keyBoardVisible value is true if keyboard is visible and false for dismissed keyboard

1

FocusScope.of(context).unfocus() has a downside when using with filtered listView. Apart from so many details and concisely, use keyboard_dismisser package in https://pub.dev/packages/keyboard_dismisser will solve all the problems.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.