Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions lib/src/stepper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,37 @@ 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.primaryColor = const Color(0xFF6D72FF),
this.textColor = const Color(0xFF6D72FF)})
: super(key: key);

/// the orientation of the stepper its horizontal or vertical.
final Axis direction;

/// the initial value of the stepper
final int initialValue;

///For Color
final primaryColor, textColor;

/// called whenever the value of the stepper changed
final ValueChanged<int> onChanged;

/// 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;

@override
_Stepper2State createState() => _Stepper2State();
}

class _Stepper2State extends State<StepperTouch>
with SingleTickerProviderStateMixin {
class _Stepper2State extends State<StepperTouch> with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation _animation;
int _value;
Expand All @@ -49,17 +46,14 @@ class _Stepper2State extends State<StepperTouch>
void initState() {
super.initState();
_value = widget.initialValue ?? 0;
_controller =
AnimationController(vsync: this, lowerBound: -0.5, upperBound: 0.5);
_controller = AnimationController(vsync: this, lowerBound: -0.5, upperBound: 0.5);
_controller.value = 0.0;
_controller.addListener(() {});

if (widget.direction == Axis.horizontal) {
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0))
.animate(_controller);
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0)).animate(_controller);
} else {
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5))
.animate(_controller);
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5)).animate(_controller);
}
}

Expand All @@ -73,11 +67,9 @@ class _Stepper2State extends State<StepperTouch>
void didUpdateWidget(oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.direction == Axis.horizontal) {
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0))
.animate(_controller);
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(1.5, 0.0)).animate(_controller);
} else {
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5))
.animate(_controller);
_animation = Tween<Offset>(begin: Offset(0.0, 0.0), end: Offset(0.0, 1.5)).animate(_controller);
}
}

Expand All @@ -91,20 +83,19 @@ class _Stepper2State extends State<StepperTouch>
type: MaterialType.canvas,
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(60.0),
color: Colors.white.withOpacity(0.2),
color: widget.primaryColor.withOpacity(0.2),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
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: Icon(Icons.remove, size: 40.0, color: widget.primaryColor),
),
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: Icon(Icons.add, size: 40.0, color: widget.primaryColor),
),
GestureDetector(
onHorizontalDragStart: _onPanStart,
Expand All @@ -113,22 +104,19 @@ class _Stepper2State extends State<StepperTouch>
child: SlideTransition(
position: _animation,
child: Material(
color: widget.dragButtonColor,
color: widget.primaryColor,
shape: const CircleBorder(),
elevation: 5.0,
child: Center(
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 500),
transitionBuilder:
(Widget child, Animation<double> animation) {
return ScaleTransition(
child: child, scale: animation);
transitionBuilder: (Widget child, Animation<double> animation) {
return ScaleTransition(child: child, scale: animation);
},
child: Text(
'$_value',
key: ValueKey<int>(_value),
style: TextStyle(
color: widget.counterColor, fontSize: 56.0),
style: TextStyle(color: widget.textColor, fontSize: 56.0),
),
),
),
Expand Down Expand Up @@ -175,22 +163,18 @@ class _Stepper2State extends State<StepperTouch>
changed = true;
}
if (widget.withSpring) {
final SpringDescription _kDefaultSpring =
new SpringDescription.withDampingRatio(
final SpringDescription _kDefaultSpring = new SpringDescription.withDampingRatio(
mass: 0.9,
stiffness: 250.0,
ratio: 0.6,
);
if (widget.direction == Axis.horizontal) {
_controller.animateWith(
SpringSimulation(_kDefaultSpring, _startAnimationPosX, 0.0, 0.0));
_controller.animateWith(SpringSimulation(_kDefaultSpring, _startAnimationPosX, 0.0, 0.0));
} else {
_controller.animateWith(
SpringSimulation(_kDefaultSpring, _startAnimationPosY, 0.0, 0.0));
_controller.animateWith(SpringSimulation(_kDefaultSpring, _startAnimationPosY, 0.0, 0.0));
}
} else {
_controller.animateTo(0.0,
curve: Curves.bounceOut, duration: Duration(milliseconds: 500));
_controller.animateTo(0.0, curve: Curves.bounceOut, duration: Duration(milliseconds: 500));
}

if (changed && widget.onChanged != null) {
Expand Down