Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method once when text editing is completed fully in flutter TextFormField?

Tags:

flutter

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');
      },
like image 360
Tester12 Avatar asked Sep 09 '20 08:09

Tester12


People also ask

How do you call a function in TextField in Flutter?

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 ...

What is a text field in flutter?

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.

How do I modify the text of a textfield or Textform?

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.

How to perform any operation on user input data using textformfield?

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.

How does the texteditingcontroller work?

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.


Video Answer


2 Answers

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();
          },
like image 105
Tester12 Avatar answered Oct 22 '22 00:10

Tester12


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

like image 24
zex_rectooor Avatar answered Oct 22 '22 00:10

zex_rectooor