如何使用导航器将数据传递到Flutter中的上一个屏幕?(需要处理所有情况:滑动即可返回,按回去等) [英] How to pass data to previous screen in Flutter using Navigator? (need to handle all cases: swipe to go back, press back, etc)

查看:41
本文介绍了如何使用导航器将数据传递到Flutter中的上一个屏幕?(需要处理所有情况:滑动即可返回,按回去等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用Navigator.pop的默认方式在屏幕之间传递数据:

Let's say I'm passing data between screens using default way with Navigator.pop:

@override
void selectUserAction(BuildContext context) async {
    final result = await Navigator.of(context).push( 
          MaterialPageRoute(builder: (context) => SelectUserPage()));
    final User selectedUser = result as UserModel;
}

Navigator.pop(context, selectedUser);

因此,在这种情况下,一切都会顺利进行.

So everything goes well in that case.

我知道我可以使用AppBar前导属性来处理后退按钮交互(onPressed):

And I know I can handle back button interaction (onPressed) using AppBar leading property like this:

leading: IconButton(
    icon: BackButtonIcon(),
    onPressed: () {
       Navigator.pop(context, selectedUser);
    },
),

但是,有几种方法可以返回上一屏幕(返回android,滑动返回至iOS等).那我们该怎么办呢?

But there are few options how we can return to the previous screen (back on android, swipe to go back on iOS and etc). Then what should we do to handle this?

无论如何我都需要将数据返回到上一个屏幕.

I need to return data to the previous screen in any case.

推荐答案

将对象传递到下一页时,该对象通过引用传递.也就是说,将指针传递到该对象所在的内存位置.因此,您的两个页面都指向相同的内存区域(相同的对象),对该对象的任何更改都将反映两个页面.这是一个例子.

When you pass an object to a next page, it is passed by reference. That is, a pointer is passed to the position of the memory where that object is. So you have both pages pointing to the same area of memory (the same object) and any changes to that object will reflect both pages. Here's an example.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  _HomeState createState() => _HomeState();
  Model model1 = Model('Page 1');
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("First Page")),
        body: Column(
          children: <Widget>[
            Center(
              child: RaisedButton(
                child: Text("GO"),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => Page2(widget.model1)),
                  );
                },
              ),
            ),
            Text(widget.model1.myInfo)
          ],
        ));
  }
}

class Page2 extends StatefulWidget {
  Model model2;

  _Page2State createState() => _Page2State();

  Page2(this.model2);
}

class _Page2State extends State<Page2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Page 2"),
        ),
        body: Column(
          children: <Widget>[
            TextField(
              onChanged: (text) {
                widget.model2.setNewInfo(text);
              },
              decoration: InputDecoration(
                hintText: 'Enter a new info here',
              ),
            ),
          ],
        ));
  }
}

class Model {
  String myInfo;

  Model(this.myInfo);
  setNewInfo(String info) {
    myInfo = info;
  }
}

这篇关于如何使用导航器将数据传递到Flutter中的上一个屏幕?(需要处理所有情况:滑动即可返回,按回去等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆