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

时间:2017-09-26 00:21:13

标签: navigation widget flutter gesturedetector

我有一个Expanded小部件,它包含在Listview.builder的Card中。如何使我的卡不仅检测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]和&#39;视频[索引]&#39;类似于SWIFT中的didSelectRowAtIndexPath

2 个答案:

答案 0 :(得分:9)

您正在将String myjson = "a:130{/*JSON here*/}"; String newjson = myjson.substring(myjson.indexOf("{"), myjson.length()); 添加为GestureDetector的一个孩子,而Flutter不了解这个Column需要检测不同触摸事件的UI(您不是指定您需要这个GestureDetector执行其任务的确切位置

如果您需要整个GestureDetector互动,则需要将Card包裹在Card内,如下所示

GestureDecetor

答案 1 :(得分:5)

类似于aziza的建议,您可以查看InkWell,它基本上是一个GestureDetector,但具有材质设计。

您还询问了如何将变量传递给另一个类。您可以通过在实例化中将它们作为构造函数变量进行处理来实现。看一下代码示例中的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
                  ),
                ),
              ],
            ),
          ),
        );
      }),
  );
}

*代码未经过测试