showOverlay<T> method

Future<T> showOverlay<T>({
  1. required Future<T> asyncFunction(),
  2. Color opacityColor = Colors.black,
  3. Widget? loadingWidget,
  4. double opacity = .5,
})

Implementation

Future<T> showOverlay<T>({
  required Future<T> Function() asyncFunction,
  Color opacityColor = Colors.black,
  Widget? loadingWidget,
  double opacity = .5,
}) async {
  final navigatorState =
      Navigator.of(Get.overlayContext!, rootNavigator: false);
  final overlayState = navigatorState.overlay!;

  final overlayEntryOpacity = OverlayEntry(builder: (context) {
    return Opacity(
        opacity: opacity,
        child: Container(
          color: opacityColor,
        ));
  });
  final overlayEntryLoader = OverlayEntry(builder: (context) {
    return loadingWidget ??
        const Center(
            child: SizedBox(
          height: 90,
          width: 90,
          child: Text('Loading...'),
        ));
  });
  overlayState.insert(overlayEntryOpacity);
  overlayState.insert(overlayEntryLoader);

  T data;

  try {
    data = await asyncFunction();
  } on Exception catch (_) {
    overlayEntryLoader.remove();
    overlayEntryOpacity.remove();
    rethrow;
  }

  overlayEntryLoader.remove();
  overlayEntryOpacity.remove();
  return data;
}