gpt4 book ai didi

dart - Flutter - 在路由之间推送和获取值(value)

转载 作者:IT老高 更新时间:2023-10-28 12:29:47 26 4
gpt4 key购买 nike

如何将绿色字符串从 HomePage 页面发送到 ContaPage 页面?

我认为是这样 Navigator.of(context).pushNamed('/conta/green'); 但我不知道如何进入页面 conta green 字符串

因此,通过获取字符串的值,我可以例如更改 ContaPageappBar 的 backgroundColor 的颜色。

ma​​in.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),
),
);
}

最佳答案

您可以按需创建 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,
),
);
}

关于dart - Flutter - 在路由之间推送和获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729512/

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