Skip to content

Commit 271d90c

Browse files
committed
fixes AnimationType.onHover
1 parent 72bdddc commit 271d90c

5 files changed

Lines changed: 63 additions & 11 deletions

File tree

lib/animated_toggle_switch.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter/gestures.dart';
99
import 'package:flutter/material.dart';
1010

1111
part 'src/animation_type_builder.dart';
12+
part 'src/animations.dart';
1213
part 'src/properties.dart';
1314
part 'src/style.dart';
1415
part 'src/tweens.dart';

lib/src/animation_type_builder.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class _AnimationTypeHoverBuilderState<T, V>
6161
StyledToggleProperties(value: values[index2], index: index2)),
6262
pos - pos.floor(),
6363
);
64-
final unlistedDoubleValue = isListed ? 0.0 : 1.0;
65-
return TweenAnimationBuilder<double>(
66-
duration: widget.indicatorAppearingDuration,
67-
curve: widget.indicatorAppearingCurve,
68-
tween: Tween(begin: unlistedDoubleValue, end: unlistedDoubleValue),
69-
builder: (context, unlistedDoubleValue, _) {
70-
if (unlistedDoubleValue == 0.0) {
64+
final indicatorAppearingAnimation =
65+
widget.properties._indicatorAppearingAnimation;
66+
return AnimatedBuilder(
67+
animation: indicatorAppearingAnimation,
68+
builder: (context, _) {
69+
final appearingValue = indicatorAppearingAnimation.value;
70+
if (appearingValue >= 1.0) {
7171
return _EmptyWidget(
7272
key: _builderKey, child: widget.builder(listedValueFunction()));
7373
}
@@ -81,10 +81,10 @@ class _AnimationTypeHoverBuilderState<T, V>
8181
builder: (context, unlistedValue, _) {
8282
return _EmptyWidget(
8383
key: _builderKey,
84-
child: widget.builder(unlistedDoubleValue == 1.0
84+
child: widget.builder(appearingValue <= 0.0
8585
? unlistedValue
86-
: widget.lerp(listedValueFunction(), unlistedValue,
87-
unlistedDoubleValue)),
86+
: widget.lerp(
87+
unlistedValue, listedValueFunction(), appearingValue)),
8888
);
8989
});
9090
},

lib/src/animations.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
part of 'package:animated_toggle_switch/animated_toggle_switch.dart';
2+
3+
/// This class is a proxy for another animation.
4+
///
5+
/// It is used for passing animations in builders without exposing the real
6+
/// animation to users.
7+
class _PrivateAnimation<T> extends Animation<T> {
8+
final Animation<T> _parent;
9+
10+
_PrivateAnimation(this._parent);
11+
12+
@override
13+
void addListener(VoidCallback listener) => _parent.addListener(listener);
14+
15+
@override
16+
void addStatusListener(AnimationStatusListener listener) =>
17+
_parent.addStatusListener(listener);
18+
19+
@override
20+
void removeListener(VoidCallback listener) =>
21+
_parent.removeListener(listener);
22+
23+
@override
24+
void removeStatusListener(AnimationStatusListener listener) =>
25+
_parent.removeStatusListener(listener);
26+
27+
@override
28+
AnimationStatus get status => _parent.status;
29+
30+
@override
31+
T get value => _parent.value;
32+
}

lib/src/properties.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ class GlobalToggleProperties<T> {
4444

4545
final bool active;
4646

47+
/// This animation indicates whether the indicator is currently visible.
48+
///
49+
/// 0.0 means it is not visible.
50+
///
51+
/// 1.0 means it is fully visible.
52+
///
53+
/// Depending on the curve of the animation, the value can also be below 0.0 or above 1.0.
54+
///
55+
/// This will be publicly accessible in future releases.
56+
final Animation<double> _indicatorAppearingAnimation;
57+
4758
const GlobalToggleProperties({
4859
required this.position,
4960
required this.current,
@@ -55,7 +66,8 @@ class GlobalToggleProperties<T> {
5566
required this.mode,
5667
required this.loadingAnimationValue,
5768
required this.active,
58-
});
69+
required Animation<double> indicatorAppearingAnimation,
70+
}) : _indicatorAppearingAnimation = indicatorAppearingAnimation;
5971
}
6072

6173
class DetailedGlobalToggleProperties<T> extends GlobalToggleProperties<T> {
@@ -88,6 +100,7 @@ class DetailedGlobalToggleProperties<T> extends GlobalToggleProperties<T> {
88100
required super.mode,
89101
required super.loadingAnimationValue,
90102
required super.active,
103+
required super.indicatorAppearingAnimation,
91104
});
92105
}
93106

lib/src/widgets/custom_animated_toggle_switch.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ class _CustomAnimatedToggleSwitchState<T>
420420
double spacing = widget.spacing;
421421
final textDirection = _textDirectionOf(context);
422422
final loadingValue = _animationInfo.loading ? 1.0 : 0.0;
423+
final privateIndicatorAppearingAnimation =
424+
_PrivateAnimation(_appearingAnimation);
423425

424426
final defaultCursor = !_isActive
425427
? (_animationInfo.loading
@@ -463,6 +465,8 @@ class _CustomAnimatedToggleSwitchState<T>
463465
mode: _animationInfo.toggleMode,
464466
loadingAnimationValue: loadingValue,
465467
active: widget.active,
468+
indicatorAppearingAnimation:
469+
privateIndicatorAppearingAnimation,
466470
);
467471
Widget child = Padding(
468472
padding: widget.padding,
@@ -574,6 +578,8 @@ class _CustomAnimatedToggleSwitchState<T>
574578
mode: _animationInfo.toggleMode,
575579
loadingAnimationValue: loadingValue,
576580
active: widget.active,
581+
indicatorAppearingAnimation:
582+
privateIndicatorAppearingAnimation,
577583
);
578584

579585
List<Widget> stack = <Widget>[

0 commit comments

Comments
 (0)