Home > Net >  can't type into textfield after requesting focus
can't type into textfield after requesting focus

Time:05-28

when I input something from the textfield it should clear the text in the textfield and request focus and continue typing, the first two works but I can't type in the textfield once I request focus.
help?

FocusNode node = FocusNode();
TextField(
    focusNode: node,
    controller: controller,
    textAlign: TextAlign.center,
    onSubmitted: (value) {
        setState(() {
            if(value == (n1 * n2).toString()){
                score  = 10;
                n1 = Random().nextInt(13)   0;
                n2 = Random().nextInt(13)   0;
                print(true);
            }
            else {
                lives -= 1;
                print(false);
            }

         });
         controller.clear();
         sleep(Duration(milliseconds: 10));
         node.requestFocus();
     },
     autofocus: true,
     decoration: InputDecoration(
         border: InputBorder.none
     ),
     style: TextStyle(
     color: Colors.white,
     fontSize: 48
     ),
),

CodePudding user response:

Future.delayed(const Duration(milliseconds: 10), () {
  node.requestFocus();
});

Try this.

CodePudding user response:

Insure that your node initialization is not inside the build, also, your sleep method is not working like that, you should either do the following

void sleep(final Duration duration, {final VoidCallback? onDone}) {
    Future.delayed(duration, onDone);
}
//and use it like this,
sleep(Duration(milliseconds: 10), onDone: node.requestFocus);

OR

Future sleep(final Duration duration) async {
    await Future.delayed(duration);
}
//and use it like this
await sleep(Duration(milliseconds: 10));
node.requestFocus();

OR the known way, same as @aoiTenshi mentioned

  • Related