1
final myPage = new MyPage(); 

Navigator.of(context).push(MaterialPageRoute(
                  builder: (BuildContext context) => myPage));

I need myPage not to create new state every time I push it into the MaterialPageRoute.

PiDEV
  • 59
  • 5

2 Answers2

1

It is kind of buggy in master channel, but here you go.

class PersistantTab extends StatefulWidget {
  @override
  _PersistantTabState createState() => _PersistantTabState();
}

class _PersistantTabState extends State<PersistantTab> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return Container();
  }

  // Setting to true will force the tab to never be disposed. This could be dangerous.
  @override
  bool get wantKeepAlive => true;
}
Marc Ma
  • 318
  • 2
  • 12
0

Assigning a key to your widget should be enough.

final myPage = new MyPage(key: GlobalKey());

Note that your MyPage should accept a Key in the constructor in order to pass it to its super. You should have something like this as a constructor:

MyPage({Key key}): super(key:key)

For more information on how Keys are used to preserve Widgets I suggest this Medium article.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • I do assign a global key to a new MyPage, but when I call the pop context like this: Navigator.pop(context) and try to push the same page again like: Navigator.of(context).push(MaterialPageRoute( builder: (BuildContext context) => myPage)); It creates new state =/ – PiDEV Nov 06 '19 at 17:03