createOverlayEntry function

Function createOverlayEntry ({@required BuildContext context, @required Widget child, Function willPopCallback })

Implementation

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

  //悬浮
  OverlayEntry overlayEntry = OverlayEntry(builder: (context) {
    return DefaultTextStyle(
      style: Theme.of(context).textTheme.body1,
      child: child,
    );
  });

  overlayState.insert(overlayEntry);

  //返回关闭
  Future<bool> backClose() {
    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;
}