Multiple List in TabBarView with flutter

huangapple go评论85阅读模式
英文:

Multiple List<Widget> in TabBarView with flutter

问题

你想在TabBarView中添加多个数组的小部件,但你遇到了“Too much positional arguments exceptions in TabBarView”的问题。以下是一种可能的解决方案:

body: TabBarView(
  children: [
    Column(
      children: [
        ...stack,
        ...info,
        ...addStuff,
      ],
    ),
    // Add the widgets for Tab 2 here
    // Add the widgets for Tab 3 here
  ],
)

这里,我将stackinfo,和addStuff 数组合并到一个Column小部件中,然后将该Column作为Tab 1的内容。你可以在Tab 2和Tab 3的部分分别添加所需的小部件。

请注意,你需要根据自己的需求在Tab 2和Tab 3的部分添加适当的小部件,以便实现你想要的5个、2个和1个小部件的布局。这个示例只是为了帮助你理解如何将不同的小部件放在不同的Tab中。

希望这可以帮助你解决问题。如果还有其他问题,请随时提出。

英文:

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?
How can I achieve that?

Here is my code.

    @override
      Widget build(BuildContext context) {
        final List&lt;Widget&gt; stack = &lt;Widget&gt;[];
        final List&lt;Widget&gt; info  = &lt;Widget&gt;[
          _infoScreen();
        ];
        final List&lt;Widget&gt; addStuff   = &lt;Widget&gt;[
          _someAdditionalStuff()
        ];
        if (_queryProductError == null) {
          stack.add(
            ListView(
              children: &lt;Widget&gt;[
                _buildConnectionCheckTile(),
                _buildProductList(),
                _adsTitle(),
                _adsButton(),
                _buildRestoreButton(),
              ],
            ),
          );
        } else {
          stack.add(Center(
            child: Text(_queryProductError!),
          ));
        }
        if (_purchasePending) {
          stack.add(
            // TODO(goderbauer): Make this const when that&#39;s available on stable.
            // ignore: prefer_const_constructors
            Stack(
              children: const &lt;Widget&gt;[
                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 : &#39;Донације&#39;),
                Tab(icon: Icon(Icons.info_outline),
                text : &#39;Инфо&#39;),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
          centerTitle: true,
          title: Text(&#39;Донације&#39;),
          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 : &#39;Донације&#39;),
                Tab(icon: Icon(Icons.info_outline),
                text : &#39;Инфо&#39;),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
          centerTitle: true,
          title: Text(&#39;Донације&#39;),
          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.

答案1

得分: 1

TabBarView 组件中的 children 属性需要是一个小部件数组,其中数组的长度应等于标签的数量。

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 需要一个小部件数组,其中长度 === children.length
          stack,
          info,
          // 还需要在这里添加一个小部件,因为 DefaultTabController 的长度设置为 3
        ],
      ),
    ),
  ),
);
英文:

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 : &#39;Донације&#39;),
              Tab(icon: Icon(Icons.info_outline),
              text : &#39;Инфо&#39;),
              Tab(icon: Icon(Icons.directions_bike)),
            ],
          ),
        centerTitle: true,
        title: Text(&#39;Донације&#39;),
        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
        ]
      ),
    ),
  ),
);

答案2

得分: 0

我发现每个标签页只能有一个小部件,所以我使用了ListTile和内部的Column来管理它,而不是使用Card,所以我只有一个ListView小部件,而不是几个Card小部件。它看起来不像我希望使用Card小部件那样,但我对这个解决方案感到满意,因为我没有找到其他的方法。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月9日 02:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定