Flutter - Push and Get value between routes

前端 未结 5 619
情话喂你
情话喂你 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 02:47

    It is a bit late, but it might help some one. I think the best way is to use flutter route project fluro.

    define some where globally:

    final router = Router();
    

    after that define the routes

    var contaHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return ContaPage(color: params["color"][0]);
    });
    
    void defineRoutes(Router router) {
      router.define("/conta/:color", handler: contaHandler);
    }
    

    set the onGenerateRoute in MaterialApp to use the router generator:

     new MaterialApp (
       ...
       nGenerateRoute: router.generator
       ...
     ),
    

    you have to add the fluro in the dependencies :

     dependencies:
         fluro: "^1.3.4"
    

    and do the package upgrade by the following or the IDE way.

     flutter packages upgrade
    
    0 讨论(0)
  • 2021-02-04 02:52

    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: <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,
        ),
      );
    }
    
    0 讨论(0)
  • 2021-02-04 02:54

    A better solution is already given on Flutter website, how to use:

    Arguments

    class ScreenArguments {
      final String title;
      final String message;
    
      ScreenArguments(this.title, this.message);
    }
    

    Extract arguments

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

    Register Route

    MaterialApp(
      //...
      routes: {
        ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
        //...
      },     
    );
    

    Navigate

    Navigator.pushNamed(
          context,
          ExtractArgumentsScreen.routeName,
          arguments: ScreenArguments(
            'Extract Arguments Screen',
            'This message is extracted in the build method.',
          ),
        );
    

    Code copied from link.

    0 讨论(0)
  • 2021-02-04 03:00

    Passing data from 1st Page to 2nd

    In 1st page

    // sending "Foo" from 1st page
    Navigator.push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));
    

    In 2nd page.

    class Page2 extends StatelessWidget {
      final String string;
      Page2(this.string); // receiving "Foo" in 2nd
    }
    

    Passing data back from 2nd Page back to 1st

    In 2nd page

    // sending "Bar" from 2nd
    Navigator.pop(context, "Bar");
    

    In 1st page, it is the same which was used earlier but with little modification.

    // receiving "Bar" in 1st
    String received = await Navigator.push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));
    
    0 讨论(0)
  • 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: <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),
        ), 
      );
    }
    

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

    0 讨论(0)
提交回复
热议问题