Flutter - Push and Get value between routes

前端 未结 5 2004
终归单人心
终归单人心 2021-02-04 02:25

How do I send the green string from the HomePage page to the ContaPage page?

I think it\'s so Navigator.of(context).pushNamed(\'/conta/green\'); but I do no

5条回答
  •  情书的邮戳
    2021-02-04 02:57

    You can create a MaterialPageRoute on demand and pass the argument to the ContaPage constructor.

    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: [
            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,
        ),
      );
    }
    

提交回复
热议问题