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.
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;
}
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.