Flutter Widgets Binding Observer
Asked Answered
V

1

1

In Flutter,I want to go back to the page I left the app from. But when I try to back, it always navigates to LoginPage. For example,I have 3 page.LoginPage,WorkoutPage,ProgressPage.Login page is my launcher. When I am on ProgressPage, I leave the app. But when I resume,it navigates Login Page.I used this code in login page.

Login Page

  class ProgressTabState extends State with WidgetsBindingObserver{
  AppLifecycleState state;
  @override
  void initState() {
  // TODO: implement initState
  super.initState();

  WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
  // TODO: implement dispose
  super.dispose();
  WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
  // TODO: implement didChangeAppLifecycleState
  super.didChangeAppLifecycleState(state);
  state = appLifecycleState;  
  }

in login page I use this code to navigate to MainTab.

  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (_) {
        return MainTabs();
      },
    ),
  ); 

in Main tabs I have Tabbarviews with two page. Workoutpage and ProgressPage. This is Workout Page.

 class WorkoutTabState extends State <WorkoutTab> {
 @override
 void initState() {
// TODO: implement initState
 super.initState();
 }

 @override
 void dispose() {
  // TODO: implement dispose
   super.dispose();

  }

  @override
  Widget build(BuildContext context)  {

   // TODO: implement build
   return Scaffold(
       body:Text("Workout Page"),
    );
   }

Progress Page

    class ProgressTabState extends State with WidgetsBindingObserver{

    AppLifecycleState state;

    @override
    void initState() {
     // TODO: implement initState
     super.initState();

     WidgetsBinding.instance.addObserver(this);
     }

     @override
     void dispose() {
     // TODO: implement dispose
     super.dispose();
     WidgetsBinding.instance.removeObserver(this);
     }


     @override
     void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
     // TODO: implement didChangeAppLifecycleState
     super.didChangeAppLifecycleState(state);
     state = appLifecycleState;



      }


      @override
      Widget build(BuildContext context) {
      return Scaffold(

      body:Text("Progress Page"));


      }}
Vani answered 22/5, 2020 at 15:27 Comment(3)
Please post your source code or a minimal reproducible example to help diagnose the problem.Pansy
Please also show the code for navigation.Pansy
You don't need all these observers. You just need to store your state (that represents that someone is logged in) somewhere (for example shared_preferences) and then add the correct initialRoute to the MaterialApp when your app starts.Convolution
M
6

In the login page dont use Navigator.push instead use Navigator.of(context).pushNamedAndRemoveUntil

The difference is Navigator.push will put login page as the first page so it will alway fall back to login page, after successful login you dont want that, you need to remove the login page from the routes stack, by using Navigator.of(context).pushNamedAndRemoveUntil, now when navigating between main and progress use Navigator.push That will make the main page as the first route to fallbck to Please read about it heare https://api.flutter.dev/flutter/widgets/NavigatorState/pushAndRemoveUntil.html

Mccarter answered 22/5, 2020 at 18:37 Comment(2)
this helped me with login page. How can I return the same page when I push home button and the program goes background not exit?Vani
if i understood what you want, you are in MainPage you go (use push) to Page1 then from Page1 you wanna go back to MainPage use Navigator.pop(context); in Page1 this will pop the Page1 and send you back to MainPageMccarter

© 2022 - 2024 — McMap. All rights reserved.