0

嗨,我设计了一个用 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, 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();
  }

我想下一个带有索引值的飞镖文件。单击上面的列表视图后,请告诉我如何获取索引值。

4

2 回答 2

1

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

var yourRandomList = ExcerciselistPojo.randomList;

yourRandomList.map((flowers) {

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

}).toList();

于 2020-09-15T15:30:02.527 回答
-1

也许看看 ListView.builder 示例会帮助您找到您所寻求的答案。

于 2020-09-15T15:20:04.593 回答