(扑动)在Grid View Builder中选择对项目的效果 [英] (Flutter) Select Effect on items in Grid View Builder

查看:78
本文介绍了(扑动)在Grid View Builder中选择对项目的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have items loaded in GridView.builder from sqflite database. Since the model class of it is not Stateful, ofcourse, I am not able to create Select effect on the items from there.

我的选择效果是这样的:

What i mean by select effect is this:

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

When users tap on an item, it is selected

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

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

I am able to create a select effect, but it selects all items if I tap on any one item. How can create select effect for each individual item.

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

So how can I create select effect for each individual item?

聚苯乙烯我只在代码中写了相关的东西

P.S. I have written only relevant things in the code

推荐答案

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

Because of itemList index is not managed for selection and unselection, Your items data loaded in GridView.builder from sqflite database, you are manage selection manualy through the local variable into GridView,

您需要在选择项表中插入选择字段,而不是局部变量;当将插入项首次插入表中时,默认值item为false时,将插入选择项

Instead of local variable you need to insert selection field into item table, when inset item into table first time default value item is false.

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

然后在onTap之后管理项目选择

Then after onTap manage the item selection

这篇关于(扑动)在Grid View Builder中选择对项目的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆