我如何仅突出显示GridView.build上的onTaped项?

问题描述 投票:0回答:1

我仅尝试更改UI中所选项目的边框。

只能选择一个和一个项目,例如,如果我点击索引3的项目,

显示该项目的容器/小部件变为绿色(为边框),其余为红色

用户可以通过点击另一个项目来更改对选项的选择,然后再次执行相同的过程...

任何帮助将不胜感激。

Container(
            decoration: BoxDecoration(
              color: Colors.black87,
            ),
            height: double.infinity,
            child: StreamBuilder(
            stream: Firestore.instance.collection("cats").snapshots(),
            builder: (BuildContext  context, AsyncSnapshot snapshot)
            {
              // if (snapshot.connectionState == ConnectionState.waiting) return Center(child: CircularProgressIndicator());
              if (snapshot.hasData)
              {
                return GridView.builder(

                  itemCount: snapshot.data.documents.length,
                  itemBuilder: (context, index) {

                    final key = new GlobalKey<ScaffoldState>();
                    List<DocumentSnapshot> docs = snapshot.data.documents;
                    DocumentSnapshot ds = docs[index];

                    return Container(
                      margin : EdgeInsets.symmetric(horizontal: 20.0,vertical: 20.0),
                      decoration: BoxDecoration(
                        color: Colors.grey[300],
                        image: DecorationImage(
                          image: NetworkImage(ds["img"].toString()),
                          fit: BoxFit.fill,
                        ),
                        borderRadius: BorderRadius.circular(20),
                        border: selected_ItemBorder,
                      ),
                      child: InkWell(
                        onTap: () {
                          setState(() { // I played around this but no results ..
                            for (var item in docs) {
                              if (item.documentID == ds.documentID){
                                selected_ItemBorder = null;
                              }else{
                                selected_ItemBorder = Border.all(color: Colors./*black54*/green ,style: BorderStyle.solid,width: 2);
                                print(item["name"]);

                              }
                            }
                          });
                          // print(ds["name"]);
                          // key.currentState.removeCurrentSnackBar();
                          Scaffold.of(context).showSnackBar(
                            new SnackBar(
                              duration: Duration(milliseconds: 600),
                              backgroundColor: Colors.black87,
                              behavior: SnackBarBehavior.fixed,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.only(topLeft: Radius.circular(40), topRight: Radius.circular(40)),
                                // side: BorderSide(width: 1,color: Colors.green,style: BorderStyle.solid)
                              ),
                              content: Text(

                               "Selected : ${ds["name"]}", 
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Colors.white60,
                                  // fontWeight: FontWeight.bold,
                                ),
                              )
                            ),

                          );
                        },

                        customBorder: CircleBorder(),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Expanded(
                              child: Container(
                              height: 25,
                              child: Padding(
                                padding: const EdgeInsets.only(top: 2),
                                child: Text(
                                  ds["name"],
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    fontSize: 14,
                                    fontWeight: FontWeight.bold,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                              decoration: BoxDecoration(
                                color: Colors.black38,
                                borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
                                // border: Border(bottom: BorderSide(color: Colors.black54,style: BorderStyle.solid,width: 1))
                              ),
                              ),
                            )
                          ],
                        ),
                      ),

                    );
                  },
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2
                  ),
              );
            }
            // else {
            //   return Err(); // _edit
            // }
          },
        ),
      ),
flutter dart android-gridview
1个回答
0
投票

没关系,我设法做到了:

i使变量int indSel = 0; //选择索引0的默认项然后在边界:indSel == index?绿色Border():红色..或其他任何东西,

然后轻按:

 setState(() {
     indSel = index;
 });
© www.soinside.com 2019 - 2024. All rights reserved.