在我的项目中,我有页面产品列表,当它来自产品详细信息和任何其他页面时,我想让它保持活动状态,因为我正在使用 AutomaticKeepAliveClientMixin,我不想保持状态, 下面是我的代码:
//This class use for Global variables
class GlobalVars{
static bool keepProductListState = true;
}
//ProductList Class
class ProductListScreen extends StatefulWidget{
ProductListScreen();
@override
ProductListState createState() => ProductListState();
}
//ProductList Class state
class ProductListState extends State<ProductListScreen> with AutomaticKeepAliveClientMixin{
bool shouldKeepAlive = true;
@override
Widget build(BuildContext context) {
return Scaffold();
}
@override
void initState() {
if(!GlobalVars.keepProductListState){
shouldKeepAlive = false;
updateKeepAlive();
}
super.initState();
}
@override
bool get wantKeepAlive => shouldKeepAlive;
}
//i want keep state when open page from this page
class ProductDetail extends StatefulWidget{
ProductDetail();
@override
ProductDetailState createState() => ProductDetailState();
}
class ProductDetailState extends State<ProductDetail>{
@override
Widget build(BuildContext context) {
return Scaffold( );
}
@override
void initState() {
GlobalVars.keepProductListState = true;
super.initState();
}
}
//when product list open from main page i don't want it to be keep its state
class Main extends StatefulWidget{
@override
MainState createState() => MainState();
}
class MainState extends State<Main >{
@override
Widget build(BuildContext context) {
return Scaffold( );
}
@override
void initState() {
GlobalVars.keepProductListState = false;
super.initState();
}
}
但它不起作用 产品列表保持其状态, 我用tabcontroller来切换页面,我不知道我的代码有什么问题