Reputation: 15091
I want to improve the following code snippet by removing the hard coded number 5
(the number of elements in the list). How to do so?
child: TabBar(
tabs: <Widget>[
...List.generate(
5,
(i) => Tab(
child: Text([
'Text',
'Icons',
'Images',
'Simple Form',
'Proper Form'
][i]),
),
),
],
),
Upvotes: 2
Views: 2228
Reputation: 44056
Something like
tabs:
'Text/Icons/Images/Simple Form/Proper Form'.split('/').
map((e) => Tab(child: Text(e)).toList();
Upvotes: 3
Reputation: 2868
It's simple.
Define your list of Strings as the main source for your interface, iterate over it and create the Tab widgets.
TabBar(
tabs: [
'Text',
'Icons',
'Images',
'Simple Form',
'Proper Form'
].map(
(tabName) => Tab(child: Text(tabName))
).toList()
)
Upvotes: 4