【问题标题】:Wrapping Gesture detector around a card将手势检测器包裹在卡片周围
【发布时间】:2018-07-13 04:16:55
【问题描述】:

现在我在页面主体上有一个按钮,但无法实现这个按钮(路由到不同的页面)只封装卡片

child: new Text(data["data"][index]["title"]),

它在 itemBuilder 中,所以我想我必须做一个 GestureDetector。我试图将那个孩子放入 GestureDetector 方法,但除非它是整个身体,否则无法让它工作。

@override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text("Listviews"),
        ),
        body: new GestureDetector(
          onTap: () {
            Navigator.push(context,
              MaterialPageRoute(builder:  (context) => StandardsControlPage()),
            );
          },
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: new ListView.builder(
              itemCount: data == null ? 0 : data["data"].length,
              itemBuilder: (BuildContext context, int index) {
                return new Card(
                  child: new Text(data["data"][index]["title"]),
                );},
            ),
          ),
        ),
      );
    }}

如果我有多个按钮可以通过不同的路径按下,则此示例将不起作用,并且想知道如果无论如何要实现一个具有该路径的按钮到那个孩子,我会怎么做?

【问题讨论】:

  • 所以您尝试将 Card 放入 GestureDetector。它不工作?
  • 我是在退回新卡之后放它(----还是我把它放在它之前,似乎每次我把它放在itemBuilder之后它似乎都搞砸了

标签: flutter


【解决方案1】:
return new Scaffold(
    appBar: new AppBar(
      title: new Text("Listviews"),
    ),
    body: Container(
      padding: EdgeInsets.all(16.0),
      child: new ListView.builder(
        itemCount: data == null ? 0 : data["data"].length,
        itemBuilder: (BuildContext context, int index) {
          return new GestureDetector(
            child: new Card(
              child: new Text(data["data"][index]["title"]),
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => StandardsControlPage()),
              );
            },
          );
        },
      ),
    ));

我认为这会奏效。

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 2018-09-08
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多