toast.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * @Author: XianKaiQun
  3. * @Date: 2020-04-03 10:12:41
  4. * @LastEditors : WuWei
  5. * @LastEditTime : 2023-05-25 15:51:34
  6. * @Descripttion: 编程式调用吐司提示
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'package:wisdom_cli/wisdom_cli.dart';
  10. ///吐司位置枚举
  11. enum WToastLoction { top, center, bottom }
  12. class WToastUtil {
  13. /// 显示吐司提示 ByText
  14. ///
  15. ///[data] 文本
  16. ///
  17. ///[duration] 持续时间
  18. ///
  19. ///[animatedDuration] 动画过渡时间
  20. static Future show(
  21. String data, {
  22. Duration duration = const Duration(milliseconds: 3000),
  23. Duration animatedDuration = const Duration(milliseconds: 100),
  24. WToastLoction location = WToastLoction.center,
  25. }) async {
  26. return await showWidget(
  27. child: Text(data),
  28. duration: duration,
  29. animatedDuration: animatedDuration,
  30. location: location,
  31. );
  32. }
  33. /// 显示吐司提示 ByWidget
  34. ///
  35. ///[data] 文本
  36. ///
  37. ///[duration] 持续时间
  38. ///
  39. ///[animatedDuration] 动画过渡时间
  40. static Future showWidget({
  41. Widget? child,
  42. Duration duration = const Duration(milliseconds: 3000),
  43. Duration animatedDuration = const Duration(milliseconds: 100),
  44. WToastLoction location = WToastLoction.bottom,
  45. }) async {
  46. bool _isShow = false;
  47. OverlayEntry _overlayEntry = OverlayEntry(
  48. builder: (BuildContext context) {
  49. double? top;
  50. double? bottom;
  51. switch (location) {
  52. case WToastLoction.center:
  53. bottom = null;
  54. top = MediaQuery.of(context).size.height / 2;
  55. break;
  56. case WToastLoction.top:
  57. bottom = null;
  58. top = 50;
  59. break;
  60. default:
  61. bottom = 150;
  62. top = null;
  63. }
  64. return Positioned(
  65. top: top,
  66. left: 25,
  67. right: 25,
  68. bottom: bottom,
  69. child: Center(
  70. child: DefaultTextStyle(
  71. style: TextStyle(
  72. color: Colors.white,
  73. fontSize: 14,
  74. ),
  75. textAlign: TextAlign.center,
  76. child: AnimatedOpacity(
  77. opacity: _isShow ? 1 : 0,
  78. duration: animatedDuration,
  79. child: AnimatedPadding(
  80. padding: EdgeInsets.only(
  81. bottom: _isShow ? 5 : 0,
  82. ),
  83. duration: animatedDuration,
  84. child: Container(
  85. padding: EdgeInsets.all(8),
  86. decoration: BoxDecoration(
  87. color: Colors.black54,
  88. borderRadius: BorderRadius.circular(5),
  89. ),
  90. child: child,
  91. ),
  92. ),
  93. ),
  94. ),
  95. ),
  96. );
  97. },
  98. );
  99. WNavUtil.instance!.overlay!.insert(_overlayEntry);
  100. //重新绘制UI, 开始显示
  101. await Future.delayed(animatedDuration);
  102. _isShow = true;
  103. _overlayEntry.markNeedsBuild();
  104. //重新绘制UI, 开始隐藏
  105. await Future.delayed(duration);
  106. _isShow = false;
  107. _overlayEntry.markNeedsBuild();
  108. ///延迟动画执行时间后 删掉
  109. await Future.delayed(animatedDuration);
  110. _overlayEntry.remove();
  111. }
  112. }