flutter - Flutter:根据所选类别在 GridView 内显示值
问题描述
我有一个显示 _customCard 的应用程序,它是 GridView 中的自定义卡。我想控制 GridView 中卡片的内容,以便可以根据 6 个不同类别的选定值 SelectedCategory 进行更改。
这是我控制 SelectedCategory 的地方:
_favouritePressed(int index){
setState(() {
selectedIndex = index;
});
}
下面是 GridView 的代码:
GridView.count(
crossAxisCount: 2,
children: <Widget>[
_customCard(
imageUrl: "assassin.png", item: "game", price: "\$50", count: 1
),
_customCard(
imageUrl: "sedan.png", item: "car", price: "\$25", count: 4
),
_customCard(
imageUrl: "blouse.png", item: "T-shirt", price: "\$20", count: 2
),
_customCard(
imageUrl: "toy.png", item: "kids", price: "\$2", count: 3
),
],
),
解决方案
我不确定我是否理解您的问题,但我会尝试解决方案:
GridView.count(
crossAxisCount: 2,
children: <Widget>[
if (selectedIndex==0)
_customCard(
imageUrl: "assassin.png", item: "game", price: "\$50", count: 1
),
if (selectedIndex==1)
_customCard(
imageUrl: "sedan.png", item: "car", price: "\$25", count: 4
),
if (selectedIndex==2)
_customCard(
imageUrl: "blouse.png", item: "T-shirt", price: "\$20", count: 2
),
if (selectedIndex==3)
_customCard(
imageUrl: "toy.png", item: "kids", price: "\$2", count: 3
),
],
),
对不起,如果我误解了你的问题。
推荐阅读
- c# - 从 bakcground 线程调用 UI 线程上的许多方法
- java - 方法覆盖总是运行时多态性吗?
- bootstrap-modal - 将外部内容插入引导模式
- android - android中的动态表格布局,其中行应该在下一次迭代开始之前被清除
- r - 从每一行产生一个矩阵(tidyverse)
- pdf - Flutter 创建具有不同页面大小和字体大小的 pdf 文件
- image-processing - 镜头畸变
- angularjs - AngularFire 错误 - 客户端无权访问所需数据
- java - 如果我不删除arraylist,程序就会关闭,它应该显示我的歌曲,但程序不会打开
- android - 28.0.3 aapt.exe'' 以非零退出值 1 结束