代码之家  ›  专栏  ›  技术社区  ›  AIM Digital Technologies

颤振列表视图项单击侦听器

  •  9
  • AIM Digital Technologies  · 技术社区  · 7 年前

    我有一个 ListView 我想导航到项目的下一页单击。

    列表视图 Controller . 但我找不到任何例子。

    4 回复  |  直到 7 年前
        1
  •  47
  •   markt    7 年前

    例如。

     body: ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    child: Text(index.toString()),
                    onTap: () => Scaffold
                        .of(context)
                        .showSnackBar(SnackBar(content: Text(index.toString()))),
                  );
                },
                itemCount: 10));
    

    此代码将显示一个包含您点击的ListItem索引的快餐店。

    一旦您有了该项目的索引,您就可以使用此问题的其他答案提供的代码导航到新页面。

        2
  •  37
  •   CopsOnRoad    5 年前

    如果您使用的是 ListView.builder ,你可以使用 ListTile 添加 onTap . 这将确保您具有材质涟漪效应。

    ListView.builder(
      itemBuilder: (_, i) {
        return ListTile(
          title: Text('$i'),
          onTap: () {}, // Handle your onTap here. 
        );
      },
    )
    
        3
  •  14
  •   Evelyn    7 年前

    有一个 example

    onTap 在一个 ListView.builder . 我只是想把这个例子的链接贴出来,以防其他人需要更全面的解释。

    Send data to a new screen - flutter.io

    ...    
    
    final List<Todo> todos;
    
    ...
    
    ListView.builder(
      itemCount: todos.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(todos[index].title),
          onTap: () {
            //Go to the next screen with Navigator.push
          },
        );
      },
    );
    
        4
  •  11
  •   Yuchen    5 年前

    InkWell 在水龙头上有一个很好的涟漪效果 GestureDetector 没有。

    https://api.flutter.dev/flutter/material/InkWell-class.html

    return Scaffold(
        appBar: AppBar(title: Text("Hello World")),
        body: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return InkWell(
                child: Text(index.toString()),
                onTap: () => Scaffold.of(context)
                    .showSnackBar(SnackBar(content: Text(index.toString()))),
              );
            },
            itemCount: 10)
    );
    
        5
  •  6
  •   Steve    7 年前

    您应该在ListView中的项目中使用onPressed方法(或添加一个GestureDetector),然后使用Navigator,类似于下面的代码段,其中AboutScreen是您要转到的下一个页面。

    onPressed: () {
     Navigator.push(
       context,
       MaterialPageRoute(builder: (context) => AboutScreen()),
     );
    }