I want to respond to the user pressing the 'done' button on their keyboard when typing in a TextFormField.
Code so far:
TextFormField(
autofocus: true,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelText: 'ENTER YOUR TASK'
),
),
use onFieldSubmitted or onEditingComplete property.
onEditingComplete
When a completion action is pressed, such as "done", "go", "send", or "search", the user's content is submitted to the controller and then focus is given up.
When a non-completion action is pressed, such as "next" or "previous", the user's content is submitted to the controller, but focus is not given up because developers may want to immediately move focus to another input widget within onSubmitted.
Example,
TextFormField(
onEditingComplete: (){
//do your stuff
},
)
onFieldSubmitted
onSubmitted is called when the user indicates that they are done editing the text in the field.
Example,
TextFormField(
onFieldSubmitted: (val){
// process
},
)
TextEditingController _textEditingController = new TextEditingController();
TextFormField(
controller: _textEditingController,
autofocus: true,
textInputAction: TextInputAction.done,
onEditingComplete: () {
FocusScope.of(context).requestFocus(new FocusNode());
print(_textEditingController.text);
//TODO your Response code for user
},
decoration: InputDecoration(labelText: 'ENTER YOUR TASK'),
),
Related
How to retain the focus in the TextField after clicking submit/next/done in keyboard?
This is my TextField:
TextField(
autofocus: true,
readOnly: !isReady,
focusNode: qtyNode,
controller: qtyController,
textAlign: TextAlign.right,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.number,
maxLength: 4,
onEditingComplete: () {},
onChanged: (s) {
try {
ref.read(qtyProvider.state).state = int.parse(s);
} catch (e) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(e.toString()),
backgroundColor: Colors.red,
),
);
}
},
onSubmitted: (x) async {
ref
.read(formDataProvider.notifier)
.updateSelected(context, qtyController.text);
qtyNode.requestFocus();
},
maxLengthEnforcement: MaxLengthEnforcement.enforced,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly
],
);
When the onSubmitted callback is called, I update my provider(riverpod) wit the latest value of the QTY entered by user. Then I go to the next data in the ListView and mark the next data as the new selected item.
But when I go to the next record, I still want to remain focus on my TextField.
I tried adding this solution but the behavior is different. The keyboard is still shown but when I click the numbers, it wont reflect to my TextField.
Update
It seems like a flutter issue, I made an issue in github and got redirected in this thread. You can follow up there if you also experience the same issue.
You need to use FocusNode for this. It's documented here with examples.
I want to catch OTP sms code but it doesn't work on IOS.
I need show the code upper of keyboard.
my code block is;
TextField(
controller: controller,
autofillHints: const <String>[AutofillHints.oneTimeCode],
keyboardType: TextInputType.number,
onChanged: onChanged,
),
From Flutter docs at https://master-api.flutter.dev/flutter/material/TextField/autofillHints.html "Some autofill hints only work with specific keyboardTypes". Try changing the keyboardType. For example:
TextField(
controller: controller,
autofillHints: const <String>[AutofillHints.oneTimeCode],
keyboardType: TextInputType.text,
onChanged: onChanged,
),
I am creating a firebase CRUD flutter app. I want to show the typed value in textfield as in update. So that users can update the value by erasing the typed value.
//define
var _fNameController = TextEditingController();
//set value which one you set
#override
void initState() {
_fNameController.text = 'jayesh';
super.initState();
}
// textfield
TextFormField(
keyboardType: TextInputType.text,
controller: _fNameController,
textInputAction: TextInputAction.next,
textCapitalization: TextCapitalization.words,
decoration: InputDecoration(
hintText: 'First Name',
//border: OutlineInputBorder(),
),
validator: validator.validatefName,
),
I have a TextFormField with focusNode:
TextFormField(
key: Key('login-username-field-key'),
controller: loginTextController,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.text,
onFieldSubmitted: (term){
_changeFocusField(context, _loginFocus, _passwordFocus);
},
focusNode: _loginFocus,
decoration: InputDecoration(
labelText: AppLocalizations.of(context).loginFieldUsername
),
),
And then:
// Check that text field initially is not focused
final TextFormField textField = tester.widget(find.byKey(Key('login-username-field-key')));
expect(textField.focusNode.hasFocus, isFalse);
But from the docs I saw that TextFormField doesn't have 'focusNode' property (like TextField.focusNode.hasFocus).
So how to check that behavior?
PS I mean we can use FocusNode listeners, but I don't want to do that just for testing purposes. It should be really simply field.focusNode like for TextField.
You should add listener, where you can check state of your textFormField.
The method hasFocus return true or false.
#override
void initState() {
super.initState();
_loginFocus.addListener(_onFocusChange);
}
void _onFocusChange(){
debugPrint("Focus: "+_focus.hasFocus.toString());
}
I have a problem while developing my app - how can I auto-focus a textfield after completing input on the previous field, without having to click the textfield?
Use focusNode property and onEditingComplete event. Event trigger after user click on enter on first textfield, after that focusNode request focus for the second TextField.
For example:
FocusNode focusNode = FocusNode();
#override
Widget build(BuildContext context) => Scaffold(
body: Column(
children: <Widget>[
TextField(
autofocus: true,
onEditingComplete: (() => focusNode.requestFocus())
),
TextField(
focusNode: focusNode,
)
]
)
);
More info at: https://flutter.dev/docs/cookbook/forms/focus
TextField(
onEditingComplete: () => FocusScope.of(context).nextFocus(),
),
For the last textfield you would change the 'nextFocus()' to 'unfocus()'
return Focus(
child: TextField(
onChanged: (value) => context.read<SignupBloc>().add(SignupPasswordChanged(value)),
controller: controller,
keyboardType: TextInputType.visiblePassword,
obscureText: true,
key: const Key('signupForm_passwordInput_textField'),
decoration: InputDecoration(
labelText: 'Password *',
errorText: state.password.invalid
? 'Passwords must be at least 10 characters in length\nand a minimum of 1 '
'upper case and 1 lower case letter [A-Za-z]'
: null,
),
),
);
you can also try with
textInputAction: TextInputAction.next
in TextFormField