Flutter-tabview切换避免重复使用

1.使用AutomaticKeepAliveClientMixin,根据文档说明
/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. Used
/// with [State] subclasses.
///
/// Subclasses must implement [wantKeepAlive], and their [build] methods must
/// call super.build (the return value will always return null, and should be
/// ignored).
使用条件
1.必须是 Statefull 的widget

  class _HotState extends State<Hot> with AutomaticKeepAliveClientMixin {}

2.重写 wantKeepAlive

@override
bool get wantKeepAlive => true;

3.在build方法中,在返回你的内容之前必须调用super.build(context);

Widget build(BuildContext context) {
super.build(context);
return Scaffold(...);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容