In flutter creating a tabbar and navigating through the various pages with a list passed as a reference, the pages no longer report the correct data
Each tab contains a title and a list of strings, this list is displayed in each TabView, but browsing through the pages does not display the correct list.
class _TabsFabDemoState extends State<TabsFabDemo> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: choices.length,
child: Scaffold(
appBar: AppBar(
title: const Text('Tabbed AppBar'),
bottom: TabBar(
isScrollable: true,
tabs: choices.map((Choice choice) {
return Tab(
text: choice.title,
icon: Icon(choice.icon),
);
}).toList(),
),
),
body: Column(
children: <Widget>[
GestureDetector(
onTap: (){
choices.add(Choice(title: 'CAR', icon: Icons.directions_car));
setState(() {});
},
child: Container(
height: 100.0,
color: Colors.red,
),
),
Expanded(
child: TabBarView(
children: choices.map((Choice choice) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Center(child: ListaLinkWidget(tab: choice)),
);
}).toList(),
),
),
],
),
),
),
);
}
}
class Choice {
Choice({this.title, this.icon});
final String title;
final IconData icon;
List<String> liststring = new List();
}
List<Choice> choices = <Choice>[
Choice(title: 'CAR', icon: Icons.directions_car),
Choice(title: 'BICYCLE', icon: Icons.directions_bike),
Choice(title: 'BOAT', icon: Icons.directions_boat),
Choice(title: 'BUS', icon: Icons.directions_bus),
Choice(title: 'TRAIN', icon: Icons.directions_railway),
Choice(title: 'WALK', icon: Icons.directions_walk),
];
class ListaLinkWidget extends StatefulWidget{
Choice tab;
ListaLinkWidget({Key key, this.tab}) : super(key: key);
@override
ListaLinkWidgetState createState() => ListaLinkWidgetState();
}
class ListaLinkWidgetState extends State<ListaLinkWidget> {
@override
void initState() {
super.initState();
widget.tab.liststring.add("a");
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.red,
child: Text(
widget.tab.liststring.toString()
),
);
}
}
I expected every page to have its own list and not go to interfere with that of the other pages, what am I wrong?
The init block of the widget is called so many times, how do I get it to be called only once?
Thanks