如何将HomePage页面中的绿色字符串发送到ContaPage页面?
我认为是这样Navigator.of(context).pushNamed('/conta/green');但我不知道如何进入页面中conta的green字符串
因此,通过获取字符串的值,我可以例如更改appBarin 的backgroundColor的颜色ContaPage.
main.dart
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),
),
);
}
Run Code Online (Sandbox Code Playgroud)
Col*_*son 22
您可以创建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,
),
);
}
Run Code Online (Sandbox Code Playgroud)
Flutter网站上已经给出了更好的解决方案,如何使用:
参数
class ScreenArguments {
final String title;
final String message;
ScreenArguments(this.title, this.message);
}
Run Code Online (Sandbox Code Playgroud)
提取参数
class ExtractArgumentsScreen extends StatelessWidget {
static const routeName = '/extractArguments';
@override
Widget build(BuildContext context) {
final ScreenArguments args = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text(args.title),
),
body: Center(
child: Text(args.message),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
注册路由
MaterialApp(
//...
routes: {
ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
//...
},
);
Run Code Online (Sandbox Code Playgroud)
导航
Navigator.pushNamed(
context,
ExtractArgumentsScreen.routeName,
arguments: ScreenArguments(
'Extract Arguments Screen',
'This message is extracted in the build method.',
),
);
Run Code Online (Sandbox Code Playgroud)
从链接复制的代码。
您总是可以在HomePage中创建一个绿色的静态变量String作为它的值,并在创建新的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(HomePage.color)
},
);
}
}
class HomePage extends StatelessWidget {
static String color = "green";
@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 {
ContaPage({this.color})
String color;
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
backgroundColor: new Color(0xFF26C6DA),
),
);
}
Run Code Online (Sandbox Code Playgroud)
可能有更好的解决方案,但这首先突然出现在我脑海中:)
| 归档时间: |
|
| 查看次数: |
13740 次 |
| 最近记录: |