Skip to content

Commit 9f44f9d

Browse files
committed
migrates TapInfo to TapProperties
1 parent 7d3a947 commit 9f44f9d

7 files changed

Lines changed: 57 additions & 59 deletions

File tree

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class _MyHomePageState extends State<MyHomePage> {
457457
allowUnlistedValues: true,
458458
values: const [0, 1, 2, 3],
459459
onTap: (info) {
460-
if (nullableValue == info.tappedValue) {
460+
if (nullableValue == info.tapped?.value) {
461461
setState(() => nullableValue = null);
462462
}
463463
},

lib/src/animations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of 'package:animated_toggle_switch/animated_toggle_switch.dart';
22

3-
// this Animation is not covered because it contains no logic but
3+
// this Animation is not covered because it does not contain logic but
44
// forwards all methods to its parent Animation.
55
// coverage:ignore-start
66
/// This class is a proxy for another animation.

lib/src/properties.dart

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,37 +206,39 @@ class SeparatorProperties<T> {
206206
});
207207
}
208208

209-
class TapInfo<T> {
210-
/// The value that the user has tapped.
211-
final T? tappedValue;
212-
213-
/// The index of [tappedValue] in [values].
209+
class TapProperties<T> {
210+
/// Information about the point on which the user has tapped.
214211
///
215-
/// If [tappedValue] is [null], this [tappedIndex] is set to [-1].
216-
final int tappedIndex;
217-
218-
/// The current value which is given to the switch.
219-
///
220-
/// Helpful if the value is generated e.g.
221-
/// when the switch constructor is called.
222-
final T current;
223-
224-
/// The index of [current] in [values].
225-
///
226-
/// If [values] does not contain [current], this value is set to [-1].
227-
final int currentIndex;
212+
/// This value can be [null] if the user taps on the border of an
213+
/// [AnimatedToggleSwitch] or on the wrapper of a
214+
/// [CustomAnimatedToggleSwitch].
215+
final TapInfo<T>? tapped;
228216

229217
/// The values which are given to the switch.
230218
///
231219
/// Helpful if the list is generated e.g.
232220
/// when the switch constructor is called.
233221
final List<T> values;
234222

235-
const TapInfo({
236-
required this.tappedIndex,
237-
required this.tappedValue,
238-
required this.current,
239-
required this.currentIndex,
223+
const TapProperties({
224+
required this.tapped,
240225
required this.values,
241226
});
242227
}
228+
229+
class TapInfo<T> {
230+
/// The value that the user has tapped.
231+
final T value;
232+
233+
/// The index of [value] in [values].
234+
final int index;
235+
236+
/// The tapped position relative to the indices of the values.
237+
final double position;
238+
239+
TapInfo({
240+
required this.value,
241+
required this.index,
242+
required this.position,
243+
});
244+
}

lib/src/widgets/custom_animated_toggle_switch.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef IndicatorAppearingBuilder = Widget Function(
2626

2727
typedef ChangeCallback<T> = FutureOr<void> Function(T value);
2828

29-
typedef TapCallback<T> = FutureOr<void> Function(TapInfo<T> info);
29+
typedef TapCallback<T> = FutureOr<void> Function(TapProperties<T> props);
3030

3131
enum ToggleMode { animating, dragged, none }
3232

@@ -357,7 +357,7 @@ class _CustomAnimatedToggleSwitchState<T>
357357

358358
/// This method is called in two [GestureDetector]s because only one
359359
/// [GestureDetector.onTapUp] will be triggered.
360-
void _onTap(TapInfo<T> info) {
360+
void _onTap(TapProperties<T> info) {
361361
if (!_isActive) return;
362362
final result = widget.onTap?.call(info);
363363
if (result is Future) {
@@ -377,8 +377,8 @@ class _CustomAnimatedToggleSwitchState<T>
377377
/// IMPORTANT: This must be called in [didUpdateWidget] because it updates
378378
/// [_currentIndex] also.
379379
void _checkValuePosition() {
380-
if (_animationInfo.toggleMode == ToggleMode.dragged) return;
381380
_currentIndex = widget.values.indexOf(widget.current);
381+
if (_animationInfo.toggleMode == ToggleMode.dragged) return;
382382
if (_currentIndex >= 0) {
383383
_animateTo(_currentIndex);
384384
} else {
@@ -402,11 +402,17 @@ class _CustomAnimatedToggleSwitchState<T>
402402
return result;
403403
}
404404

405-
/// Returns the value index by the local position of the cursor.
405+
/// Returns the [TapInfo] by the local position of the cursor.
406406
/// It is mainly intended as a helper function for the build method.
407-
int _indexFromPosition(
407+
TapInfo<T> _tapInfoFromPosition(
408408
double x, DetailedGlobalToggleProperties<T> properties) {
409-
return _doubleFromPosition(x, properties).round();
409+
final position = _doubleFromPosition(x, properties);
410+
final index = position.round();
411+
return TapInfo(
412+
value: widget.values[index],
413+
index: index,
414+
position: position,
415+
);
410416
}
411417

412418
@override
@@ -433,11 +439,8 @@ class _CustomAnimatedToggleSwitchState<T>
433439
cursor: defaultCursor,
434440
child: GestureDetector(
435441
behavior: HitTestBehavior.deferToChild,
436-
onTapUp: (_) => _onTap(TapInfo(
437-
tappedIndex: -1,
438-
tappedValue: null,
439-
current: widget.current,
440-
currentIndex: _currentIndex,
442+
onTapUp: (_) => _onTap(TapProperties(
443+
tapped: null,
441444
values: widget.values,
442445
)),
443446
child: TweenAnimationBuilder<double>(
@@ -648,20 +651,17 @@ class _CustomAnimatedToggleSwitchState<T>
648651
behavior: HitTestBehavior.translucent,
649652
dragStartBehavior: DragStartBehavior.down,
650653
onTapUp: (details) {
651-
int tappedIndex = _indexFromPosition(
654+
final tapInfo = _tapInfoFromPosition(
652655
details.localPosition.dx, properties);
653-
T tappedValue =
654-
widget.values[tappedIndex];
655-
_onTap(TapInfo(
656-
tappedIndex: tappedIndex,
657-
tappedValue: tappedValue,
658-
current: widget.current,
659-
currentIndex: _currentIndex,
656+
_onTap(TapProperties(
657+
tapped: tapInfo,
660658
values: widget.values,
661659
));
662660
if (!widget.iconsTappable) return;
663-
if (tappedValue == widget.current) return;
664-
_onChanged(tappedValue);
661+
if (tapInfo.value == widget.current) {
662+
return;
663+
}
664+
_onChanged(tapInfo.value);
665665
},
666666
onHorizontalDragStart: (details) {
667667
if (!isHoveringIndicator(

test/gesture_test.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@ void main() {
2828

2929
await tester.tap(currentFinder, warnIfMissed: false);
3030
verify(() => tapFunction(any(
31-
that: isA<TapInfo<int>>()
32-
.having((i) => i.tappedValue, 'tappedValue', current)))).called(1);
31+
that: isA<TapProperties<int>>().having(
32+
(i) => i.tapped?.value, 'tapped.value', current)))).called(1);
3333

3434
await tester.tap(nextFinder, warnIfMissed: false);
3535
verify(() => changedFunction(next)).called(1);
3636
verify(() => tapFunction(any(
37-
that: isA<TapInfo<int>>()
38-
.having((i) => i.tappedValue, 'tappedValue', next)))).called(1);
37+
that: isA<TapProperties<int>>()
38+
.having((i) => i.tapped?.value, 'tapped.value', next)))).called(1);
3939

4040
// tap on the border of the switch
4141
await tester.tapAt(tester.getRect(switchFinder).centerLeft);
4242
verify(() => tapFunction(any(
43-
that: isA<TapInfo<int>>()
44-
.having((i) => i.tappedValue, 'tappedValue', null)
45-
.having((i) => i.tappedIndex, 'tappedIndex', -1)))).called(1);
43+
that: isA<TapProperties<int>>()
44+
.having((i) => i.tapped, 'tapped', null)))).called(1);
4645

4746
verifyNoMoreInteractions(tapFunction);
4847
verifyNoMoreInteractions(changedFunction);

test/helper.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,8 @@ void defaultTestAllSwitches(
123123
bool testCustom = true,
124124
bool testSize = true,
125125
}) {
126-
registerFallbackValue(const TapInfo<int>(
127-
tappedIndex: -1,
128-
tappedValue: -1,
129-
current: -1,
130-
currentIndex: -1,
126+
registerFallbackValue(const TapProperties<int>(
127+
tapped: null,
131128
values: [],
132129
));
133130

test/mocks.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:animated_toggle_switch/animated_toggle_switch.dart';
22
import 'package:mocktail/mocktail.dart';
33

44
abstract class TestOnTapFunction<T> {
5-
void call(TapInfo<T> info);
5+
void call(TapProperties<T> props);
66
}
77

88
abstract class TestOnChangedFunction<T> {

0 commit comments

Comments
 (0)