Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Navigator - Swap Pages From The Page Stack

Tags:

flutter

My situation is a bit tedious. I am pushing 2 pages to navigator

A - B

B is pushed latest.

For some reasons. I dont want to pop B to navigate from B to A. I want to replace / swap them. The reasons are due to states. I want to keep the states of both A and B pages as there are some widgets which are extremely costly to build (Unity view widget). From B how can I use Navigator so that without destroying my pages I can swap B with A?

Here is the flow:

  • A opens as the default page.

  • B is pushed with

    Navigator.of(context).pushNamed();

    Page Stack is A - B

  • From B I want to navigate to A while still keeping the B page alive.

  • -After some magical operations I am missing -

  • Page Stack is B - A. The user sees the page A. None of the pages were destroyed. They were swapped.

Is there a way to do this in Flutter?

like image 573
cs guy Avatar asked Oct 29 '20 20:10

cs guy


1 Answers

You can use PageView for simple way and set config like this:

PageController _pagerController = PageController(keepPage: true);

PageView(
      physics: NeverScrollableScrollPhysics(),
      controller: _pagerController,
      children: [
         Page_A(),
         Page_B(),
      ],
),

And for switching between pages:

goNextPage() {
  _pagerController.animateToPage(
    currentPage + 1,
    duration: Duration(milliseconds: 500),
    curve: Curves.ease,
  );
}

goPrevPage() {
  _pagerController.animateToPage(
    currentPage - 1,
    duration: Duration(milliseconds: 500),
    curve: Curves.ease,
  );
}

I hope it's useful for you.

like image 122
Saeed Fekri Avatar answered Nov 17 '22 13:11

Saeed Fekri