showToast static method
Future<bool>
showToast(
- BuildContext context, {
- required String msg,
- int showTime = 1000,
- Color bgColor = Colors.black,
- Color textColor = Colors.white,
- double textSize = 14.0,
- ToastPostion position = ToastPostion.bottom,
- double pdHorizontal = 20.0,
- double pdVertical = 10.0,
Implementation
static Future<bool> showToast(
BuildContext context, {
//显示的文本
required String msg,
//显示的时间 单位毫秒
int showTime = 1000,
//显示的背景
Color bgColor = Colors.black,
//显示的文本颜色
Color textColor = Colors.white,
//显示的文字大小
double textSize = 14.0,
//显示的位置
ToastPostion position = ToastPostion.bottom,
//文字水平方向的内边距
double pdHorizontal = 20.0,
//文字垂直方向的内边距
double pdVertical = 10.0,
}) async {
_msg = msg;
_startedTime = DateTime.now();
_showTime = showTime;
_bgColor = bgColor;
_textColor = textColor;
_textSize = textSize;
_toastPosition = position;
_pdHorizontal = pdHorizontal;
_pdVertical = pdVertical;
// 获取OverlayState
OverlayState overlayState = Overlay.of(context);
_showing = true;
if (_overlayEntry == null) {
// OverlayEntry负责构建布局
// 通过OverlayEntry将构建的布局插入到整个布局的最上层
_overlayEntry = OverlayEntry(
builder: (BuildContext context) => Positioned(
// top值,可以改变这个值来改变toast在屏幕中的位置
top: buildToastPosition(context),
child: Semantics(
label: 'jhdebug_JhToast',
child: Container(
alignment: Alignment.center,
width: MediaQuery.of(context).size.width,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 40.0),
child: AnimatedOpacity(
opacity: _showing ? 0.7 : 0.0, // 目标透明度
duration: _showing
? Duration(milliseconds: 100)
: Duration(milliseconds: 400),
child: _buildToastWidget(),
),
),
),
),
),
);
// 插入到整个布局的最上层
overlayState.insert(_overlayEntry!);
} else {
_overlayEntry?.markNeedsBuild();
}
/// 等待时间
await Future.delayed(Duration(milliseconds: _showTime!));
// 2秒后消失
if (DateTime.now().difference(_startedTime).inMilliseconds >= _showTime!) {
_showing = false;
_overlayEntry?.markNeedsBuild();
await Future.delayed(Duration(milliseconds: 400));
_overlayEntry?.remove();
_overlayEntry = null;
}
return true;
}