Flutter: How to get initialized value as the final value if the TextField widget is not changed

Mahisha Patel

This is the code for the TextField. I've initialized it and I want this value to get print when a button is pressed if the text field is not changed.

Container(
  width: 80,
  child: TextField(
    controller: TextEditingController()..text = '${json['satur']}',
    onChanged: (value) => {
      satur = value,
    },
    keyboardType: TextInputType.number,
    textAlign: TextAlign.center,
    style: const TextStyle(
      color: Colors.orangeAccent,
    ),
    cursorColor: Colors.orangeAccent,
    decoration: const InputDecoration(
      contentPadding: EdgeInsets.symmetric(
          vertical: 10.0, horizontal: 20.0),
      border: OutlineInputBorder(
        borderRadius:
            BorderRadius.all(Radius.circular(32.0)),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(
            color: Colors.orangeAccent, width: 1.0),
        borderRadius:
            BorderRadius.all(Radius.circular(32.0)),
      ),
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
            color: Colors.orangeAccent, width: 2.0),
        borderRadius:
            BorderRadius.all(Radius.circular(32.0)),
      ),
    ),
  ),
  //flexible
),

This is the code for button:

Material(
  color: Colors.deepOrangeAccent,
  borderRadius: const BorderRadius.all(Radius.circular(30.0)),
  elevation: 5.0,
  child: MaterialButton(
    onPressed: () {
      print("Success");
      print('${satur}');
    },
    minWidth: 100.0,
    height: 42.0,
    child: const Text(
      'Evaluate',
      style: TextStyle(color: Colors.white, fontSize: 20),
    ),
  ),
),

Your help will be appreciated.

sajith lakmal

You have to declare your TextEditingController outside the build method and declare it as a field of the State Class. then when pass it to your TextField as the controller. then you can read the current value of the TextField using that controller as follows.

// inside your State class

TextEditingController textEditingController = TextEditingController();
//..............

// your text field 
child: TextField(
            controller: textEditingController..text = '${json['satur']}',

//.....................

//inside your onPressed method
onPressed: () {
                        String value = textEditingController.value.text;
                        print("Success");
                        print(value);
                      },

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get the value of TextField in a custom created Widget in Flutter

how to get value in textfield flutter

How to get the TextField value in flutter

How to get textfield value from widget to bloc

How to get flutter datepicker value to the textfield?

how to get flutter textfield value to datepicker

Flutter - how do you update the value of textfield without reopening the widget?

how to get value from a widget in flutter

how to get value from Custom Widget in flutter?

How to track if textField value is changed by rxSwift?

Flutter - how to get TextField value and display it as a new text

In Flutter How to pass value to the widget

How is to get a value down in the widget build other than setstate flutter

how to get value from function and display in text widget flutter

How to get the value of a textfield into another Textfield?

Flutter listening to textField value changes from other widget

Set flutter textfield inside Listview and get the value of each textfield on submit

Flutter get the value of a widget class variable

how to set value style in center (at TextField Flutter)

How to change TextField value from state in Flutter?

How to show Button Value in TextField in Flutter?

flutter: how to pass textfield value in api body

How to handle null value in TextField. [Flutter]

How do I center the value of the textfield (flutter)

How to input the value received as a parameter into the TextField Flutter

Flutter: Save TextField value

Flutter set value to a textfield

how to get the value of the widget select?

Flutter : How to pass a value that is not final to another class?

TOP Ranking

HotTag

Archive