To dismiss the on-screen keyboard, you can call the unfocus() method on the FocusNode associated with the TextFormField. Here's how you can modify your code to achieve this:
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
MyHomePageState createState() => new MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = new TextEditingController();
FocusNode _focusNode = FocusNode();
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.send),
onPressed: () {
setState(() {
// send message
_focusNode.unfocus(); // dismiss on-screen keyboard
_controller.clear();
});
},
),
body: new Container(
alignment: FractionalOffset.center,
padding: new EdgeInsets.all(20.0),
child: new TextFormField(
controller: _controller,
focusNode: _focusNode,
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());
}
In this modified code, we create a FocusNode instance and assign it to the focusNode property of the TextFormField. When the FloatingActionButton is pressed, we call the unfocus() method on the FocusNode instance to dismiss the on-screen keyboard. We also clear the TextEditingController so that the TextFormField is emptied. Finally, we dispose of the FocusNode instance in the dispose() method to release any resources it's holding onto.