Flutter-在路线之间推动并获取价值
问题内容:
如何将绿色字符串从主页页面发送到ContaPage页面?
我认为是这样,Navigator.of(context).pushNamed('/conta/green');
但我不知道如何在页面中conta
获取green
字符串
因此,通过获取字符串的值,我可以例如更改appBar
in 的backgroundColor的颜色ContaPage
。
主镖
import "package:flutter/material.dart";
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "MyApp",
home: new HomePage(),
routes: <String, WidgetBuilder> {
'/home': (BuildContext context) => new HomePage(),
'/conta': (BuildContext context) => new ContaPage()
},
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
backgroundColor: new Color(0xFF26C6DA),
),
body: new ListView (
children: <Widget>[
new FlatButton(
child: new Text("ok"),
textColor: new Color(0xFF66BB6A),
onPressed: () {
Navigator.of(context).pushNamed('/conta');
},
),
],
)
);
}
class ContaPage extends StatelessWidget {
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
backgroundColor: new Color(0xFF26C6DA),
),
);
}
问题答案:
您可以创建MaterialPageRoute
按需并将参数传递给ContaPage
构造函数。
import "package:flutter/material.dart";
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: "MyApp",
home: new HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
backgroundColor: new Color(0xFF26C6DA),
),
body: new ListView (
children: <Widget>[
new FlatButton(
child: new Text("ok"),
textColor: new Color(0xFF66BB6A),
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (BuildContext context) => new ContaPage(new Color(0xFF66BB6A)),
));
},
),
],
)
);
}
class ContaPage extends StatelessWidget {
ContaPage(this.color);
final Color color;
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
backgroundColor: color,
),
);
}