import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

import 'loading_dialog.dart';

// loading弹窗
class Loading {
  static OverlayEntry _overlayEntry;

  static Future show(
    BuildContext context, {
    String msg,
  }) async {
    dismiss();
    _overlayEntry = new OverlayEntry(builder: (context) {
      return GestureDetector(
        onTap: dismiss,
        child: Container(
          color: Colors.black.withOpacity(0.3),
          child: LoadingDialog(
            message: msg,
          ),
        ),
      );
    });

    try {
      //插入到 Overlay中显示 OverlayEntry
      Overlay.of(context).insert(_overlayEntry);
    }catch(e, s){
      Logger.error(e, s);
    }
  }

  static dismiss() {
    try {
      _overlayEntry?.remove();
      _overlayEntry = null;
    }catch(e, s){
      Logger.error(e, s);
    }
  }
}
