- 在子页面加上混合AutomaticKeepAliveClientMixin这个Mixin
- 重写wantKeepAlive方法,修改返回值为true
- 在home页面使用IndexedStack来控制页面显示
body: IndexedStack(
index: 0,
children: <Widget>[IndexPage(), Buy(), MyPage(), MyPage()]
),
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
bool get wantKeepAlive => true;