How to update a TextEditingController automatically when typing in a Form field? Flutter

Viewed 139

So my problem is that i have some TextEditingControllers that I fill with some input fields.

Here are some of them, for example:

final TextEditingController _noMuestreoController = TextEditingController();
  final TextEditingController _pecesCapturaController = TextEditingController();
  final TextEditingController _pesoCapturaController = TextEditingController();

Here's the widget I use for the form fields.

Widget formField(TextEditingController controller) {
return Container(
  height: 55,
  width: MediaQuery.of(context).size.width,
  decoration: BoxDecoration(
      color: Colors.white, borderRadius: BorderRadius.circular(13)),
  child: TextFormField(
    controller: controller,
    decoration: const InputDecoration(
        border: InputBorder.none,
        contentPadding: EdgeInsets.symmetric(vertical: 20, horizontal: 15)),
    style: const TextStyle(
      color: Colors.black,
      fontSize: 16,
    ),
  ),
);

}

My problem is that with some of this Controllers I perform some calculations and then put those results in some variables. Right now, calculations are done correctly, but the controllers only update their values when I close the keyboard on screen. What I would want is that everytime I input something in the formfields, the value of the controllers associated with said fields changed automatically.

Thanks.

1 Answers

You can use onChanged to listen to every input without needing to close your keyboard

as example:

TextFormField(
controller: controller,
onChanged:(newValue){
//Do Whatever You want with the new value here
}
decoration: const InputDecoration(
    border: InputBorder.none,
    contentPadding: EdgeInsets.symmetric(vertical: 20, horizontal: 15)),
style: const TextStyle(
  color: Colors.black,
  fontSize: 16,
  ),
),
Related