如何将绿色字符串从主页页面发送到ContaPage页面?
我认为是这样,Navigator.of(context).pushNamed('/conta/green');但我不知道如何在页面中conta获取green字符串
Navigator.of(context).pushNamed('/conta/green');
conta
green
因此,通过获取字符串的值,我可以例如更改appBarin 的backgroundColor的颜色ContaPage。
appBar
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构造函数。
MaterialPageRoute
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, ), ); }