简体   繁体   中英

How do I make user continue from last route in flutter

Please I need help regarding how to approach this issue in my flutter app.

I have a parent page in which there is a button that when clicked leads to page1, page1 then leads to page2, page2 leads to page3.

What I want to achieve is that, I will like the user to continue from where they stop whenever the button in the main page is clicked.

to explain better, let's say a user clicks the button in the parent page, the button will lead to page1, then user can move to page2 with a button in page1. If user then leaves the app while on page 2, I would like that the user continues at page2 instead of page1 when the button in the parent page is clicked.

I hope someone can help me to solve this.

Thank you

Currently I have been trying to use flutter provider or sharedpreferences but could not get how to solve it.

For this, you have to store information in Share Preference. So that when the user reopens the app we can get the last information we saved.

You can use shared_preferences or get_storage package to store the progress

and when the user clicks on the navigation button on "parent page" simply get the last progress from share preferences and navigate the user accordingly

Here is the sample code for your better understanding:

enum Progress { pageOne, pageTwo, pageThree }

Progress? getProgress() {
  //TODO: will get progress from Shared Preference and return
}

void saveProgress(Progress progress) {
  //TODO:will save progress in Shared preference
}

class ParentPage extends StatelessWidget {
  const ParentPage({super.key});

  void _performNavigation(BuildContext context) {
    switch (getProgress()) {
      case null:
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => const PageOne()));
        break;
      case Progress.pageOne:
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => const PageTwo()));
        break;
      case Progress.pageTwo:
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => const PageThree()));
        break;
      case Progress.pageThree:
        // TODO: Show that process is already completed
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Parent Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => _performNavigation(context),
          child: const Text('Continue'),
        ),
      ),
    );
  }
}

class PageOne extends StatelessWidget {
  const PageOne({super.key});

  void _performNavigation(BuildContext context) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => const PageTwo()));
  }

  @override
  Widget build(BuildContext context) {
    saveProgress(Progress.pageOne);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page 1'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => _performNavigation(context),
          child: const Text('Continue'),
        ),
      ),
    );
  }
}

class PageTwo extends StatelessWidget {
  const PageTwo({super.key});

  void _performNavigation(BuildContext context) {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => const PageThree()));
  }

  @override
  Widget build(BuildContext context) {
    saveProgress(Progress.pageTwo);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page 2'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => _performNavigation(context),
          child: const Text('Continue'),
        ),
      ),
    );
  }
}

class PageThree extends StatelessWidget {
  const PageThree({super.key});

  @override
  Widget build(BuildContext context) {
    saveProgress(Progress.pageThree);
    return Scaffold(
      appBar: AppBar(
        title: const Text('Page Three'),
      ),
      body: const Center(
        child: Text('Thanks'),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM