diff --git a/CHANGELOG.md b/CHANGELOG.md index f793cb5..41d9e0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.2 [2022-02-25] +- BREAKING: changes parameters of SlideCallback +- optimizes fade animation + ## 0.1.1 [2022-02-21] - fixes README.md diff --git a/README.md b/README.md index 5eeb974..9f48a95 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,11 @@ Easy to use and highly customizable. ```dart ActionSlider.standard( - width: 300.0, child: const Text('Slide to confirm'), - successIcon: const Icon(Icons.check_rounded, color: Colors.white), - onSlide: (loading, success, failure, reset) async { - loading(); //or controller.loading() + onSlide: (controller) async { + controller.loading(); //starts loading animation await Future.delayed(const Duration(seconds: 3)); - success(); //or controller.success() + controller.success(); //starts success animation }, -), +) ``` diff --git a/example/lib/main.dart b/example/lib/main.dart index 88b11ac..7e21b21 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -47,12 +47,12 @@ class _MyHomePageState extends State { ActionSlider.standard( width: 300.0, child: const Text('Slide to confirm'), - onSlide: (loading, success, failure) async { - loading(); //or controller.loading() + onSlide: (controller) async { + controller.loading(); //starts loading animation await Future.delayed(const Duration(seconds: 3)); - success(); //or controller.success() + controller.success(); //starts success animation await Future.delayed(const Duration(seconds: 1)); - _controller.reset(); //for resetting the slider + controller.reset(); //resets the slider }, ), const SizedBox(height: 24.0), @@ -74,12 +74,12 @@ class _MyHomePageState extends State { ), backgroundBuilder: (context, pos, width, height, child) => child!, backgroundBorderRadius: BorderRadius.circular(5.0), - onSlide: (loading, success, failure) async { - loading(); //or controller.loading() + onSlide: (controller) async { + controller.loading(); //or controller.loading() await Future.delayed(const Duration(seconds: 3)); - success(); //or controller.success() + controller.success(); //or controller.success() await Future.delayed(const Duration(seconds: 1)); - _controller.reset(); //for resetting the slider + controller.reset(); //for resetting the slider }, ) ], diff --git a/example/pubspec.lock b/example/pubspec.lock index f804226..49327c4 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: path: ".." relative: true source: path - version: "0.1.0" + version: "0.1.2" async: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index bc12222..1ec02f2 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,5 +1,5 @@ name: example -description: Example for confirmation_slider. +description: Example for action_slider. publish_to: 'none' diff --git a/lib/src/action_slider_widget.dart b/lib/src/action_slider_widget.dart index 754decd..00f15d6 100644 --- a/lib/src/action_slider_widget.dart +++ b/lib/src/action_slider_widget.dart @@ -10,8 +10,7 @@ typedef BackgroundBuilder = Widget Function( BuildContext, double, double, double, Widget?); typedef ForegroundBuilder = Widget Function( BuildContext, double, double, double, Widget?, SliderMode); -typedef SlideCallback = Function(Function() loading, Function() success, - Function() failure, Function() reset); +typedef SlideCallback = Function(ActionSliderController controller); class ActionSliderController extends ValueNotifier { ActionSliderController() : super(SliderMode.standard); @@ -90,7 +89,7 @@ class ActionSlider extends StatefulWidget { ///Callback for sliding completely to the right. ///Here you should call the loading, success and failure methods of the [controller] for controlling the further behaviour/animations of the slider. ///Optionally [onSlide] can be a [SlideCallback] for using the widget without an external controller. - final Function? onSlide; + final SlideCallback? onSlide; ///Controller for controlling the widget from everywhere. final ActionSliderController? controller; @@ -125,10 +124,7 @@ class ActionSlider extends StatefulWidget { offset: Offset(0, 2), ) ], - }) : assert(onSlide == null || - onSlide is SlideCallback || - onSlide is Function()), - super(key: key); + }) : super(key: key); ///Standard constructor for creating a Slider. ///If [customForegroundBuilder] is not null, the values of [successIcon], [failureIcon], [loadingIcon] and [icon] are ignored. @@ -148,7 +144,7 @@ class ActionSlider extends StatefulWidget { double height = 65.0, double circleRadius = 25.0, bool rotating = false, - Function? onSlide, + SlideCallback? onSlide, ActionSliderController? controller, double? width, Duration slideAnimationDuration = const Duration(milliseconds: 250), @@ -275,7 +271,8 @@ class ActionSlider extends StatefulWidget { angle: pos * width / radius, child: icon) : icon); } - throw StateError('This should not happen :('); + throw StateError('For using custom SliderModes you have to ' + 'set customForegroundBuilder!'); }, size: (m1, m2) => m2 == SliderMode.success || m2 == SliderMode.failure), @@ -381,6 +378,10 @@ class _ActionSliderState extends State return LayoutBuilder(builder: (context, constraints) { final maxWidth = min(widget.width ?? double.infinity, constraints.maxWidth); + if (maxWidth == double.infinity) { + throw StateError('The constraints of the ActionSlider ' + 'are unbound and no width is set'); + } final standardWidth = maxWidth - widget.toggleWidth - frame * 2; return AnimatedBuilder( builder: (context, child) { @@ -499,15 +500,6 @@ class _ActionSliderState extends State } void _onSlide() { - if (widget.onSlide == null) return; - if (widget.onSlide is Function()) { - widget.onSlide!(); - } else if (widget.onSlide is SlideCallback) { - widget.onSlide!(_controller.loading, _controller.success, - _controller.failure, _controller.reset); - } else { - throw ArgumentError( - 'onSlide should be null, a Function() or a SlideCallback'); - } + widget.onSlide?.call(_controller); } } diff --git a/lib/src/cross_fade.dart b/lib/src/cross_fade.dart index 547e55d..51178a8 100644 --- a/lib/src/cross_fade.dart +++ b/lib/src/cross_fade.dart @@ -39,7 +39,11 @@ class _CrossFadeState extends State> if (status == AnimationStatus.completed) { if (todo.length <= 1) return; todo.removeAt(0); - if (todo.length > 1) _controller.forward(from: 0.0); + if (todo.length > 1) { + _controller.forward(from: 0.0); + } else { + _controller.value = 0.0; + } } }); @@ -66,8 +70,6 @@ class _CrossFadeState extends State> ), ], ).animate(_controller); - - _controller.forward(from: 0.0); } @override @@ -76,21 +78,14 @@ class _CrossFadeState extends State> super.dispose(); } - Widget get current => Container( - key: _LocalKey(todo.first), child: widget.builder!(context, todo.first)); - - Widget? get next => todo.length > 1 - ? Container( - key: _LocalKey(todo[1]), child: widget.builder!(context, todo[1])) - : null; - @override Widget build(BuildContext context) { if (!widget.equals(widget.current, todo.last)) { - if (todo.length < 3) + if (todo.length < 3) { todo.add(widget.current); - else + } else { todo[todo.length - 1] = widget.current; + } if (!_controller.isAnimating) _controller.forward(from: 0.0); } @@ -100,15 +95,21 @@ class _CrossFadeState extends State> : 1.0, child: Stack( children: [ - if (!_opacityAnimation.isCompleted) - Opacity(opacity: 1 - _opacityAnimation.value, child: current), - Opacity(opacity: _opacityAnimation.value, child: next ?? current) + Opacity( + key: _LocalKey(todo[0]), + opacity: 1 - _opacityAnimation.value, + child: widget.builder!(context, todo[0])), + if (todo.length > 1) + Opacity( + key: _LocalKey(todo[1]), + opacity: _opacityAnimation.value, + child: widget.builder!(context, todo[1])) ], ), ); } } -class _LocalKey extends ValueKey { - const _LocalKey(value) : super(value); +class _LocalKey extends ValueKey { + const _LocalKey(T value) : super(value); } diff --git a/pubspec.yaml b/pubspec.yaml index 93c514a..6911515 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: action_slider description: A slider to confirm actions and provide feedback on the success of these after subsequent loading. -version: 0.1.1 +version: 0.1.2 repository: https://github.com/SplashByte/action_slider environment: