单击列表视图后如何获取索引

问题描述

嗨,我设计了一个用flatbutton包裹的列表视图,但是单击下面的列表视图后,我无法获取索引。我被GestureDetector包裹住了。

List<GestureDetector> _buildListItemsfromFlower() {
    return ExcerciselistPojo.randomList.map((flowers) {
      var flatbutton = GestureDetector(
          child: Card(
              elevation: 2.0,child: new Row(
                textBaseline: TextBaseline.alphabetic,crossAxisAlignment: CrossAxisAlignment.start,mainAxisAlignment: MainAxisAlignment.start,children: <Widget>[
                  new Container(
                      child: new Column(
                    children: <Widget>[
                      new Container(
                        width: MediaQuery.of(context).size.width * 0.5,height: MediaQuery.of(context).size.height / 15,margin: EdgeInsets.fromLTRB(0,5,0),child: new Text(
                          flowers.name,style: TextStyle(
                              color: Colors.black,fontSize: 20,decoration: Textdecoration.none),),new Container(
                          margin: EdgeInsets.fromLTRB(15,10,width: MediaQuery.of(context).size.width * 0.5,child: new Text(
                            flowers.duration,style: TextStyle(
                                color: Colors.pink,)),],new Container(
                    margin: new EdgeInsets.fromLTRB(55,child: ImageSequenceAnimator(
                      "assets/images/" + flowers.imageUrl,"Pic_","webp",3,isAutoplay: true,color: null,fps: 2,isboomerang: false,isLooping: true,width: 80,height: 80,)));
      return GestureDetector(
        child: flatbutton,onTap: () {
          Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => ExcerciseDetails(
                ExcerciselistPojo.randomList[2].name,ExcerciselistPojo.randomList[2].image,ExcerciselistPojo.randomList[2].description),));
        },);
    }).toList();
  }

我要下一个具有索引值的dart文件。单击上面的列表视图后,请让我知道如何获取索引值。

解决方法

您需要将ExcerciselistPojo.randomList放入变量中,并从列表中访问索引

var yourRandomList = ExcerciselistPojo.randomList;

yourRandomList.map((flowers) {

    int index = yourRandomList.indexWhere((flowersInside) => flowers.imageUrl == flowersInside.imageUrl);

}).toList();

,

也许看看ListView.builder示例可以帮助您找到所需的答案。