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

如何将绿色字符串从 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,
    ),
  );
}

https://stackoverflow.com/questions/44729512/

相关文章:

android - 如何在单击推送通知时打开特定屏幕以进行 flutter

flutter - 如何为 flutter 添加自定义颜色?

dart - Flutter:是否有可能创建 App Widgets (Android) 和 Tod

firebase - flutter 和 Firebase : Compression before

flutter - 如何检查滚动位置是在 ListView 的顶部还是底部?

mobile - 是否可以在 Linux 虚拟机上使用 Flutter 开发 iOS 应用程序?

dart - 有没有办法在 InitState 方法上加载异步数据?

dart - 如何在 ListView 内 flutter 中创建一行可滚动的文本框或小部件?

Flutter - 如何更改 TextField 提示颜色?

dart - Flutter 图像预加载