import 'package:flutter/material.dart';

import '../widgets/Loader/gif_loader_container.dart';

class GifLoaderDialogUtils {
  static bool _isLoadingVisible = false;

  static bool get isLoading => _isLoadingVisible;

  static showMyDialog(BuildContext context, {bool barrierDismissible = true}) {
    // showDialog(context: context, builder: (cxt) => GifLoaderContainer());
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _isLoadingVisible = true;
      showDialog(
        barrierDismissible: barrierDismissible,
        context: context,
        barrierColor: Colors.black.withOpacity(0.5),
        useRootNavigator: false,
        builder: (BuildContext context) => GifLoaderContainer(
          barrierDismissible: barrierDismissible,
        ),
      ).then((value) {
        _isLoadingVisible = false;
      });
    });
  }

  static hideDialog(BuildContext context) {
    try {

      if (_isLoadingVisible) {
        _isLoadingVisible = false;
        Navigator.of(context).pop();
      }
      _isLoadingVisible = false;
      // Navigator.of(context, rootNavigator: true).canPop();
    } catch (error) {
      //Future.delayed(Duration(milliseconds: 250)).then((value) => Navigator.of(context, rootNavigator: true).canPop());
      // Future.delayed(Duration(milliseconds: 250)).then((value) => Navigator.of(context, rootNavigator: true).canPop());
      if (_isLoadingVisible) {
        _isLoadingVisible = false;
        Navigator.of(context).pop();
      }
      _isLoadingVisible = false;
    }
  }
}