mirror of https://github.com/flutter/samples.git
add TweenSequence sample (#127)
* Add TweenSequence sample * rename to TweenSequenceDemo * add copyright headers * remote type * introduce animated builder earlier in basics/ so that other examples can use it * formatting * use async / await, increase animation durationpull/128/head
parent
c2f50f31ff
commit
349ca7bc6f
@ -0,0 +1,74 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TweenSequenceDemo extends StatefulWidget {
|
||||
static const String routeName = '/basics/chaining_tweens';
|
||||
|
||||
@override
|
||||
_TweenSequenceDemoState createState() => _TweenSequenceDemoState();
|
||||
}
|
||||
|
||||
class _TweenSequenceDemoState extends State<TweenSequenceDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const Duration duration = Duration(seconds: 3);
|
||||
AnimationController controller;
|
||||
Animation<Color> animation;
|
||||
|
||||
static final colors = [
|
||||
Colors.red,
|
||||
Colors.orange,
|
||||
Colors.yellow,
|
||||
Colors.green,
|
||||
Colors.blue,
|
||||
Colors.indigo,
|
||||
Colors.purple,
|
||||
];
|
||||
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
final sequenceItems = <TweenSequenceItem<Color>>[];
|
||||
|
||||
for (var i = 0; i < colors.length; i++) {
|
||||
final beginColor = colors[i];
|
||||
final endColor = colors[(i + 1) % colors.length];
|
||||
final weight = 1 / colors.length;
|
||||
|
||||
sequenceItems.add(
|
||||
TweenSequenceItem<Color>(
|
||||
tween: ColorTween(begin: beginColor, end: endColor),
|
||||
weight: weight,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
controller = AnimationController(duration: duration, vsync: this);
|
||||
animation = TweenSequence<Color>(sequenceItems).animate(controller);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: AnimatedBuilder(
|
||||
animation: animation,
|
||||
builder: (context, child) {
|
||||
return MaterialButton(
|
||||
color: animation.value,
|
||||
onPressed: () async {
|
||||
await controller.forward();
|
||||
controller.reset();
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
},
|
||||
child: Text('Animate', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue