Display Name
Display Name

Reputation: 15091

How to create a list of Tab from a list of String?

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

Answers (2)

Randal Schwartz
Randal Schwartz

Reputation: 44056

Something like

tabs:
  'Text/Icons/Images/Simple Form/Proper Form'.split('/').
    map((e) => Tab(child: Text(e)).toList();

Upvotes: 3

siega
siega

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

Related Questions