buildLoadingOverlayWidget method

Widget buildLoadingOverlayWidget({
  1. required Widget child,
  2. String? loadingKey = LoadingKey.global,
  3. Widget? loadingWidget,
  4. Duration timeout = const Duration(seconds: 30),
})

Builds the loading overlay widget using BlocBuilder.

This is a helper method that can be called from buildLoadingOverlay implementations. It handles the BlocBuilder logic and timeout management.

Parameters:

  • child: The widget to wrap with the loading overlay
  • loadingKey: Optional key to manage multiple loading states (defaults to global)
  • loadingWidget: Optional custom loading indicator widget
  • timeout: Optional duration after which loading will automatically hide (defaults to 30 seconds)

Note: The context for mounted checks comes from the BlocBuilder's builder callback.

Implementation

Widget buildLoadingOverlayWidget({
  required Widget child,
  String? loadingKey = LoadingKey.global,
  Widget? loadingWidget,
  Duration timeout = const Duration(seconds: 30),
}) {
  return BlocBuilder<CommonBloc, CommonState>(
    buildWhen: (previous, current) =>
        previous.isLoading(key: loadingKey) !=
        current.isLoading(key: loadingKey),
    builder: (context, state) {
      if (state.isLoading(key: loadingKey)) {
        Future.delayed(timeout, () {
          if (context.mounted && state.isLoading(key: loadingKey)) {
            hideLoading(key: loadingKey);
          }
        });
      }
      return Stack(
        children: [
          child,
          if (state.isLoading(key: loadingKey))
            AnimatedOpacity(
              opacity: state.isLoading(key: loadingKey) ? 1.0 : 0.0,
              duration: const Duration(milliseconds: 300),
              curve: Curves.easeInOut,
              child: loadingWidget ?? buildPageLoading(),
            ),
        ],
      );
    },
  );
}