Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter TextField with controller resetting after TextInputType change

Tags:

flutter

dart

I'm using a fullscreen dialog in my app to make the user input values into text fields. Some of these values are Strings and some are ints. So I want to change the keyboard accordingly to make the unnecessary keys disappear. Here's my implementation:

import 'package:flutter/material.dart';
import 'package:meal_tracker/model/food_model.dart';

class AddFoodDialog extends StatelessWidget {
  TextEditingController nameController = TextEditingController(),
      carbsController = TextEditingController(),
      fatController = TextEditingController(),
      proteinController = TextEditingController(),
      amountController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Add Food"),
        actions: <Widget>[
          FlatButton(
            child: Text(
              "SAVE",
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () {
              _handleSave(context);
            },
          )
        ],
      ),
      body: Form(
          child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            TextFormField(
              decoration: InputDecoration(labelText: "Name"),
              controller: nameController,
            ),
            TextFormField(
              decoration: InputDecoration(labelText: "Carbs per 100g"),
              controller: carbsController,
              keyboardType: TextInputType.number,
            ),
            TextFormField(
              decoration: InputDecoration(labelText: "Fat per 100g"),
              controller: fatController,
              keyboardType: TextInputType.number,
            ),
            TextFormField(
              decoration: InputDecoration(labelText: "Protein per 100g"),
              controller: proteinController,
              keyboardType: TextInputType.number,
            ),
            TextFormField(
              decoration: InputDecoration(labelText: "Amount in g"),
              controller: amountController,
              keyboardType: TextInputType.number,
            ),
          ],
        ),
      )),
    );
  }

  void _handleSave(BuildContext context) {
    print("Dialog text: " + nameController.text);
    Navigator.of(context).pop(Food(
        name: nameController.text,
        carbsPer100g: double.parse(carbsController.text),
        fatPer100g: double.parse(fatController.text),
        proteinPer100g: double.parse(proteinController.text),
        amount: double.parse(amountController.text)));
  }
}

The problem is that when I input something in the "Name" TextField, which has TextInputType.text and then tap into another one of the TextFields, which has TextInputType.number, the "Name" field clears out for no apparent reason. That also happens the other way around.

like image 962
leodriesch Avatar asked Jul 25 '18 11:07

leodriesch


People also ask

How do you AutoFill TextField in Flutter?

Also, as you already mentioned, you need to configure an AutoFill service in the system settings. Working sample code: @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( title: Text("Login"), ), body: AutofillGroup( child: Column( children: [ TextField( autofillHints: [AutofillHints.

How do you get rid of the Underfield of a TextField in Flutter?

To remove TextField underline/border in Flutter, you can simply set the border property to InputBorder. none. This will remove the underline for all states such as Focused, Enabled, Error, Disabled.


Video Answer


1 Answers

I had to put the TextEditingControllers inside a Stateful Widget, that did end up working. Thanks to Jonah Williams for the fast reply!

like image 85
leodriesch Avatar answered Oct 17 '22 06:10

leodriesch