/*
 * @Author: XianKaiQun
 * @Date: 2020-04-02 11:13:13
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-09-30 16:41:55
 * @Descripttion: 函数式调用loading弹出层
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WLoadingUtil {
  WLoadingUtil._();
  static OverlayEntry? _overlayEntry;

  /// 显示Loading层
  ///
  /// [bg]   是否显示朦胧层
  static show({bool bg = true, Color? color}) {
    if (_overlayEntry != null) {
      return;
    }
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) => Container(
        alignment: Alignment.center,
        color: bg == true ? color ?? Colors.black38 : Colors.transparent,
        child: Container(
          height: 20,
          width: 20,
          alignment: Alignment.center,
          child: SizedBox(
            height: 20,
            width: 20,
            child: CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(
                Theme.of(context).indicatorColor,
              ),
              strokeWidth: 2,
            ),
          ),
        ),
      ),
    );
    WNavUtil.instance!.overlay!.insert(_overlayEntry!);
  }

  /// 带有文字的加载
  static showInfo(Widget infoWidget, {bool bg = true, Color? color}) {
    if (_overlayEntry != null) {
      return;
    }
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) => Container(
          alignment: Alignment.center,
          color: bg == true ? color ?? Colors.black38 : Colors.transparent,
          child: Wisdom.column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              infoWidget,
              SizedBox(width: 5.pt,),
              Container(
                height: 20,
                width: 20,
                alignment: Alignment.center,
                child: SizedBox(
                  height: 20,
                  width: 20,
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation(
                      Theme.of(context).indicatorColor,
                    ),
                    strokeWidth: 2,
                  ),
                ),
              ),
            ],
          )),
    );
    WNavUtil.instance!.overlay!.insert(_overlayEntry!);
  }

  ///隐藏Loading
  ///
  static hide() {
    try {
      _overlayEntry!.remove();
      _overlayEntry = null;
    } catch (e) {}
  }
}
