首页 > 解决方案 > 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
            ),
          ],
        ),

标签: flutterdartgridviewcard

解决方案


我不确定我是否理解您的问题,但我会尝试解决方案:

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
          ),
      ],
    ),

对不起,如果我误解了你的问题。


推荐阅读