From 4159cf78c4ac2feae90d6dd155564e5b0b43609b Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Sat, 29 Jul 2023 11:46:59 +0200 Subject: [PATCH 1/9] adds analysis file & adds separator tests --- analysis_options.yaml | 9 + example/lib/main.dart | 13 +- example/pubspec.lock | 2 +- lib/src/properties.dart | 4 +- lib/src/widgets/animated_toggle_switch.dart | 155 +++++++++--------- .../custom_animated_toggle_switch.dart | 23 +-- lib/src/widgets/drag_region.dart | 2 +- pubspec.lock | 20 ++- pubspec.yaml | 1 + test/gesture_test.dart | 10 +- test/helper.dart | 59 ++++++- test/keys.dart | 15 +- test/loading_test.dart | 8 +- test/separator_test.dart | 74 +++++++++ 14 files changed, 283 insertions(+), 112 deletions(-) create mode 100644 analysis_options.yaml create mode 100644 test/separator_test.dart diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..9b1ed37 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,9 @@ +include: package:flutter_lints/flutter.yaml + +analyzer: + language: + strict-casts: true + strict-raw-types: true + +linter: + rules: \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 96c6206..aaf85fd 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -161,7 +161,7 @@ class _MyHomePageState extends State { const CupertinoActivityIndicator(color: Colors.white), onChanged: (b) { setState(() => positive = b); - return Future.delayed(Duration(seconds: 2)); + return Future.delayed(Duration(seconds: 2)); }, styleBuilder: (b) => ToggleStyle( indicatorColor: b ? Colors.purple : Colors.green), @@ -206,7 +206,7 @@ class _MyHomePageState extends State { Colors.red[800], green, global.position)), onChanged: (b) { setState(() => positive = b); - return Future.delayed(Duration(seconds: 2)); + return Future.delayed(Duration(seconds: 2)); }, iconBuilder: (value) => value ? Icon(Icons.power_outlined, color: green, size: 32.0) @@ -280,7 +280,7 @@ class _MyHomePageState extends State { values: const [0, 1, 2, 3], onChanged: (i) { setState(() => value = i); - return Future.delayed(Duration(seconds: 3)); + return Future.delayed(Duration(seconds: 3)); }, iconBuilder: rollingIconBuilder, ), @@ -314,7 +314,7 @@ class _MyHomePageState extends State { value = i; loading = true; }); - return Future.delayed(Duration(seconds: 3)) + return Future.delayed(Duration(seconds: 3)) .then((_) => setState(() => loading = false)); }, iconBuilder: rollingIconBuilder, @@ -374,7 +374,8 @@ class _MyHomePageState extends State { values: const [0, 1, 2, 3], onChanged: (i) => setState(() => value = i), iconBuilder: rollingIconBuilder, - separatorBuilder: (context, index) => const VerticalDivider(), + separatorBuilder: (index) => + SizedBox.expand(child: ColoredBox(color: Colors.red)), borderWidth: 4.5, style: ToggleStyle( indicatorColor: Colors.white, @@ -497,7 +498,7 @@ class _MyHomePageState extends State { i.isEven == true ? Colors.amber : Colors.red), onChanged: (i) { setState(() => value = i); - return Future.delayed(Duration(seconds: 3)); + return Future.delayed(Duration(seconds: 3)); }, ), SizedBox(height: 16.0), diff --git a/example/pubspec.lock b/example/pubspec.lock index 928d22e..9d54bbb 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: path: ".." relative: true source: path - version: "0.8.0" + version: "0.8.0-beta.0" async: dependency: transitive description: diff --git a/lib/src/properties.dart b/lib/src/properties.dart index facf13b..8b7f837 100644 --- a/lib/src/properties.dart +++ b/lib/src/properties.dart @@ -177,7 +177,7 @@ class SizeProperties extends AnimatedToggleProperties { }); } -class DifProperties { +class SeparatorProperties { /// Index of the separator. /// /// The separator is located between the items at [index] and [index+1]. @@ -186,7 +186,7 @@ class DifProperties { /// The position of the separator relative to the indices of the values. double get position => index + 0.5; - const DifProperties({ + const SeparatorProperties({ required this.index, }); } diff --git a/lib/src/widgets/animated_toggle_switch.dart b/lib/src/widgets/animated_toggle_switch.dart index 635db47..856f340 100644 --- a/lib/src/widgets/animated_toggle_switch.dart +++ b/lib/src/widgets/animated_toggle_switch.dart @@ -42,6 +42,8 @@ typedef CustomStyleBuilder = ToggleStyle Function( GlobalToggleProperties global, ); +typedef SeparatorBuilder = Widget Function(int index); + class ToggleStyle { /// Background color of the indicator. final Color? indicatorColor; @@ -358,7 +360,7 @@ class AnimatedToggleSwitch extends StatelessWidget { /// The available width is specified by [dif]. /// /// This builder is supported by [IconArrangement.row] only. - final IndexedWidgetBuilder? separatorBuilder; + final SeparatorBuilder? separatorBuilder; /// Builder for divider or other separators between the icons. Consider using [separatorBuilder] for a simpler builder function. /// @@ -379,7 +381,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, this.indicatorSize = const Size(48.0, double.infinity), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, @@ -413,7 +415,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, - }) : this._iconArrangement = IconArrangement.row, + }) : _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -430,12 +432,12 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, this.indicatorSize = const Size(48.0, double.infinity), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, - iconSize = const Size(23.0, 23.0), - selectedIconSize = const Size(34.5, 34.5), + Size iconSize = const Size(23.0, 23.0), + Size selectedIconSize = const Size(34.5, 34.5), this.iconAnimationCurve = Curves.easeOutBack, this.iconAnimationDuration, this.iconOpacity = 0.5, @@ -468,7 +470,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.customSeparatorBuilder, }) : animatedIconBuilder = _iconSizeBuilder( iconBuilder, customIconBuilder, iconSize, selectedIconSize), - this._iconArrangement = IconArrangement.row, + _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -487,16 +489,16 @@ class AnimatedToggleSwitch extends StatelessWidget { SimpleSizeIconBuilder? iconBuilder, SizeIconBuilder? customIconBuilder, this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, - iconSize = const Size(0.5, 0.5), - selectedIconSize = const Size(0.75, 0.75), + Size iconSize = const Size(0.5, 0.5), + Size selectedIconSize = const Size(0.75, 0.75), this.iconAnimationCurve = Curves.easeOutBack, this.iconAnimationDuration, this.iconOpacity = 0.5, - dif = 0.0, + double dif = 0.0, this.foregroundIndicatorIconBuilder, this.selectedIconOpacity = 1.0, this.iconAnimationType = AnimationType.onSelected, @@ -522,14 +524,14 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, - }) : this.indicatorSize = indicatorSize * (height - 2 * borderWidth), - this.dif = dif * (height - 2 * borderWidth), + }) : indicatorSize = indicatorSize * (height - 2 * borderWidth), + dif = dif * (height - 2 * borderWidth), animatedIconBuilder = _iconSizeBuilder( iconBuilder, customIconBuilder, iconSize * (height + 2 * borderWidth), selectedIconSize * (height + 2 * borderWidth)), - this._iconArrangement = IconArrangement.row, + _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -539,8 +541,9 @@ class AnimatedToggleSwitch extends StatelessWidget { Size iconSize, Size selectedIconSize) { assert(iconBuilder == null || customIconBuilder == null); - if (customIconBuilder == null && iconBuilder != null) + if (customIconBuilder == null && iconBuilder != null) { customIconBuilder = (c, l, g) => iconBuilder(l.value, l.iconSize); + } return customIconBuilder == null ? null : (context, local, global) => customIconBuilder!( @@ -574,14 +577,14 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, Size indicatorSize = const Size(1.0, 1.0), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, this.iconAnimationCurve = Curves.easeOutBack, this.iconAnimationDuration, this.iconOpacity = 0.5, - dif = 0.0, + double dif = 0.0, this.foregroundIndicatorIconBuilder, this.selectedIconOpacity = 1.0, this.iconAnimationType = AnimationType.onSelected, @@ -607,9 +610,9 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, - }) : this.dif = dif * (height - 2 * borderWidth), - this.indicatorSize = indicatorSize * (height - 2 * borderWidth), - this._iconArrangement = IconArrangement.row, + }) : dif = dif * (height - 2 * borderWidth), + indicatorSize = indicatorSize * (height - 2 * borderWidth), + _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -634,7 +637,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, Size indicatorSize = const Size(1.0, 1.0), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, @@ -666,26 +669,25 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, - }) : this.iconAnimationCurve = Curves.linear, - this.dif = dif * (height - 2 * borderWidth), - this.iconAnimationDuration = Duration.zero, - this.indicatorSize = indicatorSize * (height - 2 * borderWidth), - this.selectedIconOpacity = iconOpacity, - this.iconAnimationType = AnimationType.onSelected, - this.foregroundIndicatorIconBuilder = - _rollingForegroundIndicatorIconBuilder( - values, - iconBuilder, - customIconBuilder, - Size.square( - selectedIconRadius * 2 * (height - 2 * borderWidth)), - transitionType), + }) : iconAnimationCurve = Curves.linear, + dif = dif * (height - 2 * borderWidth), + iconAnimationDuration = Duration.zero, + indicatorSize = indicatorSize * (height - 2 * borderWidth), + selectedIconOpacity = iconOpacity, + iconAnimationType = AnimationType.onSelected, + foregroundIndicatorIconBuilder = _rollingForegroundIndicatorIconBuilder< + T>( + values, + iconBuilder, + customIconBuilder, + Size.square(selectedIconRadius * 2 * (height - 2 * borderWidth)), + transitionType), animatedIconBuilder = _standardIconBuilder( iconBuilder, customIconBuilder, Size.square(iconRadius * 2 * (height - 2 * borderWidth)), Size.square(iconRadius * 2 * (height - 2 * borderWidth))), - this._iconArrangement = IconArrangement.row, + _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -706,7 +708,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, this.indicatorSize = const Size(46.0, double.infinity), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, @@ -739,23 +741,23 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, - }) : this.iconAnimationCurve = Curves.linear, - this.iconAnimationDuration = Duration.zero, - this.selectedIconOpacity = iconOpacity, - this.iconAnimationType = AnimationType.onSelected, - this.foregroundIndicatorIconBuilder = + }) : iconAnimationCurve = Curves.linear, + iconAnimationDuration = Duration.zero, + selectedIconOpacity = iconOpacity, + iconAnimationType = AnimationType.onSelected, + foregroundIndicatorIconBuilder = _rollingForegroundIndicatorIconBuilder( values, iconBuilder, customIconBuilder, Size.square(selectedIconRadius * 2), transitionType), - this.animatedIconBuilder = _standardIconBuilder( + animatedIconBuilder = _standardIconBuilder( iconBuilder, customIconBuilder, Size.square(iconRadius * 2), Size.square(iconRadius * 2)), - this._iconArrangement = IconArrangement.row, + _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -766,11 +768,12 @@ class AnimatedToggleSwitch extends StatelessWidget { Size iconSize, ForegroundIndicatorTransitionType transitionType) { assert(iconBuilder == null || customIconBuilder == null); - if (customIconBuilder == null && iconBuilder != null) + if (customIconBuilder == null && iconBuilder != null) { customIconBuilder = (c, l, g) => iconBuilder(l.value, l.iconSize, l.foreground); + } return (context, global) { - if (customIconBuilder == null) return SizedBox(); + if (customIconBuilder == null) return const SizedBox(); double distance = global.dif + global.indicatorSize.width; double angleDistance = transitionType == ForegroundIndicatorTransitionType.rolling @@ -824,9 +827,10 @@ class AnimatedToggleSwitch extends StatelessWidget { Size iconSize, Size selectedIconSize) { assert(iconBuilder == null || customIconBuilder == null); - if (customIconBuilder == null && iconBuilder != null) + if (customIconBuilder == null && iconBuilder != null) { customIconBuilder = (c, l, g) => iconBuilder(l.value, l.iconSize, l.foreground); + } return customIconBuilder == null ? null : (t, local, global) => customIconBuilder!( @@ -854,7 +858,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.animationCurve = Curves.easeInOutCirc, this.indicatorSize = const Size(46.0, double.infinity), this.onChanged, - this.borderWidth = 2, + this.borderWidth = 2.0, this.style = const ToggleStyle(), this.styleBuilder, this.customStyleBuilder, @@ -884,20 +888,19 @@ class AnimatedToggleSwitch extends StatelessWidget { ForegroundIndicatorTransitionType transitionType = ForegroundIndicatorTransitionType.rolling, }) : assert(clipAnimation || opacityAnimation), - this.iconOpacity = 1.0, - this.selectedIconOpacity = 1.0, - this.values = [first, second], - this.iconAnimationType = AnimationType.onHover, - this.iconsTappable = false, - this.onTap = onTap ?? _dualOnTap(onChanged, [first, second], current), - this.foregroundIndicatorIconBuilder = - _rollingForegroundIndicatorIconBuilder( - [first, second], - iconBuilder == null ? null : (v, s, f) => iconBuilder(v), - customIconBuilder, - Size.square(iconRadius * 2), - transitionType), - this.animatedIconBuilder = _dualIconBuilder( + iconOpacity = 1.0, + selectedIconOpacity = 1.0, + values = [first, second], + iconAnimationType = AnimationType.onHover, + iconsTappable = false, + onTap = onTap ?? _dualOnTap(onChanged, [first, second], current), + foregroundIndicatorIconBuilder = _rollingForegroundIndicatorIconBuilder( + [first, second], + iconBuilder == null ? null : (v, s, f) => iconBuilder(v), + customIconBuilder, + Size.square(iconRadius * 2), + transitionType), + animatedIconBuilder = _dualIconBuilder( textBuilder, customTextBuilder, Size.square(iconRadius * 2), @@ -907,14 +910,14 @@ class AnimatedToggleSwitch extends StatelessWidget { clipAnimation, opacityAnimation, ), - this._iconArrangement = IconArrangement.overlap, - this.allowUnlistedValues = false, - this.indicatorAppearingBuilder = _defaultIndicatorAppearingBuilder, - this.indicatorAppearingDuration = + _iconArrangement = IconArrangement.overlap, + allowUnlistedValues = false, + indicatorAppearingBuilder = _defaultIndicatorAppearingBuilder, + indicatorAppearingDuration = _defaultIndicatorAppearingAnimationDuration, - this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, - this.separatorBuilder = null, - this.customSeparatorBuilder = null, + indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, + separatorBuilder = null, + customSeparatorBuilder = null, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -1042,8 +1045,7 @@ class AnimatedToggleSwitch extends StatelessWidget { separatorBuilder: customSeparatorBuilder ?? (separatorBuilder == null ? null - : (context, local, global) => - separatorBuilder!(context, local.index)), + : (context, local, global) => separatorBuilder!(local.index)), backgroundIndicatorBuilder: foregroundIndicatorIconBuilder != null ? null : (context, properties) => @@ -1169,10 +1171,11 @@ class AnimatedToggleSwitch extends StatelessWidget { double animationValue = 0.0; double localPosition = global.position - global.position.floorToDouble(); - if (values[global.position.floor()] == local.value) + if (values[global.position.floor()] == local.value) { animationValue = 1 - localPosition; - else if (values[global.position.ceil()] == local.value) + } else if (values[global.position.ceil()] == local.value) { animationValue = localPosition; + } return _animatedIcon( context, AnimatedToggleProperties._fromLocal( @@ -1211,12 +1214,12 @@ class AnimatedToggleSwitch extends StatelessWidget { children: [ if (loadingValue < 1.0) Opacity( - key: ValueKey(0), + key: const ValueKey(0), opacity: 1.0 - loadingValue, child: child), if (loadingValue > 0.0) Opacity( - key: ValueKey(1), + key: const ValueKey(1), opacity: loadingValue, child: loadingIconBuilder(context, global)), ], @@ -1284,7 +1287,7 @@ class _CustomTween extends Tween { _CustomTween(this.lerpFunction, {super.begin, super.end}); @override - V lerp(double t) => lerpFunction(begin!, end!, t); + V lerp(double t) => lerpFunction(begin as V, end as V, t); } extension _XTargetPlatform on TargetPlatform { diff --git a/lib/src/widgets/custom_animated_toggle_switch.dart b/lib/src/widgets/custom_animated_toggle_switch.dart index d5aa09b..7dea02e 100644 --- a/lib/src/widgets/custom_animated_toggle_switch.dart +++ b/lib/src/widgets/custom_animated_toggle_switch.dart @@ -14,7 +14,7 @@ typedef CustomWrapperBuilder = Widget Function( /// Custom builder for the dif section between the icons. typedef CustomSeparatorBuilder = Widget Function(BuildContext context, - DifProperties local, DetailedGlobalToggleProperties global); + SeparatorProperties local, DetailedGlobalToggleProperties global); /// Custom builder for the appearing animation of the indicator. /// @@ -233,7 +233,7 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { super(key: key); @override - _CustomAnimatedToggleSwitchState createState() => + State> createState() => _CustomAnimatedToggleSwitchState(); bool get _isCurrentUnlisted => !values.contains(current); @@ -344,7 +344,7 @@ class _CustomAnimatedToggleSwitchState void _onChanged(T value) { if (!_isActive) return; - var result = widget.onChanged?.call(value); + dynamic result = widget.onChanged?.call(value); if (result is Future && widget.loading == null) { _loading(true); result.whenComplete(() => _loading(false)); @@ -353,7 +353,7 @@ class _CustomAnimatedToggleSwitchState void _onTap() { if (!_isActive) return; - var result = widget.onTap?.call(); + dynamic result = widget.onTap?.call(); if (result is Future && widget.loading == null) { _loading(true); result.whenComplete(() => _loading(false)); @@ -385,27 +385,29 @@ class _CustomAnimatedToggleSwitchState /// Returns the value position by the local position of the cursor. /// It is mainly intended as a helper function for the build method. double _doubleFromPosition( - double x, DetailedGlobalToggleProperties properties) { + double x, DetailedGlobalToggleProperties properties) { double result = (x.clamp( properties.indicatorSize.width / 2, properties.switchSize.width - properties.indicatorSize.width / 2) - properties.indicatorSize.width / 2) / (properties.indicatorSize.width + properties.dif); - if (properties.textDirection == TextDirection.rtl) + if (properties.textDirection == TextDirection.rtl) { result = widget.values.length - 1 - result; + } return result; } /// Returns the value index by the local position of the cursor. /// It is mainly intended as a helper function for the build method. - int _indexFromPosition(double x, DetailedGlobalToggleProperties properties) { + int _indexFromPosition( + double x, DetailedGlobalToggleProperties properties) { return _doubleFromPosition(x, properties).round(); } /// Returns the value by the local position of the cursor. /// It is mainly intended as a helper function for the build method. - T _valueFromPosition(double x, DetailedGlobalToggleProperties properties) { + T _valueFromPosition(double x, DetailedGlobalToggleProperties properties) { return widget.values[_indexFromPosition(x, properties)]; } @@ -606,8 +608,9 @@ class _CustomAnimatedToggleSwitchState _onChanged(newValue); }, onHorizontalDragStart: (details) { - if (!isHoveringIndicator(details.localPosition)) + if (!isHoveringIndicator(details.localPosition)) { return; + } _onDragged( _doubleFromPosition( details.localPosition.dx, properties), @@ -686,7 +689,7 @@ class _CustomAnimatedToggleSwitchState width: properties.dif, child: Center( child: widget.separatorBuilder!( - context, DifProperties(index: i), properties), + context, SeparatorProperties(index: i), properties), ), ), ] diff --git a/lib/src/widgets/drag_region.dart b/lib/src/widgets/drag_region.dart index 939e93e..5149ad3 100644 --- a/lib/src/widgets/drag_region.dart +++ b/lib/src/widgets/drag_region.dart @@ -85,9 +85,9 @@ class DragRegion extends StatelessWidget { return _HoverRegion( cursor: dragging ? draggingCursor : null, hoverCursor: dragCursor, - child: child, hoverCheck: hoverCheck, defaultCursor: defaultCursor, + child: child, ); } } diff --git a/pubspec.lock b/pubspec.lock index 98112e1..798dcf2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -110,6 +110,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4" + url: "https://pub.dev" + source: hosted + version: "2.0.2" flutter_test: dependency: "direct dev" description: flutter @@ -163,6 +171,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.7" + lints: + dependency: transitive + description: + name: lints + sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" + url: "https://pub.dev" + source: hosted + version: "2.1.1" logging: dependency: transitive description: @@ -388,10 +404,10 @@ packages: dependency: transitive description: name: vm_service - sha256: b8c67f5fa3897b122cf60fe9ff314f7b0ef71eab25c5f8b771480bc338f48823 + sha256: ada49637c27973c183dad90beb6bd781eea4c9f5f955d35da172de0af7bd3440 url: "https://pub.dev" source: hosted - version: "11.7.2" + version: "11.8.0" watcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 6626207..4bbd98f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: sdk: flutter dev_dependencies: + flutter_lints: ^2.0.2 flutter_test: sdk: flutter mocktail: ^0.3.0 diff --git a/test/gesture_test.dart b/test/gesture_test.dart index 043c7a5..1ca690a 100644 --- a/test/gesture_test.dart +++ b/test/gesture_test.dart @@ -11,7 +11,7 @@ void main() { final current = values.first; final next = values.last; final tapFunction = MockFunction(); - final changedFunction = MockOnChangedFunction(); + final changedFunction = MockOnChangedFunction(); await tester.pumpWidget(TestWrapper( child: buildSwitch( @@ -40,7 +40,7 @@ void main() { final values = defaultValues.sublist(0, 2); final current = values.first; final next = values.last; - final changedFunction = MockOnChangedFunction(); + final changedFunction = MockOnChangedFunction(); await tester.pumpWidget(TestWrapper( child: AnimatedToggleSwitch.dual( @@ -65,7 +65,7 @@ void main() { final current = values.first; final next = values.last; final tapFunction = MockFunction(); - final changedFunction = MockOnChangedFunction(); + final changedFunction = MockOnChangedFunction(); await tester.pumpWidget(TestWrapper( child: buildSwitch( @@ -73,6 +73,8 @@ void main() { iconBuilder: iconBuilder, onTap: tapFunction, onChanged: changedFunction, + // Necessary for AnimatedToggleSwitch.dual + dif: 5.0, ), )); final currentFinder = find.byKey(iconKey(current)); @@ -96,7 +98,7 @@ void main() { final current = values.first; final next = values.last; final tapFunction = MockFunction(); - final changedFunction = MockOnChangedFunction(); + final changedFunction = MockOnChangedFunction(); await tester.pumpWidget(TestWrapper( child: buildSwitch( diff --git a/test/helper.dart b/test/helper.dart index 1699ac0..9b2a562 100644 --- a/test/helper.dart +++ b/test/helper.dart @@ -20,7 +20,10 @@ class TestWrapper extends StatelessWidget { @override Widget build(BuildContext context) { - return Directionality(textDirection: textDirection, child: child); + return Directionality( + textDirection: textDirection, + child: Center(child: child), + ); } } @@ -34,12 +37,19 @@ void checkValidSwitchIconBuilderState(T current, List values) { } } -Widget iconBuilder(T value, bool foreground) => - Text(key: iconKey(value, foreground: foreground), '$value'); +Widget iconBuilder(T value, bool foreground) => SizedBox.expand( + key: iconKey(value, foreground: foreground), + child: const ColoredBox(color: Colors.black), + ); Key iconKey(T value, {bool foreground = false}) => IconKey(value, foreground: foreground); +Widget separatorBuilder(int index) => + SizedBox.expand(key: separatorKey(index)); + +Key separatorKey(int index) => SeparatorKey(index); + final loadingIconKey = GlobalKey(); Widget _loadingIconBuilder( @@ -48,7 +58,7 @@ Widget _loadingIconBuilder( typedef TestIconBuilder = Widget Function(T value, bool foreground); -typedef SwitchBuilder = AnimatedToggleSwitch Function({ +typedef SwitchBuilder = AnimatedToggleSwitch Function({ required T current, required List values, TestIconBuilder? iconBuilder, @@ -61,9 +71,11 @@ typedef SwitchBuilder = AnimatedToggleSwitch Function({ StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }); -typedef SimpleSwitchBuilder = AnimatedToggleSwitch Function({ +typedef SimpleSwitchBuilder = AnimatedToggleSwitch Function({ required T current, TestIconBuilder? iconBuilder, TextDirection? textDirection, @@ -75,6 +87,8 @@ typedef SimpleSwitchBuilder = AnimatedToggleSwitch Function({ StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }); /// Tests all AnimatedToggleSwitch constructors @@ -100,6 +114,8 @@ void defaultTestAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => buildSwitch( current: current, @@ -114,6 +130,8 @@ void defaultTestAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable, + dif: dif, + separatorBuilder: separatorBuilder, ), defaultValues, )); @@ -135,6 +153,8 @@ void defaultTestAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.dual( current: current, @@ -154,6 +174,7 @@ void defaultTestAllSwitches( style: style ?? const ToggleStyle(), styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, + dif: dif ?? 40, ), values, ), @@ -183,6 +204,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.rolling( current: current, @@ -201,6 +224,8 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: dif ?? 0.0, + separatorBuilder: separatorBuilder, ))); testWidgets( '$description (AnimatedToggleSwitch.size)', @@ -219,6 +244,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.size( current: current, @@ -236,6 +263,8 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: dif ?? 0.0, + separatorBuilder: separatorBuilder, ))); testWidgets( '$description (AnimatedToggleSwitch.rollingByHeight)', @@ -254,6 +283,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.rollingByHeight( current: current, @@ -272,6 +303,8 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: _convertToByHeightValue(dif ?? 0.0, 50.0, 2.0), + separatorBuilder: separatorBuilder, ))); testWidgets( '$description (AnimatedToggleSwitch.sizeByHeight)', @@ -290,6 +323,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.sizeByHeight( current: current, @@ -307,6 +342,8 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: _convertToByHeightValue(dif ?? 0.0, 50.0, 2.0), + separatorBuilder: separatorBuilder, ))); testWidgets( '$description (AnimatedToggleSwitch.custom)', @@ -325,6 +362,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.custom( current: current, @@ -343,6 +382,8 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: dif ?? 0.0, + separatorBuilder: separatorBuilder, ))); testWidgets( '$description (AnimatedToggleSwitch.customByHeight)', @@ -361,6 +402,8 @@ void testAllSwitches( StyleBuilder? styleBuilder, CustomStyleBuilder? customStyleBuilder, bool? iconsTappable, + double? dif, + SeparatorBuilder? separatorBuilder, }) => AnimatedToggleSwitch.customByHeight( current: current, @@ -379,5 +422,11 @@ void testAllSwitches( styleBuilder: styleBuilder, customStyleBuilder: customStyleBuilder, iconsTappable: iconsTappable ?? true, + dif: _convertToByHeightValue(dif ?? 0.0, 50.0, 2.0), + separatorBuilder: separatorBuilder, ))); } + +double _convertToByHeightValue( + double value, double height, double borderWidth) => + value / (height - 2 * borderWidth); diff --git a/test/keys.dart b/test/keys.dart index 1dbcccf..cebecf3 100644 --- a/test/keys.dart +++ b/test/keys.dart @@ -5,7 +5,7 @@ class IconKey extends LocalKey { final bool foreground; final T value; - IconKey(this.value, {this.foreground = false}); + const IconKey(this.value, {this.foreground = false}); @override bool operator ==(Object other) => @@ -17,3 +17,16 @@ class IconKey extends LocalKey { @override int get hashCode => foreground.hashCode ^ value.hashCode; } + +class SeparatorKey extends LocalKey { + final int index; + + const SeparatorKey(this.index); + + @override + bool operator ==(Object other) => + identical(this, other) || other is SeparatorKey && index == other.index; + + @override + int get hashCode => index.hashCode; +} diff --git a/test/loading_test.dart b/test/loading_test.dart index 786bd7b..1e654c5 100644 --- a/test/loading_test.dart +++ b/test/loading_test.dart @@ -14,8 +14,8 @@ void main() { child: buildSwitch( current: current, iconBuilder: iconBuilder, - onTap: () => Future.delayed(loadingDuration), - onChanged: (_) => Future.delayed(loadingDuration), + onTap: () => Future.delayed(loadingDuration), + onChanged: (_) => Future.delayed(loadingDuration), ), )); final currentFinder = find.byKey(iconKey(current)); @@ -104,8 +104,8 @@ void main() { child: buildSwitch( current: current, iconBuilder: iconBuilder, - onTap: () => Future.delayed(loadingDuration), - onChanged: (_) => Future.delayed(loadingDuration), + onTap: () => Future.delayed(loadingDuration), + onChanged: (_) => Future.delayed(loadingDuration), loading: false, ), )); diff --git a/test/separator_test.dart b/test/separator_test.dart new file mode 100644 index 0000000..6e36469 --- /dev/null +++ b/test/separator_test.dart @@ -0,0 +1,74 @@ +import 'package:flutter_test/flutter_test.dart'; + +import 'helper.dart'; + +void _checkValidSeparatorPositions( + WidgetTester tester, List values, double dif) { + for (int i = 0; i < values.length - 1; i++) { + final separatorFinder = find.byKey(separatorKey(i)); + final firstIconFinder = find.byKey(iconKey(values[i])); + final secondIconFinder = find.byKey(iconKey(values[i + 1])); + expect(separatorFinder, findsOneWidget); + expect( + tester.getBottomRight(firstIconFinder) - + tester.getBottomLeft(separatorFinder), + Offset.zero, + reason: 'separator is located right of the predecessor icon', + ); + expect( + tester.getBottomLeft(secondIconFinder) - + tester.getBottomRight(separatorFinder), + Offset.zero, + reason: 'separator is located left of the successor icon', + ); + expect( + tester.getBottomRight(separatorFinder).dx - + tester.getBottomLeft(separatorFinder).dx, + dif, + reason: 'separator has correct width', + ); + } +} + +void main() { + defaultTestAllSwitches('Switch builds separator at correct position', + (tester, buildSwitch, values) async { + const difs = [5.0, 10.0, 17.0]; + final current = values[1]; + for (final dif in difs) { + await tester.pumpWidget( + TestWrapper( + child: buildSwitch( + current: current, + dif: dif, + iconBuilder: iconBuilder, + separatorBuilder: separatorBuilder, + ), + ), + const Duration(milliseconds: 500)); + _checkValidSeparatorPositions(tester, values, dif); + } + }, testDual: false); + defaultTestAllSwitches( + 'AnimatedToggleSwitch changes its state when current changes', + (tester, buildSwitch, values) async { + final current = values.first; + final next = values.last; + await tester.pumpWidget(TestWrapper( + child: buildSwitch( + current: current, + iconBuilder: iconBuilder, + ), + )); + checkValidSwitchIconBuilderState(current, values); + await tester.pumpWidget(TestWrapper( + child: buildSwitch( + current: next, + iconBuilder: iconBuilder, + ), + )); + checkValidSwitchIconBuilderState(current, values); + await tester.pump(const Duration(seconds: 1)); + checkValidSwitchIconBuilderState(next, values); + }); +} From a55bd7688fe828ef18460009c5d99b9a8cd6ff49 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Sat, 29 Jul 2023 12:00:50 +0200 Subject: [PATCH 2/9] adds test for separatorBuilder --- .gitignore | 3 +++ test/separator_test.dart | 47 +++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 1985397..ac0c6e2 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,6 @@ build/ !**/ios/**/default.mode2v3 !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 + +# Coverage +coverage/ diff --git a/test/separator_test.dart b/test/separator_test.dart index 6e36469..8ea5108 100644 --- a/test/separator_test.dart +++ b/test/separator_test.dart @@ -37,38 +37,31 @@ void main() { final current = values[1]; for (final dif in difs) { await tester.pumpWidget( - TestWrapper( - child: buildSwitch( - current: current, - dif: dif, - iconBuilder: iconBuilder, - separatorBuilder: separatorBuilder, - ), + TestWrapper( + child: buildSwitch( + current: current, + dif: dif, + iconBuilder: iconBuilder, + separatorBuilder: separatorBuilder, ), - const Duration(milliseconds: 500)); + ), + ); _checkValidSeparatorPositions(tester, values, dif); } }, testDual: false); - defaultTestAllSwitches( - 'AnimatedToggleSwitch changes its state when current changes', + defaultTestAllSwitches('separatorBuilder does not support dif = 0.0', (tester, buildSwitch, values) async { final current = values.first; - final next = values.last; - await tester.pumpWidget(TestWrapper( - child: buildSwitch( - current: current, - iconBuilder: iconBuilder, + await tester.pumpWidget( + TestWrapper( + child: buildSwitch( + current: current, + dif: 0.0, + iconBuilder: iconBuilder, + separatorBuilder: separatorBuilder, + ), ), - )); - checkValidSwitchIconBuilderState(current, values); - await tester.pumpWidget(TestWrapper( - child: buildSwitch( - current: next, - iconBuilder: iconBuilder, - ), - )); - checkValidSwitchIconBuilderState(current, values); - await tester.pump(const Duration(seconds: 1)); - checkValidSwitchIconBuilderState(next, values); - }); + ); + expect(tester.takeException(), isAssertionError); + }, testDual: false); } From 8f2a88a0372bc29ff3b204916c5a4951990033f3 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 12:52:58 +0200 Subject: [PATCH 3/9] closes #30 --- CHANGELOG.md | 8 +- lib/animated_toggle_switch.dart | 3 + lib/src/properties.dart | 4 + lib/src/style.dart | 144 +++++++++++ lib/src/widgets/animated_toggle_switch.dart | 236 +++++++----------- lib/src/widgets/conditional_wrapper.dart | 38 +++ .../custom_animated_toggle_switch.dart | 18 +- pubspec.yaml | 4 +- 8 files changed, 301 insertions(+), 154 deletions(-) create mode 100644 lib/src/style.dart create mode 100644 lib/src/widgets/conditional_wrapper.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 56198ab..2ff44d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ -## 0.8.0-beta.0 +## 0.8.0-beta.1 (2023-07-31) +- adds `active` to all constructors +- closes [#30](https://github.com/splashbyte/animated_toggle_switch/issues/30) +- minor improvements +- BREAKING: changes `separatorBuilder` parameters + +## 0.8.0-beta.0 (2023-07-29) - adds tests for all `AnimatedToggleSwitch` constructors - adds `separatorBuilder`, `customSeparatorBuilder`, `style` and `styleAnimationType` to `AnimatedToggleSwitch` - adds `separatorBuilder` to `CustomAnimatedToggleSwitch` diff --git a/lib/animated_toggle_switch.dart b/lib/animated_toggle_switch.dart index 7012f0a..c7bd7ac 100644 --- a/lib/animated_toggle_switch.dart +++ b/lib/animated_toggle_switch.dart @@ -1,5 +1,6 @@ library animated_toggle_switch; +import 'dart:async'; import 'dart:math'; import 'package:flutter/cupertino.dart'; @@ -8,6 +9,8 @@ import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; part 'src/properties.dart'; +part 'src/style.dart'; part 'src/widgets/animated_toggle_switch.dart'; part 'src/widgets/custom_animated_toggle_switch.dart'; part 'src/widgets/drag_region.dart'; +part 'src/widgets/conditional_wrapper.dart'; diff --git a/lib/src/properties.dart b/lib/src/properties.dart index 8b7f837..366edc5 100644 --- a/lib/src/properties.dart +++ b/lib/src/properties.dart @@ -34,6 +34,8 @@ class GlobalToggleProperties { /// [0] means 'not loading' and [1] means 'loading'. final double loadingAnimationValue; + final bool active; + const GlobalToggleProperties({ required this.position, required this.current, @@ -43,6 +45,7 @@ class GlobalToggleProperties { required this.textDirection, required this.mode, required this.loadingAnimationValue, + required this.active, }); } @@ -74,6 +77,7 @@ class DetailedGlobalToggleProperties extends GlobalToggleProperties { required super.textDirection, required super.mode, required super.loadingAnimationValue, + required super.active, }); } diff --git a/lib/src/style.dart b/lib/src/style.dart new file mode 100644 index 0000000..4b6e885 --- /dev/null +++ b/lib/src/style.dart @@ -0,0 +1,144 @@ +part of 'package:animated_toggle_switch/animated_toggle_switch.dart'; + +class ToggleStyle { + /// Background color of the indicator. + final Color? indicatorColor; + + /// Background color of the switch. + final Color? backgroundColor; + + /// Gradient of the background. Overwrites [innerColor] if not [null]. + final Gradient? backgroundGradient; + + /// Border color of the switch. + /// + /// For deactivating please set this to [Colors.transparent]. + final Color? borderColor; + + /// [BorderRadius] of the switch. + final BorderRadiusGeometry? borderRadius; + + /// [BorderRadius] of the indicator. + /// + /// Defaults to [borderRadius]. + final BorderRadiusGeometry? indicatorBorderRadius; + + /// [BorderRadius] of the indicator. + final BoxBorder? indicatorBorder; + + /// Shadow for the indicator [Container]. + final List? indicatorBoxShadow; + + /// Shadow for the [Container] in the background. + final List? boxShadow; + + /// Default constructor for [ToggleStyle]. + const ToggleStyle({ + this.indicatorColor, + this.backgroundColor, + this.backgroundGradient, + this.borderColor, + this.borderRadius, + this.indicatorBorderRadius, + this.indicatorBorder, + this.indicatorBoxShadow, + this.boxShadow, + }); + + /// Private constructor for setting all possible parameters. + ToggleStyle._({ + required this.indicatorColor, + required this.backgroundColor, + required this.backgroundGradient, + required this.borderColor, + required this.borderRadius, + required this.indicatorBorderRadius, + required this.indicatorBorder, + required this.indicatorBoxShadow, + required this.boxShadow, + }); + + ToggleStyle _merge(ToggleStyle? other) => other == null + ? this + : ToggleStyle._( + indicatorColor: other.indicatorColor ?? indicatorColor, + backgroundColor: other.backgroundColor ?? backgroundColor, + backgroundGradient: other.backgroundGradient ?? + (other.backgroundColor == null ? null : backgroundGradient), + borderColor: other.borderColor ?? borderColor, + borderRadius: other.borderRadius ?? borderRadius, + indicatorBorderRadius: other.indicatorBorderRadius ?? + other.borderRadius ?? + indicatorBorderRadius ?? + borderRadius, + indicatorBorder: other.indicatorBorder ?? indicatorBorder, + indicatorBoxShadow: other.indicatorBoxShadow ?? indicatorBoxShadow, + boxShadow: other.boxShadow ?? boxShadow, + ); + + static ToggleStyle _lerp(ToggleStyle style1, ToggleStyle style2, double t) => + ToggleStyle._( + indicatorColor: + Color.lerp(style1.indicatorColor, style2.indicatorColor, t), + backgroundColor: + Color.lerp(style1.backgroundColor, style2.backgroundColor, t), + backgroundGradient: Gradient.lerp( + style1.backgroundGradient ?? style1.backgroundColor?.toGradient(), + style2.backgroundGradient ?? style2.backgroundColor?.toGradient(), + t, + ), + borderColor: Color.lerp(style1.borderColor, style2.borderColor, t), + borderRadius: BorderRadiusGeometry.lerp( + style1.borderRadius, + style2.borderRadius, + t, + ), + indicatorBorderRadius: BorderRadiusGeometry.lerp( + style1.indicatorBorderRadius ?? style1.borderRadius, + style2.indicatorBorderRadius ?? style2.borderRadius, + t, + ), + indicatorBorder: BoxBorder.lerp( + style1.indicatorBorder, + style2.indicatorBorder, + t, + ), + indicatorBoxShadow: BoxShadow.lerpList( + style1.indicatorBoxShadow, + style2.indicatorBoxShadow, + t, + ), + boxShadow: BoxShadow.lerpList( + style1.boxShadow, + style2.boxShadow, + t, + ), + ); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is ToggleStyle && + runtimeType == other.runtimeType && + indicatorColor == other.indicatorColor && + backgroundColor == other.backgroundColor && + backgroundGradient == other.backgroundGradient && + borderColor == other.borderColor && + borderRadius == other.borderRadius && + indicatorBorderRadius == other.indicatorBorderRadius && + indicatorBorder == other.indicatorBorder && + indicatorBoxShadow == other.indicatorBoxShadow && + boxShadow == other.boxShadow; + + @override + int get hashCode => + indicatorColor.hashCode ^ + backgroundColor.hashCode ^ + backgroundGradient.hashCode ^ + borderColor.hashCode ^ + borderRadius.hashCode ^ + indicatorBorderRadius.hashCode ^ + indicatorBorder.hashCode ^ + indicatorBoxShadow.hashCode ^ + boxShadow.hashCode; +} diff --git a/lib/src/widgets/animated_toggle_switch.dart b/lib/src/widgets/animated_toggle_switch.dart index 856f340..29abd95 100644 --- a/lib/src/widgets/animated_toggle_switch.dart +++ b/lib/src/widgets/animated_toggle_switch.dart @@ -44,122 +44,6 @@ typedef CustomStyleBuilder = ToggleStyle Function( typedef SeparatorBuilder = Widget Function(int index); -class ToggleStyle { - /// Background color of the indicator. - final Color? indicatorColor; - - /// Background color of the switch. - final Color? backgroundColor; - - /// Gradient of the background. Overwrites [innerColor] if not [null]. - final Gradient? backgroundGradient; - - /// Border color of the switch. - /// - /// For deactivating please set this to [Colors.transparent]. - final Color? borderColor; - - /// [BorderRadius] of the switch. - final BorderRadiusGeometry? borderRadius; - - /// [BorderRadius] of the indicator. - /// - /// Defaults to [borderRadius]. - final BorderRadiusGeometry? indicatorBorderRadius; - - /// [BorderRadius] of the indicator. - final BoxBorder? indicatorBorder; - - /// Shadow for the indicator [Container]. - final List? indicatorBoxShadow; - - /// Shadow for the [Container] in the background. - final List? boxShadow; - - /// Default constructor for [ToggleStyle]. - const ToggleStyle({ - this.indicatorColor, - this.backgroundColor, - this.backgroundGradient, - this.borderColor, - this.borderRadius, - this.indicatorBorderRadius, - this.indicatorBorder, - this.indicatorBoxShadow, - this.boxShadow, - }); - - /// Private constructor for setting all possible parameters. - ToggleStyle._({ - required this.indicatorColor, - required this.backgroundColor, - required this.backgroundGradient, - required this.borderColor, - required this.borderRadius, - required this.indicatorBorderRadius, - required this.indicatorBorder, - required this.indicatorBoxShadow, - required this.boxShadow, - }); - - ToggleStyle _merge(ToggleStyle? other) => other == null - ? this - : ToggleStyle._( - indicatorColor: other.indicatorColor ?? indicatorColor, - backgroundColor: other.backgroundColor ?? backgroundColor, - backgroundGradient: other.backgroundGradient ?? - (other.backgroundColor == null ? null : backgroundGradient), - borderColor: other.borderColor ?? borderColor, - borderRadius: other.borderRadius ?? borderRadius, - indicatorBorderRadius: other.indicatorBorderRadius ?? - other.borderRadius ?? - indicatorBorderRadius ?? - borderRadius, - indicatorBorder: other.indicatorBorder ?? indicatorBorder, - indicatorBoxShadow: other.indicatorBoxShadow ?? indicatorBoxShadow, - boxShadow: other.boxShadow ?? boxShadow, - ); - - static ToggleStyle _lerp(ToggleStyle style1, ToggleStyle style2, double t) => - ToggleStyle._( - indicatorColor: - Color.lerp(style1.indicatorColor, style2.indicatorColor, t), - backgroundColor: - Color.lerp(style1.backgroundColor, style2.backgroundColor, t), - backgroundGradient: Gradient.lerp( - style1.backgroundGradient ?? style1.backgroundColor?.toGradient(), - style2.backgroundGradient ?? style2.backgroundColor?.toGradient(), - t, - ), - borderColor: Color.lerp(style1.borderColor, style2.borderColor, t), - borderRadius: BorderRadiusGeometry.lerp( - style1.borderRadius, - style2.borderRadius, - t, - ), - indicatorBorderRadius: BorderRadiusGeometry.lerp( - style1.indicatorBorderRadius ?? style1.borderRadius, - style2.indicatorBorderRadius ?? style2.borderRadius, - t, - ), - indicatorBorder: BoxBorder.lerp( - style1.indicatorBorder, - style2.indicatorBorder, - t, - ), - indicatorBoxShadow: BoxShadow.lerpList( - style1.indicatorBoxShadow, - style2.indicatorBoxShadow, - t, - ), - boxShadow: BoxShadow.lerpList( - style1.boxShadow, - style2.boxShadow, - t, - ), - ); -} - /// Specifies when an value should be animated. enum AnimationType { /// Starts an animation if an item is selected. @@ -239,7 +123,7 @@ class AnimatedToggleSwitch extends StatelessWidget { final Size indicatorSize; /// Callback for selecting a new value. The new [current] should be set here. - final Function(T)? onChanged; + final ChangeCallback? onChanged; /// Width of the border of the switch. For deactivating please set this to [0.0]. final double borderWidth; @@ -279,7 +163,7 @@ class AnimatedToggleSwitch extends StatelessWidget { final AnimationType indicatorAnimationType; /// Callback for tapping anywhere on the widget. - final Function()? onTap; + final TapCallback? onTap; final IconArrangement _iconArrangement; @@ -369,6 +253,28 @@ class AnimatedToggleSwitch extends StatelessWidget { /// This builder is supported by [IconArrangement.row] only. final CustomSeparatorBuilder? customSeparatorBuilder; + /// Indicates if the switch is active. + /// + /// Please use [inactiveOpacity] for changing the opacity in inactive state. + /// + /// For controlling the [AnimatedOpacity] you can use [inactiveOpacityCurve] and [inactiveOpacityDuration]. + final bool active; + + /// Opacity of the switch when [active] is set to [false]. + /// + /// Please set this to [1.0] for deactivating. + final double inactiveOpacity; + + /// [Curve] of the animation when getting inactive. + /// + /// For deactivating this animation please set [inactiveOpacity] to [1.0]. + final Curve inactiveOpacityCurve; + + /// [Duration] of the animation when getting inactive. + /// + /// For deactivating this animation please set [inactiveOpacity] to [1.0]. + final Duration inactiveOpacityDuration; + /// Constructor of AnimatedToggleSwitch with all possible settings. /// /// Consider using [CustomAnimatedToggleSwitch] for maximum customizability. @@ -415,6 +321,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : _iconArrangement = IconArrangement.row, assert(styleBuilder == null || customStyleBuilder == null), super(key: key); @@ -468,6 +378,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : animatedIconBuilder = _iconSizeBuilder( iconBuilder, customIconBuilder, iconSize, selectedIconSize), _iconArrangement = IconArrangement.row, @@ -524,6 +438,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : indicatorSize = indicatorSize * (height - 2 * borderWidth), dif = dif * (height - 2 * borderWidth), animatedIconBuilder = _iconSizeBuilder( @@ -610,6 +528,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : dif = dif * (height - 2 * borderWidth), indicatorSize = indicatorSize * (height - 2 * borderWidth), _iconArrangement = IconArrangement.row, @@ -669,6 +591,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : iconAnimationCurve = Curves.linear, dif = dif * (height - 2 * borderWidth), iconAnimationDuration = Duration.zero, @@ -741,6 +667,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.separatorBuilder, this.customSeparatorBuilder, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : iconAnimationCurve = Curves.linear, iconAnimationDuration = Duration.zero, selectedIconOpacity = iconOpacity, @@ -887,6 +817,10 @@ class AnimatedToggleSwitch extends StatelessWidget { this.loadingAnimationCurve, ForegroundIndicatorTransitionType transitionType = ForegroundIndicatorTransitionType.rolling, + this.active = true, + this.inactiveOpacity = 0.5, + this.inactiveOpacityCurve = Curves.easeInOut, + this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : assert(clipAnimation || opacityAnimation), iconOpacity = 1.0, selectedIconOpacity = 1.0, @@ -922,7 +856,7 @@ class AnimatedToggleSwitch extends StatelessWidget { super(key: key); static Function() _dualOnTap( - Function(T)? onChanged, List values, T? current) { + ChangeCallback? onChanged, List values, T? current) { return () => onChanged?.call(values.firstWhere((element) => element != current)); } @@ -982,8 +916,8 @@ class AnimatedToggleSwitch extends StatelessWidget { }; } - static Widget _defaultLoadingIconBuilder( - BuildContext context, dynamic properties) => + static Widget _defaultLoadingIconBuilder(BuildContext context, + DetailedGlobalToggleProperties properties) => const _MyLoading(); ToggleStyle? _styleBuilder(BuildContext context, @@ -1057,42 +991,52 @@ class AnimatedToggleSwitch extends StatelessWidget { iconBuilder: (context, local, global) => _animatedOpacityIcon( _animatedSizeIcon(context, local, global), local.value == current), padding: EdgeInsets.all(borderWidth), + active: false, wrapperBuilder: (context, global, child) { - //TODO: extract this method to separate widget - return _animationTypeBuilder( - context, - styleAnimationType, - (local) => style._merge(_styleBuilder(context, local, global)), - ToggleStyle._lerp, - (style) => DecoratedBox( - decoration: BoxDecoration( - color: style.backgroundColor, - gradient: style.backgroundGradient, - borderRadius: style.borderRadius, - boxShadow: style.boxShadow, - ), - child: DecoratedBox( - position: DecorationPosition.foreground, + return _ConditionalWrapper( + condition: inactiveOpacity < 1.0, + wrapper: (context, child) => AnimatedOpacity( + opacity: global.active ? 1.0 : inactiveOpacity, + duration: inactiveOpacityDuration, + curve: inactiveOpacityCurve, + child: child, + ), + child: _animationTypeBuilder( + context, + styleAnimationType, + (local) => style._merge(_styleBuilder(context, local, global)), + ToggleStyle._lerp, + (style) => DecoratedBox( decoration: BoxDecoration( - border: borderWidth <= 0.0 - ? null - : Border.all( - color: style.borderColor!, - width: borderWidth, - ), + color: style.backgroundColor, + gradient: style.backgroundGradient, borderRadius: style.borderRadius, + boxShadow: style.boxShadow, ), - child: ClipRRect( - borderRadius: style.borderRadius, - child: child, + child: DecoratedBox( + position: DecorationPosition.foreground, + decoration: BoxDecoration( + border: borderWidth <= 0.0 + ? null + : Border.all( + color: style.borderColor!, + width: borderWidth, + ), + borderRadius: style.borderRadius, + ), + child: ClipRRect( + borderRadius: style.borderRadius, + child: child, + ), ), ), + global, ), - global, ); }); } + //TODO: extract this method to separate widget Widget _animationTypeBuilder( BuildContext context, AnimationType animationType, diff --git a/lib/src/widgets/conditional_wrapper.dart b/lib/src/widgets/conditional_wrapper.dart new file mode 100644 index 0000000..d2f7628 --- /dev/null +++ b/lib/src/widgets/conditional_wrapper.dart @@ -0,0 +1,38 @@ +part of 'package:animated_toggle_switch/animated_toggle_switch.dart'; + +class _ConditionalWrapper extends StatefulWidget { + final Widget Function(BuildContext context, Widget child) wrapper; + final bool condition; + final Widget child; + + const _ConditionalWrapper({ + required this.wrapper, + required this.condition, + required this.child, + }); + + @override + State<_ConditionalWrapper> createState() => _ConditionalWrapperState(); +} + +class _ConditionalWrapperState extends State<_ConditionalWrapper> { + final _childKey = GlobalKey(); + + @override + Widget build(BuildContext context) { + final child = _EmptyWidget(key: _childKey, child: widget.child); + if(widget.condition) return widget.wrapper(context, child); + return child; + } +} + +class _EmptyWidget extends StatelessWidget { + final Widget child; + + const _EmptyWidget({super.key, required this.child}); + + @override + Widget build(BuildContext context) { + return child; + } +} diff --git a/lib/src/widgets/custom_animated_toggle_switch.dart b/lib/src/widgets/custom_animated_toggle_switch.dart index 7dea02e..98f89e3 100644 --- a/lib/src/widgets/custom_animated_toggle_switch.dart +++ b/lib/src/widgets/custom_animated_toggle_switch.dart @@ -24,6 +24,10 @@ typedef CustomSeparatorBuilder = Widget Function(BuildContext context, typedef IndicatorAppearingBuilder = Widget Function( BuildContext context, double value, Widget indicator); +typedef ChangeCallback = FutureOr Function(T value); + +typedef TapCallback = FutureOr Function(); + enum ToggleMode { animating, dragged, none } enum FittingMode { none, preventHorizontalOverlapping } @@ -109,11 +113,12 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { final Size indicatorSize; /// Callback for selecting a new value. The new [current] should be set here. - final Function(T)? onChanged; + final ChangeCallback? onChanged; /// Space between the "indicator rooms" of the adjacent icons. final double dif; + /// Builder for divider or other separators between the icons. /// Builder for divider or other separators between the icons. /// /// The available width is specified by [dif]. @@ -122,7 +127,7 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { final CustomSeparatorBuilder? separatorBuilder; /// Callback for tapping anywhere on the widget. - final Function()? onTap; + final TapCallback? onTap; /// Indicates if [onChanged] is called when an icon is tapped. /// If [false] the user can change the value only by dragging the indicator. @@ -188,7 +193,7 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { final bool allowUnlistedValues; /// Indicates if the switch is active. - final bool active = true; + final bool active; const CustomAnimatedToggleSwitch({ Key? key, @@ -226,6 +231,7 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { _defaultIndicatorAppearingAnimationDuration, this.indicatorAppearingCurve = _defaultIndicatorAppearingAnimationCurve, this.allowUnlistedValues = false, + this.active = true, }) : assert(foregroundIndicatorBuilder != null || backgroundIndicatorBuilder != null), assert(separatorBuilder == null || @@ -344,7 +350,7 @@ class _CustomAnimatedToggleSwitchState void _onChanged(T value) { if (!_isActive) return; - dynamic result = widget.onChanged?.call(value); + final result = widget.onChanged?.call(value); if (result is Future && widget.loading == null) { _loading(true); result.whenComplete(() => _loading(false)); @@ -353,7 +359,7 @@ class _CustomAnimatedToggleSwitchState void _onTap() { if (!_isActive) return; - dynamic result = widget.onTap?.call(); + final result = widget.onTap?.call(); if (result is Future && widget.loading == null) { _loading(true); result.whenComplete(() => _loading(false)); @@ -440,6 +446,7 @@ class _CustomAnimatedToggleSwitchState textDirection: textDirection, mode: _animationInfo.toggleMode, loadingAnimationValue: loadingValue, + active: widget.active, ); Widget child = Padding( padding: widget.padding, @@ -543,6 +550,7 @@ class _CustomAnimatedToggleSwitchState textDirection: textDirection, mode: _animationInfo.toggleMode, loadingAnimationValue: loadingValue, + active: widget.active, ); List stack = [ diff --git a/pubspec.yaml b/pubspec.yaml index 4bbd98f..35a3c6b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: animated_toggle_switch description: Fully customizable, draggable and animated switch with multiple choices and smooth loading animation. It has prebuilt constructors for rolling and size animations. -version: 0.8.0-beta.0 +version: 0.8.0-beta.1 repository: https://github.com/SplashByte/animated_toggle_switch issue_tracker: https://github.com/SplashByte/animated_toggle_switch/issues @@ -25,5 +25,5 @@ funding: - https://ko-fi.com/splashbyte screenshots: - - description: 'This image shows three example usages of AnimatedToggleSwitch.rolling().' + - description: 'This image shows three example usages of AnimatedToggleSwitch.' path: screenshots/preview.webp From 0d7fec2685a0375a1716fe1383e7eb58fbd6bf14 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 12:54:46 +0200 Subject: [PATCH 4/9] dart format --- example/pubspec.lock | 2 +- lib/src/widgets/conditional_wrapper.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index 9d54bbb..dd466fc 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: path: ".." relative: true source: path - version: "0.8.0-beta.0" + version: "0.8.0-beta.1" async: dependency: transitive description: diff --git a/lib/src/widgets/conditional_wrapper.dart b/lib/src/widgets/conditional_wrapper.dart index d2f7628..0c38c2b 100644 --- a/lib/src/widgets/conditional_wrapper.dart +++ b/lib/src/widgets/conditional_wrapper.dart @@ -21,7 +21,7 @@ class _ConditionalWrapperState extends State<_ConditionalWrapper> { @override Widget build(BuildContext context) { final child = _EmptyWidget(key: _childKey, child: widget.child); - if(widget.condition) return widget.wrapper(context, child); + if (widget.condition) return widget.wrapper(context, child); return child; } } From f2287c8c7f2041abc9ae425ca2b3ab36a812811b Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 13:23:42 +0200 Subject: [PATCH 5/9] introduces ToggleCursors --- CHANGELOG.md | 1 + example/lib/main.dart | 3 +- lib/animated_toggle_switch.dart | 1 + lib/src/cursors.dart | 48 +++++++++++++++ lib/src/widgets/animated_toggle_switch.dart | 58 ++++--------------- .../custom_animated_toggle_switch.dart | 35 ++++------- lib/src/widgets/drag_region.dart | 4 +- test/gesture_test.dart | 3 + test/helper.dart | 40 ++++++------- 9 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 lib/src/cursors.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ff44d9..a4c6778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - closes [#30](https://github.com/splashbyte/animated_toggle_switch/issues/30) - minor improvements - BREAKING: changes `separatorBuilder` parameters +- BREAKING: moves all cursor parameters to `cursors` ## 0.8.0-beta.0 (2023-07-29) - adds tests for all `AnimatedToggleSwitch` constructors diff --git a/example/lib/main.dart b/example/lib/main.dart index aaf85fd..f4c229a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -307,6 +307,7 @@ class _MyHomePageState extends State { ), SizedBox(height: 16.0), AnimatedToggleSwitch.rolling( + active: false, current: value, values: const [0, 1, 2, 3], onChanged: (i) { @@ -424,7 +425,7 @@ class _MyHomePageState extends State { iconBuilder: (context, local, global) { return const SizedBox(); }, - defaultCursor: SystemMouseCursors.click, + cursors: ToggleCursors(defaultCursor: SystemMouseCursors.click), onTap: () => setState(() => positive = !positive), iconsTappable: false, wrapperBuilder: (context, global, child) { diff --git a/lib/animated_toggle_switch.dart b/lib/animated_toggle_switch.dart index c7bd7ac..7b7c20b 100644 --- a/lib/animated_toggle_switch.dart +++ b/lib/animated_toggle_switch.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; part 'src/properties.dart'; part 'src/style.dart'; +part 'src/cursors.dart'; part 'src/widgets/animated_toggle_switch.dart'; part 'src/widgets/custom_animated_toggle_switch.dart'; part 'src/widgets/drag_region.dart'; diff --git a/lib/src/cursors.dart b/lib/src/cursors.dart new file mode 100644 index 0000000..e611057 --- /dev/null +++ b/lib/src/cursors.dart @@ -0,0 +1,48 @@ +part of 'package:animated_toggle_switch/animated_toggle_switch.dart'; + +class ToggleCursors { + /// [MouseCursor] to show when not hovering an indicator. + /// + /// Defaults to [SystemMouseCursors.click] if [iconsTappable] is [true] + /// and to [MouseCursor.defer] otherwise. + final MouseCursor? defaultCursor; + + /// [MouseCursor] to show when grabbing the indicators. + final MouseCursor draggingCursor; + + /// [MouseCursor] to show when hovering the indicators. + final MouseCursor dragCursor; + + /// [MouseCursor] to show during loading. + final MouseCursor loadingCursor; + + /// [MouseCursor] to show when [active] is set to [false]. + final MouseCursor inactiveCursor; + + const ToggleCursors({ + this.defaultCursor, + this.draggingCursor = SystemMouseCursors.grabbing, + this.dragCursor = SystemMouseCursors.grab, + this.loadingCursor = MouseCursor.defer, + this.inactiveCursor = SystemMouseCursors.forbidden, + }); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is ToggleCursors && + runtimeType == other.runtimeType && + defaultCursor == other.defaultCursor && + draggingCursor == other.draggingCursor && + dragCursor == other.dragCursor && + loadingCursor == other.loadingCursor && + inactiveCursor == other.inactiveCursor; + + @override + int get hashCode => + defaultCursor.hashCode ^ + draggingCursor.hashCode ^ + dragCursor.hashCode ^ + loadingCursor.hashCode ^ + inactiveCursor.hashCode; +} diff --git a/lib/src/widgets/animated_toggle_switch.dart b/lib/src/widgets/animated_toggle_switch.dart index 29abd95..7a85fc6 100644 --- a/lib/src/widgets/animated_toggle_switch.dart +++ b/lib/src/widgets/animated_toggle_switch.dart @@ -167,20 +167,8 @@ class AnimatedToggleSwitch extends StatelessWidget { final IconArrangement _iconArrangement; - /// [MouseCursor] to show when not hovering an indicator. - /// - /// Defaults to [SystemMouseCursors.click] if [iconsTappable] is [true] - /// and to [MouseCursor.defer] otherwise. - final MouseCursor? defaultCursor; - - /// [MouseCursor] to show when grabbing the indicators. - final MouseCursor draggingCursor; - - /// [MouseCursor] to show when hovering the indicators. - final MouseCursor dragCursor; - - /// [MouseCursor] to show during loading. - final MouseCursor loadingCursor; + /// The [MouseCursor] settings for this switch. + final ToggleCursors cursors; /// The [FittingMode] of the switch. /// @@ -306,10 +294,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -363,10 +348,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -423,10 +405,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -513,10 +492,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -574,10 +550,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -650,10 +623,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.minTouchTargetSize = 48.0, this.textDirection, this.iconsTappable = true, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loadingIconBuilder = _defaultLoadingIconBuilder, this.loading, this.loadingAnimationDuration, @@ -803,10 +773,7 @@ class AnimatedToggleSwitch extends StatelessWidget { Function()? onTap, this.minTouchTargetSize = 48.0, this.textDirection, - this.defaultCursor = SystemMouseCursors.click, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), EdgeInsetsGeometry textMargin = const EdgeInsets.symmetric(horizontal: 8.0), Offset animationOffset = const Offset(20.0, 0), bool clipAnimation = true, @@ -963,10 +930,7 @@ class AnimatedToggleSwitch extends StatelessWidget { indicatorSize: indicatorSize, iconArrangement: _iconArrangement, iconsTappable: iconsTappable, - defaultCursor: defaultCursor, - dragCursor: dragCursor, - draggingCursor: draggingCursor, - loadingCursor: loadingCursor, + cursors: cursors, minTouchTargetSize: minTouchTargetSize, textDirection: textDirection, loading: loading, @@ -991,7 +955,7 @@ class AnimatedToggleSwitch extends StatelessWidget { iconBuilder: (context, local, global) => _animatedOpacityIcon( _animatedSizeIcon(context, local, global), local.value == current), padding: EdgeInsets.all(borderWidth), - active: false, + active: active, wrapperBuilder: (context, global, child) { return _ConditionalWrapper( condition: inactiveOpacity < 1.0, diff --git a/lib/src/widgets/custom_animated_toggle_switch.dart b/lib/src/widgets/custom_animated_toggle_switch.dart index 98f89e3..0e298a3 100644 --- a/lib/src/widgets/custom_animated_toggle_switch.dart +++ b/lib/src/widgets/custom_animated_toggle_switch.dart @@ -165,20 +165,8 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { /// If set to [null], the [TextDirection] is taken from the [BuildContext]. final TextDirection? textDirection; - /// [MouseCursor] to show when not hovering an indicator. - /// - /// Defaults to [SystemMouseCursors.click] if [iconsTappable] is [true] - /// and to [MouseCursor.defer] otherwise. - final MouseCursor? defaultCursor; - - /// [MouseCursor] to show when grabbing the indicators. - final MouseCursor draggingCursor; - - /// [MouseCursor] to show when hovering the indicators. - final MouseCursor dragCursor; - - /// [MouseCursor] to show during loading. - final MouseCursor loadingCursor; + /// The [MouseCursor] settings for this switch. + final ToggleCursors cursors; /// Indicates if the switch is currently loading. /// @@ -220,10 +208,7 @@ class CustomAnimatedToggleSwitch extends StatefulWidget { this.dragStartDuration = const Duration(milliseconds: 200), this.dragStartCurve = Curves.easeInOutCirc, this.textDirection, - this.defaultCursor, - this.draggingCursor = SystemMouseCursors.grabbing, - this.dragCursor = SystemMouseCursors.grab, - this.loadingCursor = MouseCursor.defer, + this.cursors = const ToggleCursors(), this.loading, this.loadingAnimationDuration, this.loadingAnimationCurve, @@ -593,15 +578,17 @@ class _CustomAnimatedToggleSwitchState // manual check if cursor is above indicator // to make sure that GestureDetector and MouseRegion match. // TODO: one widget for DragRegion and GestureDetector to avoid redundancy - child: DragRegion( + child: _DragRegion( dragging: _animationInfo.toggleMode == ToggleMode.dragged, - draggingCursor: widget.draggingCursor, - dragCursor: widget.dragCursor, + draggingCursor: widget.cursors.draggingCursor, + dragCursor: widget.cursors.dragCursor, hoverCheck: isHoveringIndicator, - defaultCursor: _animationInfo.loading - ? widget.loadingCursor - : (widget.defaultCursor ?? + defaultCursor: !_isActive + ? (_animationInfo.loading + ? widget.cursors.loadingCursor + : widget.cursors.inactiveCursor) + : (widget.cursors.defaultCursor ?? (widget.iconsTappable ? SystemMouseCursors.click : MouseCursor.defer)), diff --git a/lib/src/widgets/drag_region.dart b/lib/src/widgets/drag_region.dart index 5149ad3..3ef982c 100644 --- a/lib/src/widgets/drag_region.dart +++ b/lib/src/widgets/drag_region.dart @@ -62,7 +62,7 @@ class _HoverRegionState extends State<_HoverRegion> { } } -class DragRegion extends StatelessWidget { +class _DragRegion extends StatelessWidget { final bool dragging; final Widget child; final bool Function(Offset offset) hoverCheck; @@ -70,7 +70,7 @@ class DragRegion extends StatelessWidget { final MouseCursor dragCursor; final MouseCursor draggingCursor; - const DragRegion({ + const _DragRegion({ Key? key, this.dragging = false, required this.child, diff --git a/test/gesture_test.dart b/test/gesture_test.dart index 1ca690a..5e70886 100644 --- a/test/gesture_test.dart +++ b/test/gesture_test.dart @@ -1,4 +1,5 @@ import 'package:animated_toggle_switch/animated_toggle_switch.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; @@ -25,6 +26,8 @@ void main() { final currentFinder = find.byKey(iconKey(current)); final nextFinder = find.byKey(iconKey(next)); + debugDumpApp(); + await tester.tap(currentFinder, warnIfMissed: false); verify(() => tapFunction()).called(1); diff --git a/test/helper.dart b/test/helper.dart index 9b2a562..bc6637d 100644 --- a/test/helper.dart +++ b/test/helper.dart @@ -63,8 +63,8 @@ typedef SwitchBuilder = AnimatedToggleSwitch Function({ required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues, ToggleStyle? style, @@ -79,8 +79,8 @@ typedef SimpleSwitchBuilder = AnimatedToggleSwitch Function({ required T current, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues, ToggleStyle? style, @@ -106,8 +106,8 @@ void defaultTestAllSwitches( required int current, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(int)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -145,8 +145,8 @@ void defaultTestAllSwitches( required int current, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(int)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -196,8 +196,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -236,8 +236,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -275,8 +275,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -315,8 +315,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -354,8 +354,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, @@ -394,8 +394,8 @@ void testAllSwitches( required List values, TestIconBuilder? iconBuilder, TextDirection? textDirection, - Function(T)? onChanged, - Function()? onTap, + ChangeCallback? onChanged, + TapCallback? onTap, bool? loading, bool allowUnlistedValues = false, ToggleStyle? style, From 9d7a9942a791c31a1a78848e649dd9ee1adb4cd9 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 13:28:19 +0200 Subject: [PATCH 6/9] removes cursors from coverage --- lib/src/cursors.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/src/cursors.dart b/lib/src/cursors.dart index e611057..1449765 100644 --- a/lib/src/cursors.dart +++ b/lib/src/cursors.dart @@ -1,3 +1,4 @@ +// coverage:ignore-file part of 'package:animated_toggle_switch/animated_toggle_switch.dart'; class ToggleCursors { @@ -27,6 +28,13 @@ class ToggleCursors { this.inactiveCursor = SystemMouseCursors.forbidden, }); + const ToggleCursors.all(MouseCursor cursor) + : defaultCursor = cursor, + draggingCursor = cursor, + dragCursor = cursor, + loadingCursor = cursor, + inactiveCursor = cursor; + @override bool operator ==(Object other) => identical(this, other) || From 5b0ccd7c1d884f96617f5628e418cca5b9deed82 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 13:31:18 +0200 Subject: [PATCH 7/9] Update style.dart --- lib/src/style.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/style.dart b/lib/src/style.dart index 4b6e885..77070c1 100644 --- a/lib/src/style.dart +++ b/lib/src/style.dart @@ -115,6 +115,8 @@ class ToggleStyle { ), ); + // coverage:ignore-start + @override bool operator ==(Object other) => identical(this, other) || @@ -141,4 +143,6 @@ class ToggleStyle { indicatorBorder.hashCode ^ indicatorBoxShadow.hashCode ^ boxShadow.hashCode; + + // coverage:ignore-end } From 1f60a1cd6a3e4d7518c7b944f0cac80d8d15d0b1 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 13:33:21 +0200 Subject: [PATCH 8/9] dart format --- lib/src/style.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/style.dart b/lib/src/style.dart index 77070c1..7b795bf 100644 --- a/lib/src/style.dart +++ b/lib/src/style.dart @@ -143,6 +143,6 @@ class ToggleStyle { indicatorBorder.hashCode ^ indicatorBoxShadow.hashCode ^ boxShadow.hashCode; - + // coverage:ignore-end } From cd8ad70b24ebe0cd38dee8484dad5ea0732fb008 Mon Sep 17 00:00:00 2001 From: MaddinMade Date: Mon, 31 Jul 2023 13:37:18 +0200 Subject: [PATCH 9/9] updates default inactiveOpacity --- lib/src/widgets/animated_toggle_switch.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/src/widgets/animated_toggle_switch.dart b/lib/src/widgets/animated_toggle_switch.dart index 7a85fc6..a19eb61 100644 --- a/lib/src/widgets/animated_toggle_switch.dart +++ b/lib/src/widgets/animated_toggle_switch.dart @@ -307,7 +307,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : _iconArrangement = IconArrangement.row, @@ -361,7 +361,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : animatedIconBuilder = _iconSizeBuilder( @@ -418,7 +418,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : indicatorSize = indicatorSize * (height - 2 * borderWidth), @@ -505,7 +505,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : dif = dif * (height - 2 * borderWidth), @@ -565,7 +565,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : iconAnimationCurve = Curves.linear, @@ -638,7 +638,7 @@ class AnimatedToggleSwitch extends StatelessWidget { this.separatorBuilder, this.customSeparatorBuilder, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : iconAnimationCurve = Curves.linear, @@ -785,7 +785,7 @@ class AnimatedToggleSwitch extends StatelessWidget { ForegroundIndicatorTransitionType transitionType = ForegroundIndicatorTransitionType.rolling, this.active = true, - this.inactiveOpacity = 0.5, + this.inactiveOpacity = 0.6, this.inactiveOpacityCurve = Curves.easeInOut, this.inactiveOpacityDuration = const Duration(milliseconds: 350), }) : assert(clipAnimation || opacityAnimation),