-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathtween_sequence.dart
145 lines (128 loc) · 4.58 KB
/
tween_sequence.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/material.dart';
import '../diagrams.dart';
final Duration _kTotalDuration =
_kBreakDuration +
_kAnimationDuration +
_kBreakDuration +
_kAnimationDuration;
const Duration _kAnimationDuration = Duration(seconds: 6);
const Duration _kBreakDuration = Duration(seconds: 1, milliseconds: 500);
class TweenSequenceDiagram extends StatefulWidget with DiagramMetadata {
const TweenSequenceDiagram({super.key});
@override
State<TweenSequenceDiagram> createState() => TweenSequenceDiagramState();
@override
String get name => 'tween_sequence';
@override
Duration? get duration => _kTotalDuration;
}
class TweenSequenceDiagramState extends State<TweenSequenceDiagram>
with TickerProviderStateMixin, LockstepStateMixin {
late AnimationController _controller;
int _activeItem = 0;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: _kAnimationDuration,
);
waitLockstep(_kBreakDuration).then((_) => _controller.forward());
waitLockstep(
_kBreakDuration + _kAnimationDuration + _kBreakDuration,
).then((_) => _controller.reverse());
_controller.addListener(() {
if (_controller.value == 1.0 || _controller.value == 0.0) {
_activeItem = 0;
} else if (_controller.value < 0.4) {
_activeItem = 1;
} else if (_controller.value < 0.6) {
_activeItem = 2;
} else if (_controller.value < 1.0) {
_activeItem = 3;
} else {
assert(false);
}
});
}
final Animatable<Color?> _tweenSequence =
TweenSequence<Color?>(<TweenSequenceItem<Color?>>[
TweenSequenceItem<Color?>(
tween: ColorTween(begin: Colors.yellow, end: Colors.green),
weight: 2,
),
TweenSequenceItem<Color?>(
tween: ConstantTween<Color>(Colors.green),
weight: 1,
),
TweenSequenceItem<Color?>(
tween: ColorTween(begin: Colors.green, end: Colors.red),
weight: 2,
),
]);
final TextStyle _activeStyle = TextStyle(color: Colors.blue[800]);
@override
Widget build(BuildContext context) {
return Container(
height: 250,
width: 646,
color: Colors.white,
child: Center(
child: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget? child) {
return Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: const EdgeInsets.all(16),
width: 200,
height: 200,
color: _tweenSequence.evaluate(_controller),
),
DefaultTextStyle(
style: DefaultTextStyle.of(
context,
).style.copyWith(height: 1.2, fontSize: 13.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text('TweenSequence(['),
Text(
' TweenSequenceItem(\n tween: ColorTween(begin: Colors.yellow, end: Colors.green),\n weight: 2,\n ),',
style: _activeItem == 1 ? _activeStyle : null,
),
Text(
' TweenSequenceItem(\n tween: ConstantTween(Colors.green),\n weight: 1,\n ),',
style: _activeItem == 2 ? _activeStyle : null,
),
Text(
' TweenSequenceItem(\n tween: ColorTween(begin: Colors.green, end: Colors.red),\n weight: 2,\n ),',
style: _activeItem == 3 ? _activeStyle : null,
),
const Text(']);'),
],
),
),
],
),
);
},
),
),
);
}
}
class TweenSequenceDiagramStep extends DiagramStep {
@override
final String category = 'animation';
@override
Future<List<TweenSequenceDiagram>> get diagrams async =>
<TweenSequenceDiagram>[const TweenSequenceDiagram()];
}