button_controller.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'dart:math';
  2. import 'package:battle/button/button_color.dart';
  3. import 'package:flutter/animation.dart';
  4. import 'package:flutter/foundation.dart';
  5. import '../data/data.dart';
  6. enum ButtonState { start, stop }
  7. class ButtonInfo {
  8. Color color;
  9. int startIndex;
  10. int endIndex;
  11. double process;
  12. ButtonInfo({
  13. required this.color,
  14. required this.startIndex,
  15. required this.endIndex,
  16. this.process = 0,
  17. });
  18. }
  19. class ButtonController with ChangeNotifier {
  20. final TickerProvider vsync;
  21. ButtonController({required this.vsync}) {
  22. _animationController = AnimationController(
  23. upperBound: 6.0,
  24. vsync: vsync,
  25. duration: Duration(seconds: 10),
  26. );
  27. List<Color> colorList = _generateColorList();
  28. List<int> startIndex = _generateIntList();
  29. List<int> endIndex = _generateIntList();
  30. startButton = List.generate(6, (index) {
  31. return ButtonInfo(
  32. color: colorList[index],
  33. startIndex: startIndex[index],
  34. endIndex: endIndex[index],
  35. );
  36. });
  37. _animationController.addListener(() {
  38. int index = _animationController.value ~/ 1;
  39. double process = _animationController.value % 1.0;
  40. startButton[index].process = process;
  41. notifyListeners();
  42. });
  43. }
  44. void start() {
  45. _animationController.forward();
  46. }
  47. void reset() {
  48. List<Color> colorList = _generateColorList();
  49. List<int> startIndex = _generateIntList();
  50. List<int> endIndex = _generateIntList();
  51. startButton = List.generate(6, (index) {
  52. return ButtonInfo(
  53. color: colorList[index],
  54. startIndex: startIndex[index],
  55. endIndex: endIndex[index],
  56. );
  57. });
  58. _animationController.forward(from: 0);
  59. }
  60. void stop() {
  61. _animationController.stop();
  62. }
  63. late AnimationController _animationController;
  64. late List<ButtonInfo> startButton = [];
  65. List<Color> _generateColorList() {
  66. List<Color> list = List.of(colors);
  67. var random = Random.secure();
  68. for (int i = 0; i < 10; i++) {
  69. int i = random.nextInt(list.length);
  70. int j = random.nextInt(list.length);
  71. Color temp = list[i];
  72. list[i] = list[j];
  73. list[j] = temp;
  74. }
  75. return list;
  76. }
  77. List<int> _generateIntList() {
  78. List<int> list = answers[0].toList();
  79. var random = Random.secure();
  80. for (int i = 0; i < 3; i++) {
  81. int i = random.nextInt(list.length);
  82. int j = random.nextInt(list.length);
  83. int temp = list[i];
  84. list[i] = list[j];
  85. list[j] = temp;
  86. }
  87. return list;
  88. }
  89. }