useKeepAliveClient function

AutomaticKeepAliveClientMixin<StatefulWidget> useKeepAliveClient({
  1. bool wantKeepAlive = true,
})

A hook that provides an AutomaticKeepAliveClientMixin for keeping a widget alive even when it is not visible.

This is useful for preserving the state of a widget when it is removed from the widget tree and later reinserted.

Example:

final keepAliveClient = useKeepAliveClient();

Implementation

AutomaticKeepAliveClientMixin useKeepAliveClient(
    {
    /// A boolean value indicating whether the widget should be kept alive.
    /// If true, the widget will maintain its state when it is not visible.
    /// Default is true.
    bool wantKeepAlive = true}) {
  final context = useContext();
  final keepAlive = _KeepAliveClient(context, wantKeepAlive);

  keepAlive.initState();
  onDeactivated(keepAlive.deactivate);
  onBeforeBuild(keepAlive.build);

  return keepAlive;
}