import 'dart:math'; import 'package:battle/button/button_color.dart'; import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; import '../data/data.dart'; enum ButtonState { start, stop } class ButtonInfo { Color color; int startIndex; int endIndex; double process; ButtonInfo({ required this.color, required this.startIndex, required this.endIndex, this.process = 0, }); } class ButtonController with ChangeNotifier { final TickerProvider vsync; ButtonController({required this.vsync}) { _animationController = AnimationController( upperBound: 6.0, vsync: vsync, duration: Duration(seconds: 10), ); List colorList = _generateColorList(); List startIndex = _generateIntList(); List endIndex = _generateIntList(); startButton = List.generate(6, (index) { return ButtonInfo( color: colorList[index], startIndex: startIndex[index], endIndex: endIndex[index], ); }); _animationController.addListener(() { int index = _animationController.value ~/ 1; double process = _animationController.value % 1.0; startButton[index].process = process; notifyListeners(); }); } void start() { _animationController.forward(); } void reset() { List colorList = _generateColorList(); List startIndex = _generateIntList(); List endIndex = _generateIntList(); startButton = List.generate(6, (index) { return ButtonInfo( color: colorList[index], startIndex: startIndex[index], endIndex: endIndex[index], ); }); _animationController.forward(from: 0); } void stop() { _animationController.stop(); } late AnimationController _animationController; late List startButton = []; List _generateColorList() { List list = List.of(colors); var random = Random.secure(); for (int i = 0; i < 10; i++) { int i = random.nextInt(list.length); int j = random.nextInt(list.length); Color temp = list[i]; list[i] = list[j]; list[j] = temp; } return list; } List _generateIntList() { List list = answers[0].toList(); var random = Random.secure(); for (int i = 0; i < 3; i++) { int i = random.nextInt(list.length); int j = random.nextInt(list.length); int temp = list[i]; list[i] = list[j]; list[j] = temp; } return list; } }