123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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<Color> colorList = _generateColorList();
- List<int> startIndex = _generateIntList();
- List<int> 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<Color> colorList = _generateColorList();
- List<int> startIndex = _generateIntList();
- List<int> 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<ButtonInfo> startButton = [];
- List<Color> _generateColorList() {
- List<Color> 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<int> _generateIntList() {
- List<int> 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;
- }
- }
|