flutter - ( flutter )在Grid View Builder中选择对项目有影响吗?

标签 flutter dart

如何更改CARD小部件的颜色,如果单击了gridview项之一,则CARD会更改颜色。但您只能选择一项。我该怎么做???

Widget listDenomPulsa(AsyncSnapshot <List<Payload>> snapshot) {
    return GridView.builder(
        shrinkWrap: false,
        scrollDirection: Axis.vertical,
        itemCount: snapshot.data.length,
        gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: MediaQuery.of(context).size.width / (MediaQuery.of(context).size.height / 3),
        ),
        itemBuilder: (BuildContext context, int index) {
          bool _selectItem = false;
          return GestureDetector(
            onTap: () {
              setState(() {
                  _selectItem = true;
              });
            },
            child: Card(
              color: _selectItem ? Colors.blue:Colors.amber,
              child: Container(
                height: SizeConfig.heightMultiplier * 3,
                child: Padding(
                  padding: EdgeInsets.all(SizeConfig.heightMultiplier * 3),
                  child: Text(
                    snapshot.data[index].desc,
                    style: AppTheme.styleSubTitleBlackSmall,
                  ),
                ),
              ),
            ),
          );
        }
    );
  }

最佳答案

这将完美地工作,请检查以下代码:

class MyGridView extends StatefulWidget {
  @override
  _MyGridViewState createState() => _MyGridViewState();
}

class _MyGridViewState extends State<MyGridView> {

  // set an int with value -1 since no card has been selected  
  int selectedCard = -1;

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        shrinkWrap: false,
        scrollDirection: Axis.vertical,
        itemCount: 10,
        gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: MediaQuery.of(context).size.width /
              (MediaQuery.of(context).size.height / 3),
        ),
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            onTap: () {
              setState(() {
                // ontap of each card, set the defined int to the grid view index 
                selectedCard = index;
              });
            },
            child: Card(
              // check if the index is equal to the selected Card integer
              color: selectedCard == index ? Colors.blue : Colors.amber,
              child: Container(
                height: 200,
                width: 200,
                child: Center(
                  child: Text(
                    '$index',
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ),
            ),
          );
        });
  }
}

输出:
enter image description here
enter image description here

我希望这可以帮助你。

关于flutter - ( flutter )在Grid View Builder中选择对项目有影响吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61586633/

相关文章:

android - '_Smi' 不是类型 'bool' 的子类型 - Flutter MethodChannel

flutter - “如果为空”运算符,返回一个空字符串

dart - flutter 中选择的轨道单选按钮

flutter :如何在将导航器与页面一起使用时处理后退按钮按下

android - 恢复购买在 Flutter/Google Play 中不起作用

flutter - 'TextStyle ?' can' t 被分配给参数类型 'TextStyle'

flutter - Flutter-如果NavigationBar不可见,如何更改BottomNavigationBar的索引?

flutter - 无法在初始化程序中访问实例成员 'userEmail'

flutter - 错误 [Get] 检测到 GetX 的不当使用。使用底部导航栏

datetime - Dart -日期差异