gpt4 book ai didi

flutter - 在 flutter 通中通过路线作为参数

转载 作者:行者123 更新时间:2023-12-03 03:35:48 28 4
gpt4 key购买 nike

我有点 flutter 朔迷离,我试图在页面上显示两个按钮,并使它们发送到应用程序的不同页面。现在,我为按钮设置了这段代码,但是要使其正常工作,我只是将代码的最后一部分从SearchPage()更改为ScanPage(),这似乎效率很低。我能以某种方式传递页面作为按钮小部件的争论吗?

// Button for scanPage
Widget _buttonScan(String text, Color splashColor, Color highlightColor,
Color fillColor, Color textColor) {
return RaisedButton(
highlightElevation: 0.0,
splashColor: splashColor,
highlightColor: highlightColor,
elevation: 0.0,
color: fillColor,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.bold, color: textColor, fontSize: 20),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScanPage()),
);
},
);
}

// Button for searchPage
Widget _buttonSearch(String text, Color splashColor, Color highlightColor,
Color fillColor, Color textColor) {
return RaisedButton(
highlightElevation: 0.0,
splashColor: splashColor,
highlightColor: highlightColor,
elevation: 0.0,
color: fillColor,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.bold, color: textColor, fontSize: 20),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SearchPage()),
);
},
);
}

最佳答案

您可以将函数作为参数传递给小部件,该小部件将负责导航方向。在您的情况下,请转到scanPage或searchPage,如下所示:

Widget _buttonScan(String text, Color splashColor, Color highlightColor,
Color fillColor, Color textColor, Widget Function() choosePage) {
return RaisedButton(
highlightElevation: 0.0,
splashColor: splashColor,
highlightColor: highlightColor,
elevation: 0.0,
color: fillColor,
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.bold, color: textColor, fontSize: 20),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => choosePage()),
);
},
);
}

当您调用它时:
_buttonScan(...,() => ScanPage ()) //To go to ScanPage
_buttonScan(...,() => SearchPage()) //To go to SearchPage

关于flutter - 在 flutter 通中通过路线作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61253096/

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