skip to Main Content
[Updated Question]

Hi I am a flutter beginner.
I want to add multiple arrays of widgets in TabBarView.
I have 3 Arrays with ListView containing Card Widgets in them.
Is it possible to put multiple Arrays in TabBarView, if it is how can I achieve that?

Here is my code.

@override
  Widget build(BuildContext context) {
    final List<Widget> stack = <Widget>[];
    final List<Widget> info  = <Widget>[
      _infoScreen();
    ];
    final List<Widget> addStuff   = <Widget>[
      _someAdditionalStuff()
    ];
    if (_queryProductError == null) {
      stack.add(
        ListView(
          children: <Widget>[
            _buildConnectionCheckTile(),
            _buildProductList(),
            _adsTitle(),
            _adsButton(),
            _buildRestoreButton(),
          ],
        ),
      );
    } else {
      stack.add(Center(
        child: Text(_queryProductError!),
      ));
    }
    if (_purchasePending) {
      stack.add(
        // TODO(goderbauer): Make this const when that's available on stable.
        // ignore: prefer_const_constructors
        Stack(
          children: const <Widget>[
            Opacity(
              opacity: 0.3,
              child: ModalBarrier(dismissible: false, color: Colors.grey),
            ),
            Center(
              child: CircularProgressIndicator(),
            ),
          ],
        ),
      );
    }

return MaterialApp(
  home: DefaultTabController(
    length: 3,
    child: Scaffold(
    appBar: AppBar(
        bottom: const TabBar(
          tabs: [
            Tab(icon: Icon(Icons.shopping_basket_outlined),
            text : 'Донације'),
            Tab(icon: Icon(Icons.info_outline),
            text : 'Инфо'),
            Tab(icon: Icon(Icons.directions_bike)),
          ],
        ),
      centerTitle: true,
      title: Text('Донације'),
      leading: IconButton(
            onPressed: (){ main();},
            icon: const Icon(Icons.keyboard_return_sharp),
        )
    ),
    body: new TabBarView(
      children:
        stack,
        info,
        addStuff,
    ),
  ),
  ),
);

}

This does not work and gives me a "Too much positional arguments exceptions in TabBarView.

The example below is with 3 Card Widgets and works fine but is not what I want:

return MaterialApp(
  home: DefaultTabController(
    length: 3,
    child: Scaffold(
    appBar: AppBar(
        bottom: const TabBar(
          tabs: [
            Tab(icon: Icon(Icons.shopping_basket_outlined),
            text : 'Донације'),
            Tab(icon: Icon(Icons.info_outline),
            text : 'Инфо'),
            Tab(icon: Icon(Icons.directions_bike)),
          ],
        ),
      centerTitle: true,
      title: Text('Донације'),
      leading: IconButton(
            onPressed: (){ main();},
            icon: const Icon(Icons.keyboard_return_sharp),
        )
    ),
    body: new TabBarView(
      children:
        _buildProductList
        _infoScreen
        _someAdditionalStuff

    ),
  ),
  ),
);

}

I would like to have 5 Widgets on Tab 1
and 2 Widgets on Tab 2 and 1 Widget on Tab 3

I tried a few things but I don’t get it.

I appreciate your help.

2

Answers


  1. Chosen as BEST ANSWER

    Ok i found out that per Tab, only one Widget is possible, so I managed it with ListTile and Column inside instead of Cards, so I have only One ListView Widget instead of a couple of Card Widgets. It doesn't look like I want it to do with the Card Widgets but I am good with this solution, because I didn't found another.


  2. Your children property in the TabBarView widget needs to be an array of widgets. Where array is equal to the length defined for the number of tabs.

    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
              bottom: const TabBar(
                tabs: [
                  Tab(icon: Icon(Icons.shopping_basket_outlined),
                  text : 'Донације'),
                  Tab(icon: Icon(Icons.info_outline),
                  text : 'Инфо'),
                  Tab(icon: Icon(Icons.directions_bike)),
                ],
              ),
            centerTitle: true,
            title: Text('Донације'),
            leading: IconButton(
                  onPressed: (){ main();},
                  icon: const Icon(Icons.keyboard_return_sharp),
              )
          ),
          body: TabBarView(
            children: [ //TabBarView wants an array of children where length === children.length
              stack,
              info,
              // need to add one more widget here because the length of the DefaultTabController is set to 3
            ]
          ),
        ),
      ),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search