1

Right now, there are 3 static screen, and based on user's choice at first start, 5 to 15 dynamic screen. Dynamic means a single stateful widget, which receives a string value, and based on that value collects data from server.

Right now a modal shows all screens. From there user can click on any screen and Navigator.push(context) loads that specific screen. Problem with this method is, say user navigated from A -> B -> C -> D. And when in D, user presses B, then the stack becomes A -> B -> C -> D -> B. And B makes a new network request and collects new updated data.

But what I want is:

  1. When pressing a screen, if screen is not opened, open that screen.
  2. If that screen is already opened, then show that screen with its previous state intact.

So, navigation stack A -> B -> C -> D becomes A -> C -> D -> B, instead of A -> B -> C -> D -> B.

drmirk
  • 403
  • 1
  • 7
  • 14

1 Answers1

0

AutomaticKeepAliveClientMixin can be used to preserve the Widget state:

class Page extends StatefulWidget {
  @override
  PageState createState() {
    return new PageState();
  }
}

class PageState extends State<Page> with AutomaticKeepAliveClientMixin { // using the mixin
  @override
  Widget build(BuildContext context) {
    super.build(context); // we have to call this method
    return Container(
        child: Text('test');
    );
  }

  @override
  bool get wantKeepAlive => true; // we have to set this to true
}
Stefano Amorelli
  • 4,553
  • 3
  • 14
  • 30