toast.dart 3.7 KB

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