How to pass data to previous screen in Flutter using Navigator? (need to handle all cases: swipe to go back, press back, etc)

别说谁变了你拦得住时间么 提交于 2021-02-08 05:17:43

问题


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.

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/56636235/how-to-pass-data-to-previous-screen-in-flutter-using-navigator-need-to-handle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!