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