flutter Toast 도구 클래스

7142 단어 flutter
프로젝트에 이러한 요구가 있습니다. 설정 페이지에 캐시 청소 기능이 있습니다. 캐시가 있을 때 캐시 청소를 표시하고 청소가 끝난 후에 캐시 청소를 표시하며 코드에 바로 올라갑니다.
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:funya/util/size_util.dart';

const Color _bgColor = Color.fromRGBO(51, 51, 51, 0.8);
const Color _contentColor = Color.fromRGBO(255, 255, 255, 1);
const double _textFontSize = 14.0;
const double _radius = 12.0;
const double _imgWH = 30.0;
const int _time = 1;
enum _Orientation { horizontal, vertical }

class ToastUtil {
  static Future showText(
    BuildContext context, {
    @required String msg,
    int closeTime = _time,
  }) {
    return _showToast(
        context: context, msg: msg, stopEvent: true, closeTime: closeTime);
  }

  static Future showSuccess(
    BuildContext context, {
    @required String msg,
    int closeTime = _time,
  }) {
    Widget img = Image.asset("assets/toast_sccuess.png", width: _imgWH);
    return _showToast(
        context: context,
        msg: msg,
        image: img,
        stopEvent: true,
        closeTime: closeTime);
  }

  static _HideCallback showLoadingText_iOS(
    BuildContext context, {
    String msg = "   ...",
  }) {
    Widget img = Image.asset("assets/loading.gif", width: _imgWH);
    return _showJhToast(
        context: context,
        msg: msg,
        image: img,
        isLoading: false,
        stopEvent: true);
  }
}

Future _showToast(
    {@required BuildContext context,
    String msg,
    stopEvent = false,
    Widget image,
    int closeTime,
    _Orientation orientation = _Orientation.vertical}) {
  msg = msg;
  var hide = _showJhToast(
      context: context,
      msg: msg,
      isLoading: false,
      stopEvent: stopEvent,
      image: image,
      orientation: orientation);
  return Future.delayed(Duration(seconds: closeTime), () {
    hide();
  });
}

typedef _HideCallback = Future Function();

class JhToastWidget extends StatelessWidget {
  const JhToastWidget({
    Key key,
    @required this.msg,
    this.image,
    @required this.isLoading,
    @required this.stopEvent,
    @required this.orientation,
  }) : super(key: key);

  final bool stopEvent;
  final Widget image;
  final String msg;
  final bool isLoading;
  final _Orientation orientation;

  @override
  Widget build(BuildContext context) {
    SizeUtil.init(context, width: 375, height: 812, allowFontScaling: false);
    Widget topW;
    bool isHidden;
    if (this.isLoading == true) {
      isHidden = false;
      topW = CircularProgressIndicator(
        strokeWidth: 3.0,
        valueColor: AlwaysStoppedAnimation(_contentColor),
      );
    } else {
      isHidden = image == null ? true : false;
      topW = image;
    }

    var widget = Material(
//        color: Colors.yellow,
        color: Colors.transparent,
        child: Align(
//            alignment: Alignment.center,
            alignment: Alignment(0.0, -0.2), //      
            child: Container(
              margin: const EdgeInsets.all(50.0),
              padding:
                  EdgeInsets.symmetric(horizontal: 24.0.w, vertical: 16.0.w),
              decoration: BoxDecoration(
                color: _bgColor,
                borderRadius: BorderRadius.circular(_radius),
              ),
              child: ClipRect(
                child: orientation == _Orientation.vertical
                    ? Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Offstage(
                            offstage: isHidden,
                            child: Container(
                              width: 40.0.w,
                              height: 40.0.w,
                              margin: EdgeInsets.only(bottom: 8.0.w),
                              padding: EdgeInsets.all(4.0),
                              child: topW,
                            ),
                          ),
                          Text(msg,
                              style: TextStyle(
                                  fontSize: _textFontSize,
                                  color: _contentColor),
                              textAlign: TextAlign.center),
                        ],
                      )
                    : Row(
                        mainAxisSize: MainAxisSize.min,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Offstage(
                            offstage: isHidden,
                            child: Container(
                              width: 36.0.w,
                              height: 36.0.w,
                              margin: EdgeInsets.only(right: 8.0.w),
                              padding: EdgeInsets.all(4.0),
                              child: topW,
                            ),
                          ),
                          Text(msg,
                              style: TextStyle(
                                  fontSize: _textFontSize,
                                  color: _contentColor),
                              textAlign: TextAlign.center),
                        ],
                      ),
              ),
            )));
    return IgnorePointer(
      ignoring: !stopEvent,
      child: widget,
    );
  }
}

int backButtonIndex = 2;

_HideCallback _showJhToast({
  @required BuildContext context,
  @required String msg,
  Widget image,
  @required bool isLoading,
  bool stopEvent = false,
  _Orientation orientation = _Orientation.vertical,
}) {
  Completer result = Completer();

  var backButtonName = 'funya$backButtonIndex';
  BackButtonInterceptor.add((stopDefaultButtonEvent) {
    result.future.then((hide) {
      hide();
    });
    return true;
  }, zIndex: backButtonIndex, name: backButtonName);
  backButtonIndex++;

  var overlay = OverlayEntry(
      maintainState: true,
      builder: (_) => WillPopScope(
            onWillPop: () async {
              var hide = await result.future;
              hide();
              return false;
            },
            child: JhToastWidget(
              image: image,
              msg: msg,
              stopEvent: stopEvent,
              isLoading: isLoading,
              orientation: orientation,
            ),
          ));
  result.complete(() {
    if (overlay == null) {
      return;
    }
    overlay.remove();
    overlay = null;
    BackButtonInterceptor.removeByName(backButtonName);
  });
  Overlay.of(context).insert(overlay);
  return () async {
    var hide = await result.future;
    hide();
  };
}

사용 방법:
 var hide = ToastUtil.showLoadingText_iOS(context, msg: "     ...");
        Future.delayed(Duration(seconds: 2), () {
          ToastUtil.showSuccess(context, msg: '    ');
          hide();
        });

좋은 웹페이지 즐겨찾기