在 Flutter 中将 GestureDector 添加到 Listview.builder 卡的正确方法?

Proper way to add GestureDector to Listview.builder card in Flutter?

我有一个 Expanded 小部件环绕着一张 Listview.builder 卡片。我怎样才能使我的卡不仅检测到 onTap,而且还能将变量传递到导航上的新 .dart 文件。我目前遇到尺码错误?

更新了代码

这是我的代码:

new Expanded(
                child: new ListView.builder(
                    itemCount: id == null ? 0 : id.length,
                    itemBuilder: (BuildContext context, int index) {
                      return new Card(
                        child: new Column(
                          children: <Widget>[
                            new Image.network(video[index]),
                            new Padding(padding: new EdgeInsets.all(3.0)),
                            new Text(title[index],
                            style: new TextStyle(fontWeight: FontWeight.bold,
                            color: Colors.black),
                            ),
                            new GestureDetector(onTap: (){
                              print(id[index]);
                            },)

                          ],
                        ),
                      );

                    }))

这是抛出的异常:

The following assertion was thrown during performLayout():
RenderPointerListener object was given an infinite size during layout.
This probably means that it is a render object that tries to be as big as possible, but it was put
inside another render object that allows its children to pick their own size.

我想通过 title[index]video[index] 类似于 iOS Swift 中的 didSelectRowAtIndexPath

您将 GestureDetector 添加为 Column 的一个子项,而 Flutter 不理解 UI 的哪一部分需要检测此 GestureDetector 的不同触摸事件(您没有具体说明您需要这个 GestureDetector 执行其任务的确切位置)

如果您需要整个 Card 进行交互,您需要将 Card 包裹在 GestureDecetor 中,如下所示

var id = ["title 1", "title 2", "title 3", "title 4", "title 5",];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView.builder(
          itemCount: id == null ? 0 : id.length,
          itemBuilder: (BuildContext context, int index) {
            return new GestureDetector( //You need to make my child interactive
              onTap: () => print(id[index]),
              child: new Card( //I am the clickable child
                child: new Column(
                  children: <Widget>[
                    //new Image.network(video[index]),
                    new Padding(padding: new EdgeInsets.all(3.0)),
                    new Text(id[index],
                      style: new TextStyle(fontWeight: FontWeight.bold,
                          color: Colors.black),
                    ),


                  ],
                ),),
            );
          }),
    );
  }

与 aziza 的建议类似,您可以查看 InkWell,它基本上是一个 GestureDetector,但具有 material 设计标志。

您还询问了如何将变量传递给另一个class。您可以通过在实例化中将它们作为构造函数变量移交来做到这一点。查看代码示例中的 onTap 方法。

代码可能如下所示:

@override
Widget build(BuildContext context) {
  return new Scaffold(
    body: new ListView.builder(
      itemCount: id == null ? 0 : id.length,
      itemBuilder: (BuildContext context, int index) {
        return new InkWell(
          onTap: () {
            Navigator.push(
              context,
              new MaterialPageRoute(
                builder: (context) {
                  return new OtherClass(id[index], video[index]);
                },
              ),
            );
          },
          child: new Card(
            child: new Column(
              children: <Widget>[
                //new Image.network(video[index]),
                new Padding(padding: new EdgeInsets.all(3.0)),
                new Text(id[index],
                  style: new TextStyle(fontWeight: FontWeight.bold,
                      color: Colors.black
                  ),
                ),
              ],
            ),
          ),
        );
      }),
  );
}

*代码未经测试