Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. import 'package:frideos/frideos.dart';
  4. import './custom/custom_blur_widget.dart';
  5.  
  6. import '../models/models.dart';
  7.  
  8. class CountdownWidget extends StatefulWidget {
  9. const CountdownWidget(
  10. {@required this.width, Key key, this.duration, this.triviaState})
  11. : assert(width != null),
  12. super(key: key);
  13.  
  14. final double width;
  15. final int duration; // Milliseconds
  16. final TriviaState triviaState;
  17.  
  18. @override
  19. CountdownWidgetState createState() {
  20. return CountdownWidgetState();
  21. }
  22. }
  23.  
  24. class CountdownWidgetState extends State<CountdownWidget>
  25. with SingleTickerProviderStateMixin {
  26. Animation<double> animation;
  27. AnimationController controller;
  28. Color color;
  29. final double countdownBarHeight = 24.0;
  30.  
  31. @override
  32. void initState() {
  33. super.initState();
  34.  
  35. controller = AnimationController(
  36. duration: Duration(milliseconds: widget.duration), vsync: this);
  37. animation = Tween<double>(begin: widget.width, end: 0).animate(controller)
  38. ..addListener(() {
  39. setState(() {
  40. // Animate the countdown bar from green to red
  41. final value = (animation.value ~/ 2).toInt();
  42. color = Color.fromRGBO(255 - value, value > 255 ? 255 : value, 0, 1);
  43. });
  44.  
  45. // Stop the animation if an anwser is chosen
  46. if (widget.triviaState.isAnswerChosen) {
  47. controller.stop();
  48. }
  49. });
  50.  
  51. color = Colors.green;
  52. }
  53.  
  54. @override
  55. void didUpdateWidget(CountdownWidget oldWidget) {
  56. super.didUpdateWidget(oldWidget);
  57. color = Colors.green;
  58.  
  59. // If the user click on an anwser the countdown animation
  60. // will stop.
  61. if (widget.triviaState.isAnswerChosen) {
  62. controller.stop();
  63. }
  64. // Otherwise, when a new question appears on the screen and
  65. // the widget rebuilds, then reset and play the countdown bar
  66. // animation.
  67. else {
  68. controller
  69. ..reset()
  70. ..forward();
  71. }
  72. }
  73.  
  74. @override
  75. void dispose() {
  76. controller.dispose();
  77. super.dispose();
  78. }
  79.  
  80. @override
  81. Widget build(BuildContext context) {
  82. return Row(
  83. children: <Widget>[
  84. Container(
  85. height: countdownBarHeight,
  86. width: animation.value,
  87. child: CustomBlurWidget(
  88. sigmaX: 12.0,
  89. sigmaY: 13.0,
  90. child: Container(
  91. color: color,
  92. ),
  93. ),
  94. ),
  95. ],
  96. );
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement