Reputation: 131
I am building a list of widgets. I want to display the index of the widget in the list tile. How can i do that?Is it not being displayed because the key is assigned at runtime and the current index is trying to be inititalized before the widget is even built? if yes, how can i diplay the widget index? If no, then what is happening and again how can i display the index? been stuck on this problem way too long now being new to flutter.
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
List<Widget> _data = [];
Widget SubActivitiesListTiles() {
SizeConfig().init(context);
return Container(
child: Container(
child: AnimatedList(
shrinkWrap: true,
key: _listKey,
initialItemCount: _data.length,
itemBuilder: (context, index, animation) {
return _buildItem(_data[index], animation);
}),
),
);
Widget _buildItem(CustomListTile, Animation animation) {
return SizeTransition(
sizeFactor: animation,
child: Card(
shape: Border(bottom: BorderSide(width: 0.4, color: Colors.black12)),
elevation: 0,
child: CustomListTile,
),
);
}
Widget ActivityTile(context, obj) {
int currentIndex=_data.indexWhere((item) =>(item.key == uniqueKey));;
UniqueKey uniqueKey = new UniqueKey();
return Container(
key: uniqueKey,
color: Colors.white,
child: ListTile(
onTap: () {
setState(() {
currentIndex =
_data.indexWhere((item) =>(item.key == uniqueKey));
Index;
updateTile(currentIndex);
});
},
title: Text(
'$currentIndex',)
)
)
}
Upvotes: 0
Views: 1276
Reputation: 303
You can use widget.index.
In the source code, you can access the widget order in the list or listView with widget.index where this variable returns the corresponding widget value.
like this:
return TextFormField(
controller: _nameController,
onChanged: (v) => _MyFormState.friendsList[widget.index] = v,
decoration: InputDecoration(
hintText: 'enter index number'
),
validator: (v){
if(v.trim().isEmpty) return 'enter the value';
return null;
},
);
Upvotes: 1