Flutter Tab切换的时候如何保持页面的状态

发布于 1周前 作者 itying888 最后一次编辑是 5天前 来自 Flutter

Flutter Tab切换的时候要如何保持页面的状态可以通过with AutomaticKeepAliveClientMixin 实现。

import 'package:flutter/material.dart';

class KeepAliveWrapper extends StatefulWidget {
  const KeepAliveWrapper(
      {Key? key, @required this.child, this.keepAlive = true})
      : super(key: key);

  final Widget? child;
  final bool keepAlive;

  @override
  State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
}

class _KeepAliveWrapperState extends State<KeepAliveWrapper>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return widget.child!;
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;  
}
回到顶部