Flutter - Push and Get value between routes

前端 未结 5 613
情话喂你
情话喂你 2021-02-04 02:19

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 03:02

    You could always make a static variable String with green as it's value in your HomePage and use that value in your routes when you are creating a new ContaPage. Something like this:

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

    There is probably better solutions but this popped into my head first :)

提交回复
热议问题