1、自定义KeepAliveWidget
class KeepAliveWidget extends StatefulWidget {
final Widget child;
const KeepAliveWidget(this.child);
@override
State<StatefulWidget> createState() => KeepAliveState();
}
class KeepAliveState extends State<KeepAliveWidget>
with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
return widget.child;
}
}
Widget keepAliveWrapper(Widget child) {
return KeepAliveWidget(child);
}
2、page或者component目录中新建wrapper,返回自定义的KeepAliveWidget
import 'package:flutter/material.dart';
import 'package:lib_flutter/component/widget/keep_alive_widget.dart';
Widget xxxPageWrapper(Widget widget) {
return KeepAliveWidget(widget);
}
3、在page中使用wrapper
import 'package:fish_redux/fish_redux.dart';
import 'effect.dart';
import 'reducer.dart';
import 'state.dart';
import 'view.dart';
import 'wrapper.dart';
class xxxPage extends Page<xxxState, Map<String, dynamic>> {
TownLinePage()
: super(
wrapper: xxxxPageWrapper,
initState: initState,
effect: buildEffect(),
reducer: buildReducer(),
view: buildView,
dependencies: Dependencies<xxxState>(
adapter: null, slots: <String, Dependent<xxxState>>{}),
middleware: <Middleware<xxxeState>>[],
);
}
4、没了