weui_toast.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. part of cool_ui;
  2. typedef HideCallback = Future Function();
  3. class WeuiToast extends TransitionRoute{
  4. OverlayEntry _toastBarrier;
  5. final Widget message;
  6. final Widget icon;
  7. final RouteTransitionsBuilder _transitionBuilder;
  8. final Duration closeDuration;
  9. final Alignment alignment;
  10. WeuiToast({
  11. @required this.message,
  12. @required this.icon,
  13. this.alignment = const Alignment(0.0,-0.2),
  14. Duration transitionDuration = const Duration(milliseconds: 100),
  15. RouteTransitionsBuilder transitionBuilder,
  16. this.closeDuration
  17. }):assert(icon != null),
  18. assert(message!=null),
  19. _transitionDuration = transitionDuration,
  20. _transitionBuilder = transitionBuilder;
  21. @override
  22. void didChangePrevious(Route<dynamic> previousRoute) {
  23. super.didChangePrevious(previousRoute);
  24. changedInternalState();
  25. }
  26. @override
  27. void changedInternalState() {
  28. super.changedInternalState();
  29. _toastBarrier.markNeedsBuild();
  30. }
  31. @override
  32. Iterable<OverlayEntry> createOverlayEntries() sync* {
  33. yield _toastBarrier = OverlayEntry(builder: _buildToastBarrier);
  34. yield OverlayEntry(builder: _buildToastScope, maintainState: true);
  35. }
  36. // TODO: implement opaque
  37. @override
  38. bool get opaque => false;
  39. // TODO: implement transitionDuration
  40. @override
  41. Duration get transitionDuration => _transitionDuration;
  42. final Duration _transitionDuration;
  43. Widget _buildToastBarrier(BuildContext context){
  44. return IgnorePointer(
  45. ignoring: true,
  46. );
  47. }
  48. Widget _buildToastScope(BuildContext context){
  49. return Material(
  50. color: Colors.transparent,
  51. child: Align(
  52. alignment: this.alignment,
  53. child: IntrinsicHeight(
  54. child: AnimatedBuilder(
  55. animation: animation,
  56. builder: (context,child){
  57. return _buildTransition(context,animation,secondaryAnimation,child);
  58. },
  59. child: Container(
  60. width: 122.0,
  61. decoration: BoxDecoration(
  62. color: Color.fromRGBO(17, 17, 17, 0.7),
  63. borderRadius: BorderRadius.circular(5.0)
  64. ),
  65. constraints: BoxConstraints(
  66. minHeight: 122.0,
  67. ),
  68. child: Column(
  69. children: <Widget>[
  70. Container(
  71. margin: EdgeInsets.only(top: 22.0),
  72. constraints:BoxConstraints(
  73. minHeight: 55.0
  74. ) ,
  75. child: IconTheme(data: IconThemeData(color: Colors.white,size: 55.0), child: icon),
  76. ),
  77. DefaultTextStyle(
  78. style: TextStyle(
  79. color: Colors.white,
  80. fontSize: 16.0
  81. ),
  82. child: message,
  83. ),
  84. ],
  85. ),
  86. ),
  87. ),
  88. ),
  89. )
  90. );
  91. }
  92. Widget _buildTransition( BuildContext context,
  93. Animation<double> animation,
  94. Animation<double> secondaryAnimation,
  95. Widget child){
  96. if (_transitionBuilder == null) {
  97. return FadeTransition(
  98. opacity: CurvedAnimation(
  99. parent: animation,
  100. curve: Curves.linear,
  101. ),
  102. child: child);
  103. } // Some default transition
  104. return _transitionBuilder(context, animation, secondaryAnimation, child);
  105. }
  106. }
  107. Future showWeuiSuccessToast({
  108. @required BuildContext context,
  109. Widget message=const Text("成功"),
  110. RouteTransitionsBuilder transitionBuilder,
  111. Alignment alignment = const Alignment(0.0,-0.2),
  112. Duration closeDuration = const Duration(seconds: 3)
  113. }){
  114. var hide = showWeuiToast(
  115. context: context,
  116. alignment: alignment,
  117. message: message,
  118. icon: Icon(CoolUIIcons.success_no_circle),
  119. transitionBuilder:transitionBuilder);
  120. return Future.delayed(closeDuration,(){
  121. hide();
  122. });
  123. }
  124. HideCallback showWeuiLoadingToast({
  125. @required BuildContext context,
  126. @required Widget message,
  127. Alignment alignment = const Alignment(0.0,-0.2),
  128. RouteTransitionsBuilder transitionBuilder
  129. }){
  130. return showWeuiToast(
  131. context: context,
  132. alignment: alignment,
  133. message: message,
  134. icon: WeuiLoadingIcon(),
  135. transitionBuilder:transitionBuilder);
  136. }
  137. HideCallback showWeuiToast({
  138. @required BuildContext context,
  139. @required Widget message,
  140. @required Widget icon,
  141. Alignment alignment = const Alignment(0.0,-0.2),
  142. RouteTransitionsBuilder transitionBuilder}){
  143. Completer<VoidCallback> result = Completer<VoidCallback>();
  144. Navigator.of(context,rootNavigator: true).push(
  145. WeuiToast(
  146. alignment: alignment,
  147. message: Builder(builder: (context){
  148. if(!result.isCompleted){
  149. result.complete(()=>Navigator.pop(context));
  150. }
  151. return message;
  152. }),
  153. icon: icon,
  154. transitionBuilder:transitionBuilder));
  155. return () async{
  156. var hide = await result.future;
  157. hide();
  158. };
  159. }
  160. class WeuiLoadingIcon extends StatefulWidget{
  161. final double size;
  162. WeuiLoadingIcon({this.size = 50.0});
  163. @override
  164. State<StatefulWidget> createState() => WeuiLoadingIconState();
  165. }
  166. class WeuiLoadingIconState extends State<WeuiLoadingIcon>
  167. with SingleTickerProviderStateMixin
  168. {
  169. AnimationController _controller;
  170. Animation<double> _doubleAnimation;
  171. @override
  172. void initState() {
  173. super.initState();
  174. _controller = new AnimationController(
  175. vsync: this, duration: Duration(milliseconds: 1000))
  176. ..repeat();
  177. _doubleAnimation= Tween(begin: 0.0,end: 360.0).animate(_controller)..addListener((){
  178. setState(() {
  179. });
  180. });
  181. }
  182. @override
  183. void dispose() {
  184. // TODO: implement dispose
  185. _controller.dispose();
  186. super.dispose();
  187. }
  188. @override
  189. Widget build(BuildContext context) {
  190. return Transform.rotate(
  191. angle: _doubleAnimation.value ~/ 30 * 30.0 * 0.0174533,
  192. child: Image.asset("assets/images/loading.png",
  193. package: "cool_ui",
  194. width: widget.size,
  195. height: widget.size)
  196. );
  197. }
  198. }