我正在快速创建一个动态类别列表,因此每次我从API添加新类别时,都会创建一个新的 Tab 按钮,如下所示:
我正在使用 GridView.builder 从API查看每个类别的产品,
问题,我如何使GridView在许多列表上都能工作?
示例:
类别1:
类别2:
,当我们按下类别1的选项卡按钮时,GridView.builder应该显示-Product1,Product2- 当我们按下类别2的选项卡按钮时,GridView.builder应该显示-Product3,Product4-。
那么如何使此类别显示在gridview.builder中?
我的代码:
DefaultTabController(
length: categories.length,
child: new Stack(children: <Widget>[
new CustomScrollView(
physics: NeverScrollableScrollPhysics(),
controller: _scrollController,
slivers: <Widget>[
new SliverAppBar(
title: TabBar(
isScrollable: true,
tabs: List<Widget>.generate(categories.length, (int index){
print(categories[0]);
return new Tab(icon: Icon(Icons.directions_car), text: "some random text");
}),
),
),
new SliverFillRemaining(
child: TabBarView(
children: List<Widget>.generate(categories.length, (int index){
return new GridShop();
}),
)),
]);
class GridShop extends StatefulWidget {
@override
_GridShop createState() => new _GridShop();
}
class _GridShop extends State<GridShop> {
List<Product> _products = new ProductsRepository().fetchAllProducts();
@override
Widget build(BuildContext context) {
return new Column(children: <Widget>[
new Container(
child: GridView.builder(
itemCount: _products.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: childAspectRatio),
itemBuilder: (BuildContext context, int index) {
return new Padding(
child:
ProductWidget(
product: _products[index]));
})
)),
]))
}
}