skip to Main Content

I have an implementation such that I need to have controller values inside nested list views. But I am getting the same values for the sub-list view. My problem is that I need to access the primary index so that the controller values remain distinct for each list index. Here is the minimal code representation for the same.

import 'package:flutter/material.dart';

class NestedLists extends StatefulWidget {
  const NestedLists({super.key});

  @override
  State<NestedLists> createState() => _NestedListsState();
}

class _NestedListsState extends State<NestedLists> {
  List<Map<String, dynamic>> mainList = [];
  List<TextEditingController> xControllers = [];
  List<TextEditingController> yControllers = [];

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Padding(
          padding: const EdgeInsets.all(10.0),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text('Add Nested Lists'),
                    ElevatedButton(
                      onPressed: () {
                        setState(() {
                          mainList.add({
                            'sub_list': [],
                          });
                        });
                      },
                      child: const Text('Add another List'),
                    ),
                  ],
                ),
                if (mainList.isNotEmpty)
                  ListView.builder(
                    shrinkWrap: true,
                    itemCount: mainList.length,
                    itemBuilder: (BuildContext context, int index) => Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: Card(
                        child: Padding(
                          padding: const EdgeInsets.all(20.0),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    'Index $index',
                                  ),
                                  ElevatedButton(
                                    onPressed: () {
                                      setState(() {
                                        mainList[index]['sub_list'].add({});
                                      });
                                    },
                                    child: const Text('Add Sub List'),
                                  ),
                                ],
                              ),
                              if (mainList[index]['sub_list'].isNotEmpty)
                                Padding(
                                  padding: const EdgeInsets.all(20.0),
                                  child: ListView.builder(
                                    shrinkWrap: true,
                                    itemCount:
                                        mainList[index]['sub_list'].length,
                                    itemBuilder:
                                        (BuildContext context, int sIndex) {
                                      xControllers.add(
                                        TextEditingController(text: ''),
                                      );
                                      yControllers.add(
                                        TextEditingController(text: ''),
                                      );
                                      return Row(
                                        children: [
                                          Expanded(
                                              child: TextFormField(
                                            controller: xControllers[sIndex],
                                            decoration: const InputDecoration(
                                              border: OutlineInputBorder(),
                                            ),
                                          )),
                                          const SizedBox(
                                            width: 50,
                                          ),
                                          Expanded(
                                            child: TextFormField(
                                              controller: yControllers[sIndex],
                                              decoration: const InputDecoration(
                                                border: OutlineInputBorder(),
                                              ),
                                            ),
                                          ),
                                        ],
                                      );
                                    },
                                  ),
                                ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Here is the screenshot of the issue.
enter image description here

YOu can clearly see that I am getting the same value for Index 0 and Index 1 as I type along. But in reality, it should be unique. Any help will be appreciated. Thanks.

2

Answers


  1. for the sublist, you will need to different list of TextEditingController.
    you can use a map Map<int,List>

    store the main index in int which will represent the parent index.

    Login or Signup to reply.
  2. Here is an example to achieve your result.

    link to the output

    class NestedLists extends StatefulWidget {
      const NestedLists({super.key});
    
      @override
      State<NestedLists> createState() => _NestedListsState();
    }
    
    class _NestedListsState extends State<NestedLists> {
      
      List<Map> mainList = [];
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: Padding(
              padding: const EdgeInsets.all(10.0),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('Add Nested Lists'),
                        ElevatedButton(
                          onPressed: () {
                            setState(() {
                              mainList.add({ 
                                'x_controller': [TextEditingController(text: '')], 
                                'y_controller': [TextEditingController(text: '')] }
                              );
                            });
                          },
                          child: const Text('Add another List'),
                        ),
                      ],
                    ),
                    if (mainList.isNotEmpty)
                      ListView.builder(
                        shrinkWrap: true,
                        itemCount: mainList.length,
                        itemBuilder: (BuildContext context, int index) => Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: Card(
                            child: Padding(
                              padding: const EdgeInsets.all(20.0),
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text('Index $index'),
                                      ElevatedButton(
                                        onPressed: () {
                                          setState(() {
                                            mainList[index]['x_controller'].add(TextEditingController(text: ''));
                                            mainList[index]['y_controller'].add(TextEditingController(text: ''));
                                          });
                                        },
                                        child: const Text('Add Sub List'),
                                      ),
                                    ],
                                  ),
                                  Row(
                                    children: [
                                      Expanded(
                                        child: Column(
                                          children: (mainList[index]['x_controller'] as List<TextEditingController>).map((xController) {
                                            return TextFormField(
                                              controller: xController,
                                              decoration: const InputDecoration(
                                                border: OutlineInputBorder(),
                                              ),
                                            );
                                          }).toList(),
                                        ),
                                      ),
                                      const SizedBox(width: 10),
                                      Expanded(
                                        child: Column(
                                          children: (mainList[index]['y_controller'] as List<TextEditingController>).map((yController) {
                                            return TextFormField(
                                              controller: yController,
                                              decoration: const InputDecoration(
                                                border: OutlineInputBorder(),
                                              ),
                                            );
                                          }).toList(),
                                        ),
                                      ),
                                    ],
                                  )
                                  
                                ],
                              ),
                            ),
                          ),
                        ),
                      )
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search