createOverlayEntry function

dynamic Function() createOverlayEntry({
  1. required BuildContext context,
  2. required Widget child,
  3. bool backIntercept = false,
  4. Function? willPopCallback,
})

Implementation

Function() createOverlayEntry(
    {required BuildContext context,
    required Widget child,
    bool backIntercept = false,
    Function? willPopCallback}) {
  final overlayState = Overlay.of(context);
  ModalRoute? _route;

  OverlayEntry overlayEntry = new OverlayEntry(builder: (context) {
    return DefaultTextStyle(
        style: Theme.of(context).textTheme.bodyText1!, child: child);
  });
  overlayState!.insert(overlayEntry);

  // 返回关闭
  Future<bool> backClose() {
    if (willPopCallback is Function) willPopCallback();
    return Future.value(false);
  }

  void close() {
    overlayEntry.remove();
    _route?.removeScopedWillPopCallback(backClose);
  }

  // 返回键拦截
  if (willPopCallback != null) {
    _route = ModalRoute.of(context);

    // back监听
    _route!.addScopedWillPopCallback(backClose);
  }

  return close;
}