skip to Main Content

this is my code and i want to create a textfield widget at runtime but i have some problems .

  1. i can’t use int.parse(controller.text)
    2 . when i create the textfields by using manual number i used in for loop, when i write a number in one of them the others get the same value but it is not what i want .
    3 . and i am confuse how should i save them in repository i would be thankful if you guys help me .

   for(int  i = 0 ; i <  num.parse(setController.text)    ; i++)
                              ItemTextField(controller: eSetController, themeData: themeData),

and there are some images i attached here to explain my question better
i want to get sets number and then create textfield exp if the sets number is 3 i want 3 textfield
and i want to save them in repository to make it editable .

enter image description here

2

Answers


  1. First understand you are using eSetController controller for all the text fields which is causing issue of same value on every field.

    The solution is create a list of object / Map with length exact number you entered setController controller e.g number is 2 then list should look like

    List controllers =  [ 
      {"input_text":""},
      {"input_text":""},
    ]
    

    Now you will iterate the list and create 2 fields and in onChange method pass the value in the correct index of list controller this is how you will get unique values for each field.

    Login or Signup to reply.
  2. Try this:

    class HomeScreen extends StatefulWidget {
      const HomeScreen({super.key});
    
      @override
      State<HomeScreen> createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      final TextEditingController editingController = TextEditingController();
      final List<TextEditingController> textController = <TextEditingController>[];
      final List<Widget> textFields = <Widget>[];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Expanded(
                      child: TextField(
                        controller: editingController,
                        keyboardType: TextInputType.number,
                      ),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        if (editingController.value.text.isNotEmpty) {
                          final String stringValue = editingController.value.text;
                          final int count = int.tryParse(stringValue) ?? 0;
                          addTextField(count);
                        } else {}
                      },
                      child: const Text("Add"),
                    ),
                  ],
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: textFields.length,
                    itemBuilder: (BuildContext context, int index) {
                      return textFields.isEmpty
                          ? const SizedBox()
                          : Row(
                              children: <Widget>[
                                Expanded(child: textFields[index]),
                                IconButton(
                                  onPressed: () {
                                    removeTextField(index);
                                  },
                                  icon: const Icon(Icons.clear),
                                )
                              ],
                            );
                    },
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    for (int i = 0; i < textController.length; i++) {
                      log("Index: $i - Value: ${textController[i].value.text}");
                    }
                  },
                  child: const Text("Log Value"),
                ),
              ],
            ),
          ),
        );
      }
    
      void addTextField(int count) {
        for (int i = 0; i < count; i++) {
          final TextEditingController editingController = TextEditingController();
          textController.add(editingController);
          textFields.add(TextField(controller: editingController));
        }
        setState(() {});
        return;
      }
    
      void removeTextField(int index) {
        textController.removeAt(index);
        textFields.removeAt(index);
        setState(() {});
        return;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search