Force Flutter navigator to reload state when popping

前端 未结 14 2179
情书的邮戳
情书的邮戳 2020-11-28 22:54

I have one StatefulWidget in Flutter with button, which navigates me to another StatefulWidget using Navigator.push(). On second widge

14条回答
  •  执念已碎
    2020-11-28 23:25

    This work really good, i got from this doc from flutter page: flutter doc

    I defined the method to control navigation from first page.

    _navigateAndDisplaySelection(BuildContext context) async {
        final result = await Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => AddDirectionPage()),
        );
    
        //below you can get your result and update the view with setState
        //changing the value if you want, i just wanted know if i have to  
        //update, and if is true, reload state
    
        if (result) {
          setState(() {});
        }
      }
    

    So, i call it in a action method from a inkwell, but can be called also from a button:

    onTap: () {
       _navigateAndDisplaySelection(context);
    },
    

    And finally in the second page, to return something (i returned a bool, you can return whatever you want):

    onTap: () {
      Navigator.pop(context, true);
    }
    

提交回复
热议问题