Home > Software design >  Flutter - TextEditingController does not work
Flutter - TextEditingController does not work

Time:10-13

I trying to get data from 4 TextFields (item code, item name, item unit, item quantity) to pass them to my database using API. But it not working at all. I am new to programming and Flutter is my second language.

Here is my code:

class _AddItemPageState extends State<AddItemPage> {
  final textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: const Text("Add Item",
              style: TextStyle(
                  color: Colors.white,
                  fontFamily: "Quicksand",
                  fontWeight: FontWeight.bold)),
          centerTitle: true,
          backgroundColor: Colors.green,
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                TextField(
                  controller: textController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: textController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: textController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: textController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),

                ElevatedButton.icon(
                    onPressed: () async {
                      final Map<String, String> data = ({
                        'code': textController.text,
                        'name': textController.text,
                        'unit': textController.text,
                        'quantity': textController.text,
                      });
                      // API 
                    },
                    icon: Icon(Icons.add),
                    style: ButtonStyle(
                        padding: MaterialStateProperty.all(
                            EdgeInsets.fromLTRB(0, 16, 0, 16)),
                        backgroundColor:
                        MaterialStateProperty.all<Color>(Colors.green)),
                    label: Text(
                      "Add Item",
                      style: TextStyle(fontFamily: "Quicksand", fontSize: 16),
                    ))
              ],
            ),
          ),
        ));
  }
}

i have searched for a day but still have no found anything. any help will be appreciated. thank you!!!

CodePudding user response:

TextEditingController can only be used for one TextField. So, instead of using textController for 4 text fields, you should use 4 TextEditingController.

I updated your code:

class _AddItemPageState extends State<AddItemPage> {
  final firstController = TextEditingController();
  final secondController = TextEditingController();
  final thirdController = TextEditingController();
  final fourthController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: const Text("Add Item",
              style: TextStyle(
                  color: Colors.white,
                  fontFamily: "Quicksand",
                  fontWeight: FontWeight.bold)),
          centerTitle: true,
          backgroundColor: Colors.green,
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                TextField(
                  controller: firstController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: secondController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: thirdController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextField(
                  controller: fourthController,
                  style: TextStyle(fontFamily: "Quicksand", fontSize: 18),
                ),
                const SizedBox(
                  height: 8,
                ),

                ElevatedButton.icon(
                    onPressed: () async {
                      final Map<String, String> data = ({
                        'code': textController.text,
                        'name': textController.text,
                        'unit': textController.text,
                        'quantity': textController.text,
                      });
                      // API 
                    },
                    icon: Icon(Icons.add),
                    style: ButtonStyle(
                        padding: MaterialStateProperty.all(
                            EdgeInsets.fromLTRB(0, 16, 0, 16)),
                        backgroundColor:
                        MaterialStateProperty.all<Color>(Colors.green)),
                    label: Text(
                      "Add Item",
                      style: TextStyle(fontFamily: "Quicksand", fontSize: 16),
                    ))
              ],
            ),
          ),
        ));
  }
}
  • Related