Easily move the focus between TextFormFields with FocusScopeNode

Form and TextFormField are very useful widgets when entering text input in Flutter.

Can we provide a convenient way of moving the input focus when pressing "next" on the keyboard?

With FocusScopeNode, this is super easy to do.

Say you have an email & password input form, which looks like this:

class EmailPasswordSignInForm extends StatefulWidget { @override _EmailPasswordSignInFormState createState() => _EmailPasswordSignInFormState(); } class _EmailPasswordSignInFormState extends State<EmailPasswordSignInForm> { final FocusScopeNode _node = FocusScopeNode(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); @override void dispose() { _node.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Form( key: _formKey, child: FocusScope( node: _node, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[ // email TextFormField( decoration: InputDecoration( labelText: 'Email', hintText: 'john@doe.com', ), textInputAction: TextInputAction.next, keyboardType: TextInputType.emailAddress, // move to the next field onEditingComplete: _node.nextFocus, ), // password TextFormField( decoration: InputDecoration( labelText: 'Password', ), obscureText: true, textInputAction: TextInputAction.done, // move to the next field onEditingComplete: _node.nextFocus, ), // submit RaisedButton( child: Text('Sign In'), onPressed: () {/* submit code here */}, ), ], ), ), ); } }

By adding a FocusScope and the associated FocusScopeNode, you can easily move the focus to the next TextFormField by passing _node.nextFocus to onEditingComplete.

And just as easily, you can call _node.previousFocus() if you need to go back.

NOTE: This works with TextField as well, so you can use it even without a Form.

Details matter, and these little time-savers can make our users happy.

Happy coding!

Want More?

Invest in yourself with my high-quality Flutter courses.

The Complete Dart Developer Guide

The Complete Dart Developer Guide

Learn Dart Programming in depth. Includes: basic to advanced topics, exercises, and projects. Fully updated to Dart 2.15.

Flutter Animations Masterclass - Full Course

Flutter Animations Masterclass - Full Course

Master Flutter animations and build a completely custom habit tracking application.

Flutter & Firebase Course: Build a Complete App for iOS & Android

Flutter & Firebase Course: Build a Complete App for iOS & Android

A full course with in-depth content, taking you from the basics all the way up to more advanced topics.

Flutter REST API Crash Course

Flutter REST API Crash Course

Build a Coronavirus Tracking App, and learn how to use REST APIs in Flutter.

Get the best Flutter tutorials, right in your inbox.

Join over 16,000 developers who receive my weekly Flutter email course & newsletter:

No spam, ever. Unsubscribe at any time.