Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 229 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I dismiss the on screen keyboard?

#1
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());
}

Reply

#2
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.
Reply

#3
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;
}}
Reply

#4
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());
}
Reply

#5
Following code helped me to hide keyboard

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

#6
_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>[/*...*/],
),
),
);
}
Reply

#7
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
Reply

#8
**Note:** This answer is outdated. [See the answer for newer versions of Flutter][1].

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());


[1]:

[To see links please register here]

Reply

#9
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();
}
}
Reply

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

```
FocusManager.instance.primaryFocus.unfocus();
```
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through