Flutter - Always execute a function when the page appears

后端 未结 5 1113
暗喜
暗喜 2021-01-20 03:56

How could I make the name() function run whenever the Page1 page appeared?

In the code below before going to Page2 I execute t

5条回答
  •  情话喂你
    2021-01-20 04:55

    There is no need to call dispose at all when you are willing to pop and change State later, since dispose will remove the current object from the tree, which does not translate to the logic you are trying to develop.

    You can indeed override the BackButton and pass the same call of Navigator.pop(context, result) to it. Check the following example I have tweaked your code a little bit to show you the difference between each State of your nameScreen field. I hope this helps you.

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State {
      String nameScreen = "";
    
      String name() {
        return 'foo1';
      }
    
      @override
      void initState() {
        super.initState();
        this.nameScreen = "From initState";
    
      }
    
    @override
    void dipose(){
        super.dispose();
    }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Page 1'),
            backgroundColor: Color(0xFF26C6DA),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                  child: const Text('go to Page2'),
                  onPressed: () async {
                    //dispose(); ///No need for dispose
                    String result = await Navigator.of(context).pushNamed('/page2');
    
                      setState((){
                        this.nameScreen = result;
                      });
    
                  },
                ),
                Text(
                  '$nameScreen',
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class Page2 extends StatelessWidget{
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: ()async{
                Navigator.pop(context,"From BackButton");
              }),
              title: const Text('Page 2'),
              backgroundColor: Color(0xFFE57373)
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                RaisedButton(
                    child: const Text('go back to Page1'),
                    onPressed: () {
                      Navigator.pop(context, "From RaisedButton");
                    }
                ),
              ],
            ),
          ),
        );
      }
    

提交回复
热议问题