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 navigatorState = Navigator.of(Get.overlayContext!);
  final OverlayState overlayState = navigatorState.overlay!;

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

  T data;

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

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