How to get index after clicking list view

179 Views Asked by At

Hi I have designed a List view wrapped with flatbutton but I am not able to get index after clicking the list view below is the code. I am wraped with 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, 0),
                        child: new Text(
                          flowers.name,
                          style: TextStyle(
                              color: Colors.black,
                              fontSize: 20,
                              decoration: TextDecoration.none),
                        ),
                      ),
                      new Container(
                          margin: EdgeInsets.fromLTRB(15, 10, 0, 0),
                          width: MediaQuery.of(context).size.width * 0.5,
                          height: MediaQuery.of(context).size.height / 15,
                          child: new Text(
                            flowers.duration,
                            style: TextStyle(
                                color: Colors.pink,
                                fontSize: 20,
                                decoration: TextDecoration.none),
                          )),
                    ],
                  )),
                  new Container(
                    margin: new EdgeInsets.fromLTRB(55, 0, 0, 0),
                    child: ImageSequenceAnimator(
                      "assets/images/" + flowers.imageUrl,
                      "Pic_",
                      0,
                      5,
                      "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();
  }

I want to to next dart file with index value. Please let me know how to get index value after clicking the above list view.

2

There are 2 best solutions below

1
On

Perhaps a look at ListView.builder examples would help you find the answer you seek.

0
On

You need to put your ExcerciselistPojo.randomList inside a variable and access the index from the list

var yourRandomList = ExcerciselistPojo.randomList;

yourRandomList.map((flowers) {

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

}).toList();