-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimated_opacity_reverse.dart
67 lines (60 loc) · 1.78 KB
/
animated_opacity_reverse.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
import 'package:flutter/material.dart';
/// Like [AnimatedOpacity], but with control over [reversedDuration]
class AnimatedVisibilityWithReversedDuration extends StatefulWidget {
/// Constructs AnimatedVisibilityWithReversedDuration
const AnimatedVisibilityWithReversedDuration({
Key? key,
required this.duration,
required this.visibility,
required this.child,
Duration? reversedDuration,
}) : reversedDuration = reversedDuration ?? duration,
super(key: key);
final Duration duration;
/// Reversed duration. If null - [duration] will be used
final Duration reversedDuration;
final bool visibility;
final Widget child;
@override
State<AnimatedVisibilityWithReversedDuration> createState() =>
_AnimatedVisibilityWithReversedDurationState();
}
class _AnimatedVisibilityWithReversedDurationState
extends State<AnimatedVisibilityWithReversedDuration>
with TickerProviderStateMixin {
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: widget.reversedDuration,
reverseDuration: widget.duration,
value: 1.0,
);
}
@override
void didUpdateWidget(
covariant AnimatedVisibilityWithReversedDuration oldWidget,
) {
if (oldWidget.visibility != widget.visibility) {
if (widget.visibility) {
controller.forward();
} else {
controller.reverse();
}
}
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: controller,
child: widget.child,
builder: (context, child) {
return Opacity(
opacity: controller.value,
child: child,
);
},
);
}