toast.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. /// toast
  4. class Toast {
  5. static const int LENGTH_SHORT = 1;
  6. static const int LENGTH_LONG = 2;
  7. static const int BOTTOM = 0;
  8. static const int CENTER = 1;
  9. static const int TOP = 2;
  10. static const int ERROR = -1;
  11. static const int NORMAL = 0;
  12. static const int SUCCESS = 1;
  13. static void show(String msg, BuildContext context,
  14. {int type = NORMAL,
  15. int duration = 1,
  16. int gravity = CENTER,
  17. Color backgroundColor = const Color(0xAA000000),
  18. Color textColor = Colors.white,
  19. double backgroundRadius = 5.0}) {
  20. ToastView.dismiss();
  21. ToastView.createView(msg, context, type, duration, gravity, backgroundColor,
  22. textColor, backgroundRadius);
  23. }
  24. }
  25. class ToastView {
  26. static final ToastView _singleton = new ToastView._internal();
  27. factory ToastView() {
  28. return _singleton;
  29. }
  30. ToastView._internal();
  31. static OverlayState overlayState;
  32. static OverlayEntry overlayEntry;
  33. static bool _isVisible = false;
  34. static void createView(
  35. String msg,
  36. BuildContext context,
  37. int type,
  38. int duration,
  39. int gravity,
  40. Color background,
  41. Color textColor,
  42. double backgroundRadius) async {
  43. overlayState = Overlay.of(context);
  44. overlayEntry = new OverlayEntry(
  45. builder: (BuildContext context) => ToastWidget(
  46. widget: Container(
  47. width: MediaQuery.of(context).size.width,
  48. child: Container(
  49. alignment: Alignment.center,
  50. child: Container(
  51. margin: EdgeInsets.symmetric(horizontal: 83),
  52. padding: EdgeInsets.all(15.0),
  53. decoration: BoxDecoration(
  54. color: background,
  55. borderRadius: BorderRadius.circular(backgroundRadius),
  56. ),
  57. constraints: BoxConstraints(minHeight: 52,minWidth: 210),
  58. child: _buildContent(type, msg, textColor),
  59. ),
  60. ),
  61. ),
  62. gravity: gravity),
  63. );
  64. _isVisible = true;
  65. overlayState.insert(overlayEntry);
  66. await new Future.delayed(
  67. Duration(seconds: duration == null ? Toast.LENGTH_SHORT : duration));
  68. dismiss();
  69. }
  70. static Widget _buildContent(int type, String msg, Color textColor) {
  71. if (type == 0) {
  72. return Text(msg,
  73. maxLines: 20,
  74. overflow: TextOverflow.ellipsis,
  75. textAlign: TextAlign.center,
  76. style: TextStyle(
  77. fontSize: 16,
  78. color: textColor,
  79. decoration: TextDecoration.none,
  80. fontWeight: FontWeight.normal));
  81. } else {
  82. return Column(
  83. mainAxisAlignment: MainAxisAlignment.center,
  84. mainAxisSize: MainAxisSize.min,
  85. children: <Widget>[
  86. Icon(type ==1 ?Icons.check_circle:Icons.error,color: Colors.white,),
  87. Padding(padding: EdgeInsets.only(top: 16.0),),
  88. Text(msg,
  89. maxLines: 20,
  90. overflow: TextOverflow.ellipsis,
  91. style: TextStyle(
  92. fontSize: 16,
  93. color: textColor,
  94. decoration: TextDecoration.none,
  95. fontWeight: FontWeight.normal))
  96. ],
  97. );
  98. }
  99. }
  100. static dismiss() async {
  101. if (!_isVisible) {
  102. return;
  103. }
  104. _isVisible = false;
  105. overlayEntry?.remove();
  106. }
  107. }
  108. class ToastWidget extends StatelessWidget {
  109. ToastWidget({
  110. Key key,
  111. @required this.widget,
  112. @required this.gravity,
  113. }) : super(key: key);
  114. final Widget widget;
  115. final int gravity;
  116. @override
  117. Widget build(BuildContext context) {
  118. return new Positioned(
  119. top: gravity == 2 ? 50 : null,
  120. bottom: gravity == 0 ? 50 : null,
  121. child: widget);
  122. }
  123. }