Trigger FirstPage's setState method after popp

2019-09-03 02:05发布

问题:

I have two pages in myapp namely FirstPage and SecondPage.

Basically, FirstPage widget displays a ListView with a list of items while SecondPage widget is where I can add/delete items to the list.

I can navigate to the SecondPage by:

Navigator.of(context).pushNamed("/SecondPage");

And also I can go back to the FirstPage by using:

Navigator.of(context).pop();

My problem is I can't figure out how am I going to trigger the setState method of FirstPage widget after popping the SecondPage so that the FirstPage's ListView is updated.

I would appreciate any hint.

回答1:

Whenever we push, we will get a future, we can use that future to trigger the setState

Future pushNamed = Navigator.of(context).pushNamed("/SecondPage");
pushNamed.then((_)=> setState(() {}));

Refer here to send data from secondScreen to firstScreen.



回答2:

Use this code to send values from child View to your parent view

 Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    title: new Text(widget.title),
  ),
  body: new Center(
      child: new FlatButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => SecondView(callBackValue)),
            );
          },
          child: new Text('Open Child Widget'))),
);
}

 //Your Callback Values will be received here
void callBackValue(double value) {
print('Value $value');
}

// Your SecondPage
class SecondView extends StatefulWidget{
    final void Function(double data) callback;
    SecondView(this.callback);

    @override
    _SecondView createState() => new _SecondView();
  }

  class _SecondView extends State<SecondView>{
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('Child Widget'),
        ),
        body: new Center(
          child: new FlatButton(onPressed: (){
            widget.callback(10.0);
          }, child: new Text('Send Value to parent')),
        ),
      );
    }
  }


回答3:

what you trying to achieve is similar to the startActivityForResult() method in Android.

This can be achieved in flutter something like this :-

in your first activity

  Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());

// now it will wait for the secondView to return some data
//and the below code will be executed whenever the second view pops.
  setState((){});

Hope this was helpful.

And dont forget to write async in your method declaration for the navigation.

  void _navigateToSecondActivity async {
       Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());
       setState((){});
  }

Just call this function when you need to navigate to the second activity.

or on your onTap()

onTap: () async{
  Map map = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondView());
       setState((){});
}


标签: dart flutter