作者:cl云中皓 | 来源:互联网 | 2023-01-15 18:12
我有一个RaisedButtons的ListView,其中每个RaisedButton都是这样的.name
每个RaisedButton 的变量都不同:
new RaisedButton(
onPressed: _navigateToRoute,
child: new Text(name),
),
点击RaisedButton调用_navigateToRoute():
void _navigateToRoute() {
Navigator.of(context).push(new MaterialPageRoute(
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吗?
我可以使用命名导航器路由,但我不想为ListView中的每个项目硬编码路由.
我发现了这个Github问题,我不确定这是否与我遇到的情况相同.
1> Mary..:
您可以将名称作为输入传递给onPressed函数.即
new RaisedButton(
onPressed: () => _navigateToRoute(name),
child: new Text(name),
),
函数签名将是:
void _navigateToRoute(String name)
我想强调`()=>`部分至关重要。否则,您的方法将在构建期间立即调用。