When the switch assembly TabBarView page, each subpage will initState once again, lead to switching every time the page will be redrawn as shown below
If you need to enter the page only once in the first initState first, followed by re-entering the page no longer initState , you need to add the following in the sub-pages
First, in the back of class inheritance plus with AutomaticKeepAliveClientMixin
with AutomaticKeepAliveClientMixin
Was then added in the class
@override bool get wantKeepAlive => true; ///see AutomaticKeepAliveClientMixin
Finally, the added build
super.build(context); /// see AutomaticKeepAliveClientMixin
The complete code is as follows
import 'package:flutter/material.dart'; class Pages extends StatefulWidget{ @override _PagesState createState() => _PagesState(); } class _PagesState extends State<Pages> with AutomaticKeepAliveClientMixin{ @override bool get wantKeepAlive => true; ///see AutomaticKeepAliveClientMixin @override Widget build(BuildContext context) { super.build(context); /// see AutomaticKeepAliveClientMixin // TODO: implement build return Container(); } }
Complete results are as follows, at this time will only execute initState the first time into the page