From 597b2003e4ce8f9ab6c7188354636f328c29a6ee Mon Sep 17 00:00:00 2001 From: scheinpablo Date: Tue, 29 Dec 2020 14:42:18 -0300 Subject: [PATCH 1/3] Update value on icon's tap --- lib/src/stepper.dart | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/src/stepper.dart b/lib/src/stepper.dart index ea89e2d..ea6c440 100644 --- a/lib/src/stepper.dart +++ b/lib/src/stepper.dart @@ -98,13 +98,26 @@ class _Stepper2State extends State Positioned( left: widget.direction == Axis.horizontal ? 10.0 : null, bottom: widget.direction == Axis.horizontal ? null : 10.0, - child: - Icon(Icons.remove, size: 40.0, color: widget.buttonsColor), + child: GestureDetector( + onTap: () { + setState(() { + _value--; + }); + }, + child: Icon(Icons.remove, + size: 40.0, color: widget.buttonsColor)), ), Positioned( right: widget.direction == Axis.horizontal ? 10.0 : null, top: widget.direction == Axis.horizontal ? null : 10.0, - child: Icon(Icons.add, size: 40.0, color: widget.buttonsColor), + child: GestureDetector( + onTap: () { + setState(() { + _value++; + }); + }, + child: Icon(Icons.add, + size: 40.0, color: widget.buttonsColor)), ), GestureDetector( onHorizontalDragStart: _onPanStart, From edbfebd19b14006a338ad76688545f7a81af9f00 Mon Sep 17 00:00:00 2001 From: scheinpablo Date: Thu, 18 Mar 2021 13:42:51 -0300 Subject: [PATCH 2/3] Added 'signed' option. --- lib/src/stepper.dart | 46 ++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/src/stepper.dart b/lib/src/stepper.dart index ea6c440..9cef333 100644 --- a/lib/src/stepper.dart +++ b/lib/src/stepper.dart @@ -5,16 +5,17 @@ import 'package:flutter/physics.dart'; /// from [Nikolay Kuchkarov](https://dribbble.com/shots/3368130-Stepper-Touch). /// i extended the functionality to be more useful in real world applications class StepperTouch extends StatefulWidget { - const StepperTouch({ - Key key, - this.initialValue, - this.onChanged, - this.direction = Axis.horizontal, - this.withSpring = true, - this.counterColor = const Color(0xFF6D72FF), - this.dragButtonColor = Colors.white, - this.buttonsColor = Colors.white, - }) : super(key: key); + const StepperTouch( + {Key key, + this.initialValue, + this.onChanged, + this.direction = Axis.horizontal, + this.withSpring = true, + this.counterColor = const Color(0xFF6D72FF), + this.dragButtonColor = Colors.white, + this.buttonsColor = Colors.white, + this.signed = true}) + : super(key: key); /// the orientation of the stepper its horizontal or vertical. final Axis direction; @@ -33,6 +34,10 @@ class StepperTouch extends StatefulWidget { final Color dragButtonColor; final Color buttonsColor; + /// if you want to accept negative values. + /// defaults to true + final bool signed; + @override _Stepper2State createState() => _Stepper2State(); } @@ -100,9 +105,9 @@ class _Stepper2State extends State bottom: widget.direction == Axis.horizontal ? null : 10.0, child: GestureDetector( onTap: () { - setState(() { + if (widget.signed || _value > 0) { _value--; - }); + } }, child: Icon(Icons.remove, size: 40.0, color: widget.buttonsColor)), @@ -181,10 +186,23 @@ class _Stepper2State extends State bool isHor = widget.direction == Axis.horizontal; bool changed = false; if (_controller.value <= -0.20) { - setState(() => isHor ? _value-- : _value++); + setState(() { + if (isHor) { + if (widget.signed || _value > 0) { + _value--; + } + } else + _value++; + }); changed = true; } else if (_controller.value >= 0.20) { - setState(() => isHor ? _value++ : _value--); + setState(() { + if (isHor) { + _value++; + } else if (widget.signed || _value > 0) { + _value--; + } + }); changed = true; } if (widget.withSpring) { From 042f449e680b7cf592a0c71eb89e3f7ccd405421 Mon Sep 17 00:00:00 2001 From: scheinpablo Date: Thu, 18 Mar 2021 13:48:18 -0300 Subject: [PATCH 3/3] Solved warning. Improved sintax. --- lib/src/stepper.dart | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/src/stepper.dart b/lib/src/stepper.dart index 9cef333..c806bd9 100644 --- a/lib/src/stepper.dart +++ b/lib/src/stepper.dart @@ -14,7 +14,7 @@ class StepperTouch extends StatefulWidget { this.counterColor = const Color(0xFF6D72FF), this.dragButtonColor = Colors.white, this.buttonsColor = Colors.white, - this.signed = true}) + this.signed = false}) : super(key: key); /// the orientation of the stepper its horizontal or vertical. @@ -29,15 +29,10 @@ class StepperTouch extends StatefulWidget { /// if you want a springSimulation to happens the the user let go the stepper /// defaults to true final bool withSpring; - final Color counterColor; final Color dragButtonColor; final Color buttonsColor; - - /// if you want to accept negative values. - /// defaults to true final bool signed; - @override _Stepper2State createState() => _Stepper2State(); } @@ -45,11 +40,10 @@ class StepperTouch extends StatefulWidget { class _Stepper2State extends State with SingleTickerProviderStateMixin { AnimationController _controller; - Animation _animation; + Animation _animation; int _value; double _startAnimationPosX; double _startAnimationPosY; - @override void initState() { super.initState(); @@ -58,12 +52,11 @@ class _Stepper2State extends State AnimationController(vsync: this, lowerBound: -0.5, upperBound: 0.5); _controller.value = 0.0; _controller.addListener(() {}); - if (widget.direction == Axis.horizontal) { - _animation = Tween(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0)) + _animation = Tween(begin: const Offset(0.0, 0.0), end: const Offset(1.5, 0.0)) .animate(_controller); } else { - _animation = Tween(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5)) + _animation = Tween(begin: const Offset(0.0, 0.0), end: const Offset(0.0, 1.5)) .animate(_controller); } } @@ -75,13 +68,13 @@ class _Stepper2State extends State } @override - void didUpdateWidget(oldWidget) { + void didUpdateWidget(StepperTouch oldWidget) { super.didUpdateWidget(oldWidget); if (widget.direction == Axis.horizontal) { - _animation = Tween(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0)) + _animation = Tween(begin: const Offset(0.0, 0.0), end: const Offset(1.5, 0.0)) .animate(_controller); } else { - _animation = Tween(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5)) + _animation = Tween(begin: const Offset(0.0, 0.0), end: const Offset(0.0, 1.5)) .animate(_controller); } } @@ -105,9 +98,11 @@ class _Stepper2State extends State bottom: widget.direction == Axis.horizontal ? null : 10.0, child: GestureDetector( onTap: () { - if (widget.signed || _value > 0) { - _value--; - } + setState(() { + if (widget.signed || _value > 0) { + _value--; + } + }); }, child: Icon(Icons.remove, size: 40.0, color: widget.buttonsColor)), @@ -161,8 +156,8 @@ class _Stepper2State extends State } double offsetFromGlobalPos(Offset globalPosition) { - RenderBox box = context.findRenderObject() as RenderBox; - Offset local = box.globalToLocal(globalPosition); + final RenderBox box = context.findRenderObject() as RenderBox; + final Offset local = box.globalToLocal(globalPosition); _startAnimationPosX = ((local.dx * 0.75) / box.size.width) - 0.4; _startAnimationPosY = ((local.dy * 0.75) / box.size.height) - 0.4; if (widget.direction == Axis.horizontal) { @@ -183,7 +178,7 @@ class _Stepper2State extends State void _onPanEnd(DragEndDetails details) { _controller.stop(); - bool isHor = widget.direction == Axis.horizontal; + final bool isHor = widget.direction == Axis.horizontal; bool changed = false; if (_controller.value <= -0.20) { setState(() { @@ -207,7 +202,7 @@ class _Stepper2State extends State } if (widget.withSpring) { final SpringDescription _kDefaultSpring = - new SpringDescription.withDampingRatio( + SpringDescription.withDampingRatio( mass: 0.9, stiffness: 250.0, ratio: 0.6, @@ -221,9 +216,8 @@ class _Stepper2State extends State } } else { _controller.animateTo(0.0, - curve: Curves.bounceOut, duration: Duration(milliseconds: 500)); + curve: Curves.bounceOut, duration: const Duration(milliseconds: 500)); } - if (changed && widget.onChanged != null) { widget.onChanged(_value); }