31 lines
666 B
Dart
31 lines
666 B
Dart
// Author: fengshengxiong
|
|
// Date: 2024/5/9
|
|
// Description: 保持组件状态
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
class KeepAliveWrapper extends StatefulWidget {
|
|
const KeepAliveWrapper({
|
|
super.key,
|
|
required this.child,
|
|
this.keepAlive = true,
|
|
});
|
|
|
|
final Widget child;
|
|
final bool keepAlive;
|
|
|
|
@override
|
|
State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
|
|
}
|
|
|
|
class _KeepAliveWrapperState extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
super.build(context);
|
|
return widget.child;
|
|
}
|
|
|
|
@override
|
|
bool get wantKeepAlive => widget.keepAlive;
|
|
}
|