在flutter中添加示意器的正确方法



我有一个围绕卡的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.

我想通过ios swift中的 title[index]video[index]类似于 didSelectRowAtIndexPath

您将GestureDetector添加为Column的一个孩子,而Flutter不了解此 GestureDetector的UI部分需要检测到不同的触摸事件(您没有指定确切的位置您需要此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,它基本上是一个姿势,但具有材料设计的溅出。

您还询问了如何将变量传递给另一个类。您可以将它们作为构造函数变量传递到实例化中来做到这一点。在代码示例中查看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
                  ),
                ),
              ],
            ),
          ),
        );
      }),
  );
}

*代码未测试

相关内容

  • 没有找到相关文章

最新更新