-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathflip_card.dart
254 lines (218 loc) · 6.46 KB
/
flip_card.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
library flip_card;
import 'dart:math';
import 'package:flip_card/flip_card_controller.dart';
import 'package:flutter/material.dart';
enum FlipDirection {
VERTICAL,
HORIZONTAL,
}
enum CardSide {
FRONT,
BACK,
}
enum Fill { none, fillFront, fillBack }
class AnimationCard extends StatelessWidget {
AnimationCard({this.child, this.animation, this.direction});
final Widget? child;
final Animation<double>? animation;
final FlipDirection? direction;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animation!,
builder: (BuildContext context, Widget? child) {
var transform = Matrix4.identity();
transform.setEntry(3, 2, 0.001);
if (direction == FlipDirection.VERTICAL) {
transform.rotateX(animation!.value);
} else {
transform.rotateY(animation!.value);
}
return Transform(
transform: transform,
alignment: Alignment.center,
child: child,
);
},
child: child,
);
}
}
typedef void BoolCallback(bool isFront);
class FlipCard extends StatefulWidget {
final Widget front;
final Widget back;
/// The amount of milliseconds a turn animation will take.
final int speed;
final FlipDirection direction;
final VoidCallback? onFlip;
final BoolCallback? onFlipDone;
final FlipCardController? controller;
final Fill fill;
final CardSide side;
/// If the value is set, the flip effect will work automatically after the specified duration.
final Duration? autoFlipDuration;
/// When enabled, the card will flip automatically when touched. This behavior
/// can be disabled if this is not desired. To manually flip a card from your
/// code, you could do this:
///```dart
/// GlobalKey<FlipCardState> cardKey = GlobalKey<FlipCardState>();
///
/// @override
/// Widget build(BuildContext context) {
/// return FlipCard(
/// key: cardKey,
/// flipOnTouch: false,
/// front: Container(
/// child: RaisedButton(
/// onPressed: () => cardKey.currentState.toggleCard(),
/// child: Text('Toggle'),
/// ),
/// ),
/// back: Container(
/// child: Text('Back'),
/// ),
/// );
/// }
///```
final bool flipOnTouch;
final Alignment alignment;
const FlipCard({
Key? key,
required this.front,
required this.back,
this.speed = 500,
this.onFlip,
this.onFlipDone,
this.direction = FlipDirection.HORIZONTAL,
this.controller,
this.flipOnTouch = true,
this.alignment = Alignment.center,
this.fill = Fill.none,
this.side = CardSide.FRONT,
this.autoFlipDuration,
}) : super(key: key);
@override
State<StatefulWidget> createState() {
return FlipCardState(this.side == CardSide.FRONT);
}
}
class FlipCardState extends State<FlipCard>
with SingleTickerProviderStateMixin {
AnimationController? controller;
Animation<double>? _frontRotation;
Animation<double>? _backRotation;
bool isFront;
FlipCardState(this.isFront);
@override
void initState() {
super.initState();
controller = AnimationController(
value: isFront ? 0.0 : 1.0,
duration: Duration(milliseconds: widget.speed),
vsync: this,
);
_frontRotation = TweenSequence(
[
TweenSequenceItem<double>(
tween: Tween(begin: 0.0, end: pi / 2)
.chain(CurveTween(curve: Curves.easeIn)),
weight: 50.0,
),
TweenSequenceItem<double>(
tween: ConstantTween<double>(pi / 2),
weight: 50.0,
),
],
).animate(controller!);
_backRotation = TweenSequence(
[
TweenSequenceItem<double>(
tween: ConstantTween<double>(pi / 2),
weight: 50.0,
),
TweenSequenceItem<double>(
tween: Tween(begin: -pi / 2, end: 0.0)
.chain(CurveTween(curve: Curves.easeOut)),
weight: 50.0,
),
],
).animate(controller!);
widget.controller?.state = this;
if (widget.autoFlipDuration != null) {
Future.delayed(widget.autoFlipDuration!, toggleCard);
}
}
@override
void didUpdateWidget(FlipCard oldWidget) {
widget.controller?.state ??= this;
}
/// Flip the card
/// If awaited, returns after animation completes.
Future<void> toggleCard() async {
if(!mounted) return;
widget.onFlip?.call();
final isFrontBefore = isFront;
controller!.duration = Duration(milliseconds: widget.speed);
final animation = isFront ? controller!.forward() : controller!.reverse();
animation.whenComplete(() {
if (widget.onFlipDone != null) widget.onFlipDone!(isFront);
if (!mounted) return;
setState(() => isFront = !isFrontBefore);
});
}
/// Flip the card without playing an animation.
/// This cancels any ongoing animation.
void toggleCardWithoutAnimation() {
controller!.stop();
widget.onFlip?.call();
if (widget.onFlipDone != null) widget.onFlipDone!(isFront);
setState(() {
isFront = !isFront;
controller!.value = isFront ? 0.0 : 1.0;
});
}
@override
Widget build(BuildContext context) {
final frontPositioning = widget.fill == Fill.fillFront ? _fill : _noop;
final backPositioning = widget.fill == Fill.fillBack ? _fill : _noop;
final child = Stack(
alignment: widget.alignment,
fit: StackFit.passthrough,
children: <Widget>[
frontPositioning(_buildContent(front: true)),
backPositioning(_buildContent(front: false)),
],
);
/// if we need to flip the card on taps, wrap the content
if (widget.flipOnTouch) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: toggleCard,
child: child,
);
}
return child;
}
Widget _buildContent({required bool front}) {
/// pointer events that would reach the backside of the card should be
/// ignored
return IgnorePointer(
/// absorb the front card when the background is active (!isFront),
/// absorb the background when the front is active
ignoring: front ? !isFront : isFront,
child: AnimationCard(
animation: front ? _frontRotation : _backRotation,
child: front ? widget.front : widget.back,
direction: widget.direction,
),
);
}
@override
void dispose() {
controller!.dispose();
super.dispose();
}
}
Widget _fill(Widget child) => Positioned.fill(child: child);
Widget _noop(Widget child) => child;