wrapNotifier<T> static method

ValueNotifier<T> wrapNotifier<T>(
  1. ValueListenable<T> source,
  2. List<ChangeNotifier> disposeBag
)

将源 ValueListenable<T> 包装为独立的 ValueNotifier<T>, 并注册同步监听器,同时将包装后的 notifier 加入 disposeBag

包装后的 notifier 拥有独立的生命周期,面板关闭时通过 disposeBag 统一释放。

Wraps a source ValueListenable<T> into a standalone ValueNotifier<T>, registering a sync listener, and adds it to disposeBag.

The wrapped notifier has its own lifecycle and is disposed via disposeBag.

Implementation

static ValueNotifier<T> wrapNotifier<T>(
  ValueListenable<T> source,
  List<ChangeNotifier> disposeBag,
) {
  final wrapped = ValueNotifier<T>(source.value);

  void listener() {
    wrapped.value = source.value;
  }

  source.addListener(listener);

  // 将 wrapped notifier 加入 disposeBag,
  // 同时在 dispose 时移除对 source 的监听
  disposeBag.add(
    _DisposableNotifier<T>(
      wrapped: wrapped,
      source: source,
      listener: listener,
    ),
  );

  return wrapped;
}