flutter保持页面状态
在子页面加上混合AutomaticKeepAliveClientMixin这个Mixin重写wantKeepAlive方法,修改返回值为true在home页面使用IndexedStack来控制页面显示// home.dartbody: IndexedStack(index: 0, //当前的下标children: <Widget>[IndexPage(), Buy(), MyPage(
·
- 在子页面加上混合AutomaticKeepAliveClientMixin这个Mixin
- 重写wantKeepAlive方法,修改返回值为true
- 在home页面使用IndexedStack来控制页面显示
// home.dart
body: IndexedStack(
index: 0, //当前的下标
children: <Widget>[IndexPage(), Buy(), MyPage(), MyPage()]//子页面的Widget
),
// buy.dart
import 'package:flutter/material.dart';
class Buy extends StatefulWidget {
Buy({Key key}) : super(key: key);
@override
_BuyState createState() => _BuyState();
}
class _BuyState extends State<Buy> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text("Buy页面"),
);
}
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
更多推荐



所有评论(0)