gpt4 book ai didi

listview - 如何通过 Flutter 中的 RaisedButton 传递/绑定(bind)值?

转载 作者:IT老高 更新时间:2023-10-28 12:36:41 29 4
gpt4 key购买 nike

我有一个 RaisedButtons 的 ListView,其中每个 RaisedButton 看起来像这样。每个 RaisedButton 的 name 变量都不同:

  new RaisedButton(
onPressed: _navigateToRoute,
child: new Text(name),
),

点击 RaisedButton 调用 _navigateToRoute():

  void _navigateToRoute() {
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new Scaffold(
body: new Text('A value with the word "Hello" should go here'),
);
},
));
}

当点击特定的 RaisedButton(例如,假设我们点击 ListView 中的第一个 RaisedButton,其中 name = 'Hello'),我想传递 name 新路线的变量。我怎么做?有没有办法将 name 存储在 context 变量中?我应该使用另一个小部件而不是 RaisedButton 吗?

我可以使用 Named Navigator Route但我不想为 ListView 中的每个项目硬编码路线。

我找到了 this Github issue ,我不确定这是否与我遇到的相同。

最佳答案

您可以将名称作为输入传递给 onPressed 函数。 IE。

  new RaisedButton(
onPressed: () => _navigateToRoute(name),
child: new Text(name),
),

函数签名将是:

  void _navigateToRoute(String name)

关于listview - 如何通过 Flutter 中的 RaisedButton 传递/绑定(bind)值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46535249/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com