In Flutter TextFormField, I want to call a method once editing is completed fully,I don't want to call the method while the user is changing the text in text form field(By using listener or by onchanged function), By using OnEditingComplete Function I can achieve this requirement, but the issue is oneditingcomplete is called only when the done button in the keyboard is tapped, when changing the focus after editing from one text field to another textfield onEditingComplete function is not working, So what is the best way to detect once editing is complete fully by the user.
`TextField(
onEditingComplete: () {//this is called only when done or ok is button is tapped in keyboard
print('test');
},
the solution you are looking for is gestureDetector, but it depends where you want to call the function if you want to call the function only when tapped inside the form area but outside the textfield then wrap the form with gesture detector and use onTap: (){}, or if you want to call the same on whole screen tapped ...
Text fields allow users to type text into an app. They are used to build forms, send messages, create search experiences, and more. In this recipe, explore how to create and style text fields. Flutter provides two text fields: TextField and TextFormField. TextField is the most commonly used text input widget.
There is another powerful way by setting a TextEditingController as the controller of a TextField or TextFormField. Whenever the user modifies the text field, the controller notifies its listeners so that they know the text and selection properties. TextEditingController also allows you to modify the text and selection.
We can perform any operation on that user input data using TextFormField. You can use that user input, can send and show that input. You can store it into a TextEditingController type object. To create a TextFormField in Flutter, Flutter provides a TextFormField () widget.
Whenever the user modifies the text field, the controller notifies its listeners so that they know the text and selection properties. TextEditingController also allows you to modify the text and selection.
Using the FocusScopeWidget helps me resolve this issue https://api.flutter.dev/flutter/widgets/FocusNode-class.html,
When user press done button in the keyboard onsubmitted function is called and when user change focus focus scope widget is used.
FocusScope(
onFocusChange: (value) {
if (!value) {
//here checkAndUpdate();
}
},
child: TextFormField(
key: Key('productSet${DateTime.now().toIso8601String()}'),
getImmediateSuggestions: true,
textFieldConfiguration: TextFieldConfiguration(
onSubmitted: (cal) {
//here checkAndUpdate();
},
Use TextFormField and some sort of trigger like button pressed. Use Global key to keep state of form, when save the form
...
class _Form extends State<Initialize> {
final _formKey = GlobalKey<FormState>();//create global key with FormState type
String _variableValue; //to hold data after save the form
@override
Widget build(BuildContext context) {
return form(
key: _formkey,
child: Column(
children: [
TextFormField(
onSaved: (value) {
_variableValue= value;
}
),
OutlineButton.icon(
onPressed: () {
_formKey.currentState.save();//should call this method to save value on _variableValue
},
icon: Icon(Icons.plus)
),
],
),
),
}
}
good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With