theritalin
theritalin

Reputation: 33

Flutter Widgets Binding Observer

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"));


      }}

Upvotes: 1

Views: 28295

Answers (1)

Rida Rezzag
Rida Rezzag

Reputation: 3963

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

Upvotes: 6

Related Questions