【问题标题】:(Flutter) Select Effect on items in Grid View Builder(Flutter) 在 Grid View Builder 中选择对项目的影响
【发布时间】:2019-04-05 17:56:11
【问题描述】:

我从 sqflite 数据库中加载了 GridView.builder 中的项目。由于它的模型类不是有状态的,当然,我无法从那里对项目创建 Select 效果。

我所说的选择效果是这样的:

当用户点击一个项目时,它被选中

GridView.builder(
                gridDelegate:.....,
                itemBuilder: (BuildContext context, int index) {
             bool _selectItem = false;
                  return Stack(
               children: <Widget>[
               itemsList[index]),        //====Actual Item=====//
             InkWell(onTap: () {         //===To create Select Effect====//
                    setState(() {
                      if (_selectItem == false) {
                        _selectItem = true;
                        print("Item Selected");
                      } else {
                        _selectItem = false;
                        print("Item UnSelected");
                      }
                    });
                  },
                  child: Opacity(
                    opacity: _selectItem == true ? 0.5 : 0.0,
                    child: Icon(Icons.select)                            
                   ),]); },
                itemCount: itemsList.length, 
                 ))

我可以创建一个选择效果,但如果我点击任何一个项目,它会选择所有项目。如何为每个单独的项目创建选择效果。

那么如何为每个单独的项目创建选择效果?

附:我在代码中只写了相关的东西

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    由于 itemList index 没有管理选择和取消选择, 您的项目数据从 sqflite 数据库加载到 GridView.builder 中, 您正在通过本地变量手动管理选择到 GridView,

    您需要将选择字段插入到项目表中,而不是局部变量, 第一次在表中插入项时默认值项为false。

     InkWell(onTap: () {         //===To create Select Effect====//
                    setState(() {
                      itemList[index].selectItem  = !itemList[index].selectItem 
                    });
                  },
    

    然后在onTap后管理item选择

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多