toast.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:fast/constants.dart';
  2. import 'package:fast/utils/size_fit.dart';
  3. import 'package:flutter/material.dart';
  4. // ignore: must_be_immutable
  5. class Toast extends StatefulWidget {
  6. String title;
  7. Widget? content;
  8. bool errorIcon = false;
  9. Toast({Key? key, required this.title, required this.content, bool? showError})
  10. : super(key: key) {
  11. if (showError == true) {
  12. errorIcon = showError!;
  13. }
  14. }
  15. @override
  16. State<Toast> createState() => _ToastState();
  17. }
  18. class _ToastState extends State<Toast> with SingleTickerProviderStateMixin {
  19. late AnimationController controller;
  20. late Animation alphaAni;
  21. late Animation alpha2Ani;
  22. late Animation scaleAni;
  23. late Animation positionAni;
  24. @override
  25. void initState() {
  26. super.initState();
  27. controller = AnimationController(
  28. vsync: this, duration: const Duration(milliseconds: 2500));
  29. alphaAni = Tween(begin: 0.0, end: 1.0).animate(
  30. CurvedAnimation(parent: controller, curve: const Interval(0.0, 0.1)));
  31. TweenSequenceItem item0 =
  32. TweenSequenceItem(tween: Tween(begin: 0.0, end: 1.1), weight: 9);
  33. TweenSequenceItem item1 =
  34. TweenSequenceItem(tween: Tween(begin: 1.1, end: 0.9), weight: 2);
  35. TweenSequenceItem item2 =
  36. TweenSequenceItem(tween: Tween(begin: 0.9, end: 1.0), weight: 1);
  37. TweenSequence tweenSequence = TweenSequence([item0, item1, item2]);
  38. scaleAni = tweenSequence.animate(
  39. CurvedAnimation(parent: controller, curve: const Interval(0.0, 0.1)));
  40. positionAni = Tween(begin: 0.0, end: 150.px).animate(
  41. CurvedAnimation(parent: controller, curve: const Interval(0.9, 1.0)));
  42. alpha2Ani = Tween(begin: 1.0, end: 0.0).animate(
  43. CurvedAnimation(parent: controller, curve: const Interval(0.9, 1.0)));
  44. controller.addListener(() {
  45. setState(() {});
  46. });
  47. controller.addStatusListener((status) {
  48. if (status == AnimationStatus.completed) {
  49. Navigator.of(context).pop();
  50. }
  51. });
  52. controller.forward();
  53. }
  54. @override
  55. void dispose() {
  56. controller.dispose();
  57. super.dispose();
  58. }
  59. @override
  60. Widget build(BuildContext context) {
  61. return Center(
  62. child: Opacity(
  63. opacity: alpha2Ani.value,
  64. child: Transform.scale(
  65. scale: scaleAni.value,
  66. child: Opacity(
  67. opacity: alphaAni.value,
  68. child: Container(
  69. width: 144.px,
  70. // height: 178.px,
  71. margin: EdgeInsets.only(top: positionAni.value),
  72. padding: EdgeInsets.only(top: 28.px, bottom: 24.px),
  73. // alignment: Alignment.center,
  74. decoration: BoxDecoration(
  75. borderRadius: BorderRadius.all(Radius.circular(32.px)),
  76. color: const Color(0xFF313F52),
  77. boxShadow: [
  78. BoxShadow(
  79. offset: Offset(0.0, 12.px),
  80. blurRadius: 24.px,
  81. color: kBgColor)
  82. ]),
  83. child: Column(
  84. mainAxisSize: MainAxisSize.min,
  85. children: [
  86. Image.asset(
  87. widget.errorIcon?'assets/images/tip_error.png':'assets/images/tip_success.png',
  88. width: 64.px,
  89. height: 64.px,
  90. ),
  91. SizedBox(
  92. height: 10.px,
  93. ),
  94. Text(
  95. widget.title,
  96. textAlign: TextAlign.center,
  97. style: TextStyle(
  98. fontSize: 16.px,
  99. color: Colors.white,
  100. fontWeight: FontWeight.w500,
  101. decoration: TextDecoration.none),
  102. ),
  103. if (widget.content != null)
  104. SizedBox(
  105. height: 10.px,
  106. ),
  107. if (widget.content != null) widget.content as Widget,
  108. ],
  109. ),
  110. ),
  111. ),
  112. ),
  113. ),
  114. );
  115. }
  116. }