PopScopeCompat function

Widget PopScopeCompat({
  1. required Widget child,
  2. bool canPop = true,
  3. void onPop(
    1. bool didPop,
    2. Object? result
    )?,
})

一个高性能、版本兼容的 PopScope 工具。

  • Flutter 3.22+ 使用 onPopInvokedWithResult
  • Flutter 3.11–3.21 使用 onPopInvoked

✅ 特点:

  • 只在首次加载时检测一次 API 支持情况
  • 后续构建不再使用 Function.apply
  • 性能几乎与原生 PopScope 相同

Implementation

// ignore: non_constant_identifier_names
Widget PopScopeCompat({
  required Widget child,
  bool canPop = true,
  void Function(bool didPop, Object? result)? onPop,
}) {
  return _PopScopeCompat.instance.build(child, canPop, onPop);
}