Created
January 15, 2020 15:53
-
-
Save daohoangson/6d95897dfb942851938a98949710233a to your computer and use it in GitHub Desktop.
DiscoBox with TweenColor sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MaterialApp( | |
home: DiscoBox(colors: const [Colors.red, Colors.green, Colors.blue]), | |
)); | |
class DiscoBox extends StatefulWidget { | |
final List<Color> colors; | |
DiscoBox({@required this.colors}); | |
@override | |
_DiscoBoxState createState() => _DiscoBoxState(); | |
} | |
class _DiscoBoxState extends State<DiscoBox> with TickerProviderStateMixin { | |
AnimationController _controller; | |
Animation<Color> _tween; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController(vsync: this) | |
..repeat(period: const Duration(seconds: 1)); | |
_tween = | |
TweenSequence<Color>(_listOf(widget.colors)).animate(_controller.view); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) => AnimatedBuilder( | |
animation: _controller.view, | |
builder: (_, __) => | |
Container(decoration: BoxDecoration(color: _tween.value)), | |
); | |
static List<TweenSequenceItem<Color>> _listOf(List<Color> colors) { | |
final l = List<TweenSequenceItem<Color>>(colors.length); | |
for (int i = 0; i < colors.length; i++) { | |
final a = colors[i]; | |
final b = i < colors.length - 1 ? colors[i + 1] : colors[0]; | |
l[i] = TweenSequenceItem(tween: ColorTween(begin: a, end: b), weight: 1); | |
} | |
return l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment