在项目中有这么个需求,在设置页中会有清理缓存功能,有缓存时显示清理缓存,清理完成后显示清理完成,直接上代码
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();
});