123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*
- * @Author: XianKaiQun
- * @Date: 2020-04-03 10:12:41
- * @LastEditors : WuWei
- * @LastEditTime : 2023-05-25 15:51:34
- * @Descripttion: 编程式调用吐司提示
- */
- import 'package:flutter/material.dart';
- import 'package:wisdom_cli/wisdom_cli.dart';
- ///吐司位置枚举
- enum WToastLoction { top, center, bottom }
- class WToastUtil {
- /// 显示吐司提示 ByText
- ///
- ///[data] 文本
- ///
- ///[duration] 持续时间
- ///
- ///[animatedDuration] 动画过渡时间
- static Future show(
- String data, {
- Duration duration = const Duration(milliseconds: 3000),
- Duration animatedDuration = const Duration(milliseconds: 100),
- WToastLoction location = WToastLoction.center,
- }) async {
- return await showWidget(
- child: Text(data),
- duration: duration,
- animatedDuration: animatedDuration,
- location: location,
- );
- }
- /// 显示吐司提示 ByWidget
- ///
- ///[data] 文本
- ///
- ///[duration] 持续时间
- ///
- ///[animatedDuration] 动画过渡时间
- static Future showWidget({
- Widget? child,
- Duration duration = const Duration(milliseconds: 3000),
- Duration animatedDuration = const Duration(milliseconds: 100),
- WToastLoction location = WToastLoction.bottom,
- }) async {
- bool _isShow = false;
- OverlayEntry _overlayEntry = OverlayEntry(
- builder: (BuildContext context) {
- double? top;
- double? bottom;
- switch (location) {
- case WToastLoction.center:
- bottom = null;
- top = MediaQuery.of(context).size.height / 2;
- break;
- case WToastLoction.top:
- bottom = null;
- top = 50;
- break;
- default:
- bottom = 150;
- top = null;
- }
- return Positioned(
- top: top,
- left: 25,
- right: 25,
- bottom: bottom,
- child: Center(
- child: DefaultTextStyle(
- style: TextStyle(
- color: Colors.white,
- fontSize: 14,
- ),
- textAlign: TextAlign.center,
- child: AnimatedOpacity(
- opacity: _isShow ? 1 : 0,
- duration: animatedDuration,
- child: AnimatedPadding(
- padding: EdgeInsets.only(
- bottom: _isShow ? 5 : 0,
- ),
- duration: animatedDuration,
- child: Container(
- padding: EdgeInsets.all(8),
- decoration: BoxDecoration(
- color: Colors.black54,
- borderRadius: BorderRadius.circular(5),
- ),
- child: child,
- ),
- ),
- ),
- ),
- ),
- );
- },
- );
- WNavUtil.instance!.overlay!.insert(_overlayEntry);
- //重新绘制UI, 开始显示
- await Future.delayed(animatedDuration);
- _isShow = true;
- _overlayEntry.markNeedsBuild();
- //重新绘制UI, 开始隐藏
- await Future.delayed(duration);
- _isShow = false;
- _overlayEntry.markNeedsBuild();
- ///延迟动画执行时间后 删掉
- await Future.delayed(animatedDuration);
- _overlayEntry.remove();
- }
- }
|