Guest User

Untitled

a guest
Nov 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. import 'package:app/weather/widgets/CloudProperties.dart';
  2. import 'package:flutter/widgets.dart';
  3.  
  4. class WeatherCloudWidget extends StatefulWidget {
  5. final double sunSize;
  6. final CloudProperties properties;
  7.  
  8. WeatherCloudWidget({Key key, this.properties, this.sunSize})
  9. : super(key: key);
  10.  
  11. @override
  12. State<StatefulWidget> createState() => _WeatherCloudWidget();
  13. }
  14.  
  15. class _WeatherCloudWidget extends State<WeatherCloudWidget>
  16. with TickerProviderStateMixin {
  17. AnimationController controller;
  18. AnimationController controller2;
  19.  
  20. Animation<double> position;
  21. Animation<double> opacity;
  22.  
  23. @override
  24. initState() {
  25. super.initState();
  26. _startAnimation();
  27. }
  28.  
  29. @override
  30. Widget build(BuildContext context) {
  31. // screen width and height
  32. final screenWidth = MediaQuery.of(context).size.width;
  33. final screenHeight = MediaQuery.of(context).size.height;
  34.  
  35. final properties = widget.properties;
  36.  
  37. var vertical =
  38. screenHeight * 0.5 + (widget.sunSize * properties.verticalOffset * -1);
  39.  
  40. var horizontal = (screenWidth * 0.5) + (widget.sunSize * position.value);
  41.  
  42. print(opacity.value);
  43.  
  44. // both Positioned & Opacity widgets
  45.  
  46. return Positioned(
  47. left: horizontal,
  48. top: vertical,
  49. child: Opacity(
  50. opacity: opacity.value,
  51. child: Image.asset(
  52. properties.path,
  53. width: properties.getScaledWidth(widget.sunSize),
  54. height: properties.getScaledHeight(widget.sunSize),
  55. ),
  56. ));
  57.  
  58. // Positioned only
  59.  
  60. return Positioned(
  61. left: horizontal,
  62. top: vertical,
  63. child: Image.asset(
  64. properties.path,
  65. width: properties.getScaledWidth(widget.sunSize),
  66. height: properties.getScaledHeight(widget.sunSize),
  67. ));
  68.  
  69. // Opacity only
  70.  
  71. return Positioned(
  72. left: (screenWidth * 0.5) + (widget.sunSize * properties.tween[1]),
  73. top: vertical,
  74. child: Opacity(
  75. opacity: opacity.value,
  76. child: Image.asset(
  77. properties.path,
  78. width: properties.getScaledWidth(widget.sunSize),
  79. height: properties.getScaledHeight(widget.sunSize),
  80. ),
  81. ));
  82. }
  83.  
  84. @override
  85. dispose() {
  86. controller.dispose();
  87. controller2.dispose();
  88. super.dispose();
  89. }
  90.  
  91. void _startAnimation() {
  92. controller = AnimationController(
  93. duration: const Duration(milliseconds: 5000), vsync: this);
  94.  
  95. controller2 = AnimationController(
  96. duration: const Duration(milliseconds: 5000), vsync: this);
  97.  
  98. position = Tween(
  99. begin: widget.properties.tween[0], end: widget.properties.tween[1])
  100. .animate(
  101. new CurvedAnimation(parent: controller, curve: Curves.decelerate))
  102. ..addListener(() => setState(() {}));
  103.  
  104. opacity = Tween(begin: 0.0, end: 1.0).animate(controller2)
  105. ..addListener(() => setState(() {}));
  106.  
  107. controller.forward();
  108. controller2.forward();
  109. }
  110. }
  111.  
  112. class WeatherCloudWidget extends StatefulWidget {
  113. final double sunSize;
  114. final CloudProperties properties;
  115.  
  116. WeatherCloudWidget({Key key, this.properties, this.sunSize})
  117. : super(key: key);
  118.  
  119. @override
  120. State<StatefulWidget> createState() => _WeatherCloudWidget();
  121. }
  122.  
  123. class _WeatherCloudWidget extends State<WeatherCloudWidget>
  124. with TickerProviderStateMixin {
  125. AnimationController controller;
  126. Animation<Offset> position;
  127. Animation<double> opacity;
  128.  
  129. final alphaTween = new Tween(begin: 0.0, end: 1.0);
  130.  
  131. @override
  132. initState() {
  133. super.initState();
  134. _startAnimation();
  135. }
  136.  
  137. @override
  138. Widget build(BuildContext context) {
  139. // screen width and height
  140. final screenWidth = MediaQuery.of(context).size.width;
  141. final screenHeight = MediaQuery.of(context).size.height;
  142.  
  143. final properties = widget.properties;
  144.  
  145. var vertical = (screenHeight * 0.5) +
  146. (widget.sunSize * properties.verticalOffset * -1);
  147.  
  148. var horizontal =
  149. (screenWidth * 0.5) + (widget.sunSize * properties.tweenEnd);
  150.  
  151. return Positioned(
  152. left: horizontal,
  153. top: vertical,
  154. child: SlideTransition(
  155. position: position,
  156. child: FadeTransition(
  157. opacity: opacity,
  158. child: Image.asset(
  159. properties.path,
  160. width: properties.getScaledWidth(widget.sunSize),
  161. height: properties.getScaledHeight(widget.sunSize),
  162. ),
  163. )),
  164. );
  165. }
  166.  
  167. @override
  168. dispose() {
  169. controller.dispose();
  170. super.dispose();
  171. }
  172.  
  173. void _startAnimation() {
  174. controller = AnimationController(
  175. duration: const Duration(milliseconds: 2000), vsync: this);
  176.  
  177. position = new Tween<Offset>(
  178. begin: new Offset(widget.properties.tweenStart, 0.0),
  179. end: new Offset(0.0, 0.0),
  180. ).animate(new CurvedAnimation(parent: controller, curve: Curves.decelerate))
  181. ..addListener(() => setState(() {}));
  182.  
  183. opacity = alphaTween.animate(controller)
  184. ..addListener(() {
  185. print(opacity.value);
  186. setState(() {});
  187. });
  188.  
  189. controller.forward();
  190. }
  191. }
Add Comment
Please, Sign In to add comment