Joe Early
Joe Early

Reputation: 118

textInputAction: TextInputAction.next failing to move user focus to next element

When I use TextInputAction and set it to next, the button does change on my simulator device however it doesn't do anything, User interaction is not moved into the next element. Have opened a ticket on the FLutter `gitHUb page as well as in the chat rooms but no help so far and would like to keep the ball rolling on my training :)

https://github.com/flutter/flutter/issues/47749

Upvotes: 2

Views: 3981

Answers (1)

chunhunghan
chunhunghan

Reputation: 54367

There is bug in your code , you put _passwordFocusNode in email field and cause error
when _emailEditingComplete() , focus stay in email

void _emailEditingComplete() {
    FocusScope.of(context).requestFocus(_passwordFocusNode);
}

return TextField(
  controller: _passwordController,
  focusNode: _emailFocusNode,

return TextField(
  controller: _emailController,
  focusNode: _passwordFocusNode,

onEditingComplete and onSubmitted both work correctly in this case

working demo for test

enter image description here

full test code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final FocusNode _emailFocusNode = FocusNode();
  final FocusNode _passwordFocusNode = FocusNode();

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    _emailFocusNode.dispose();
    _passwordFocusNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              focusNode: _emailFocusNode,
              decoration: InputDecoration(
                labelText: 'Email',
                hintText: '[email protected]',
              ),
              keyboardType: TextInputType.emailAddress,
              textInputAction: TextInputAction.next,
              onEditingComplete: () {
                FocusScope.of(context).requestFocus(_passwordFocusNode);
              },
              onSubmitted: (val) {
                //FocusScope.of(context).requestFocus(_passwordFocusNode);
              },
            ),
            TextField(
              focusNode: _passwordFocusNode,
              decoration: InputDecoration(
                labelText: 'password',
              ),
              keyboardType: TextInputType.emailAddress,
              textInputAction: TextInputAction.done,
              onSubmitted: (val) {},
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

bug

enter image description here

Upvotes: 5

Related Questions