Flutter - 使用pushReplacementNamed在路由之间传递值

时间:2018-04-29 16:15:31

标签: dart flutter

我正在尝试使用Navigator.pushReplacementNamed()在路由之间传递值,但我没有得到它。

我无法弄清楚哪些数据将退出pushReplacementNamed并获取routes密钥的正确语法和逻辑。

你能帮助我吗?

输入以下代码以便于理解:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
      routes: <String, WidgetBuilder> {
        '/form': (BuildContext context) => new FormPage(email: ???, header: {'auth': ???}),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          Navigator.pushReplacementNamed(context, '/form', {email: 'myemail@flutter.com', header: {'auth': '1234'}});
        },
        child: new Icon(Icons.navigate_next),
      ),
    );
  }
}

class FormPage extends StatefulWidget {
  FormPage({Key key, this.email, this.header}) : super(key: key);
  String email;
  Map header;
  @override
  FormPageState createState() => new FormPageState();
}

class FormPageState extends State<FormPage> {
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Column(
        children: <Widget>[
          new Text(widget.email),
          new Text(widget.header['auth'])
        ],
      ),
    );
  }
}

更正后:

尝试使用pushReplacementNamed的目的是删除路径历史记录,因此如果用户按下设备的后退按钮以返回到之前的路径,则路径将不再存在。 Reference

此实现的原因是在登录页面上使用,如果用户已经通过google sign进行了身份验证,则会将其重定向到下一页传递登录参数,并且无法通过设备返回登录页面后退按钮。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
      routes: <String, WidgetBuilder> {
        '/form': (BuildContext context) => new FormPage(), //new
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          Navigator.of(context).pushReplacement(                                                         //new
            new MaterialPageRoute(                                                                       //new
              settings: const RouteSettings(name: '/form'),                                              //new
              builder: (context) => new FormPage(email: 'myemail@flutter.com', header: {'auth': '1234'}) //new
            )                                                                                            //new
          );                                                                                             //new
        },
        child: new Icon(Icons.navigate_next),
      ),
    );
  }
}

class FormPage extends StatefulWidget {
  FormPage({Key key, this.email, this.header}) : super(key: key);
  String email;
  Map header;
  @override
  FormPageState createState() => new FormPageState();
}

class FormPageState extends State<FormPage> {
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new Column(
        children: <Widget>[
          new Text(widget.email),
          new Text(widget.header['auth'])
        ],
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:3)

我想您可能会错过了解结果如何运作。结果返回上一个推送最后一个路径的小部件,并使用await获取前一个Future调用发回的push的结果。你只是想开始一个带参数的路线吗?然后你就可以做到这一点。

Navigator.of(context).pushReplacement(new MaterialPageRoute(settings: const RouteSettings(name: '/form'), builder: (context) => new FormPage(email: 'myemail@flutter.com', header: {'auth': '1234'})));

答案 1 :(得分:2)

  1. 您可以使用Navigator.of(context).pushReplacementNamed('/route', arguments: 'Test String');传递任何对象作为第一个窗口小部件的参数。
  2. 然后使用String s = ModalRoute.of(context).settings.arguments;来解析initState()方法之后的参数,例如第二个小部件中的didChangeDependencies()build(BuildContext context)