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.
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
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.
Here is an example to achieve your result.
link to the output