|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:intl/intl.dart'; |
| 3 | + |
| 4 | +class TouchSpin extends StatefulWidget { |
| 5 | + final num value; |
| 6 | + final num min; |
| 7 | + final num max; |
| 8 | + final num step; |
| 9 | + final double iconSize; |
| 10 | + final ValueChanged<num>? onChanged; |
| 11 | + final NumberFormat? displayFormat; |
| 12 | + final Icon subtractIcon; |
| 13 | + final Icon addIcon; |
| 14 | + final EdgeInsetsGeometry iconPadding; |
| 15 | + final TextStyle textStyle; |
| 16 | + final Color? iconActiveColor; |
| 17 | + final Color? iconDisabledColor; |
| 18 | + final bool enabled; |
| 19 | + |
| 20 | + const TouchSpin({ |
| 21 | + Key? key, |
| 22 | + this.value = 1.0, |
| 23 | + this.onChanged, |
| 24 | + this.min = 1.0, |
| 25 | + this.max = 9999999.0, |
| 26 | + this.step = 1.0, |
| 27 | + this.iconSize = 24.0, |
| 28 | + this.displayFormat, |
| 29 | + this.subtractIcon = const Icon(Icons.remove), |
| 30 | + this.addIcon = const Icon(Icons.add), |
| 31 | + this.iconPadding = const EdgeInsets.all(4.0), |
| 32 | + this.textStyle = const TextStyle(fontSize: 24), |
| 33 | + this.iconActiveColor, |
| 34 | + this.iconDisabledColor, |
| 35 | + this.enabled = true, |
| 36 | + }) : super(key: key); |
| 37 | + |
| 38 | + @override |
| 39 | + TouchSpinState createState() => TouchSpinState(); |
| 40 | +} |
| 41 | + |
| 42 | +class TouchSpinState extends State<TouchSpin> { |
| 43 | + late num _value; |
| 44 | + |
| 45 | + bool get minusBtnDisabled => |
| 46 | + _value <= widget.min || |
| 47 | + _value - widget.step < widget.min || |
| 48 | + !widget.enabled; |
| 49 | + |
| 50 | + bool get addBtnDisabled => |
| 51 | + _value >= widget.max || |
| 52 | + _value + widget.step > widget.max || |
| 53 | + !widget.enabled; |
| 54 | + |
| 55 | + @override |
| 56 | + void initState() { |
| 57 | + super.initState(); |
| 58 | + _value = widget.value; |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + void didUpdateWidget(TouchSpin oldWidget) { |
| 63 | + super.didUpdateWidget(oldWidget); |
| 64 | + |
| 65 | + if (oldWidget.value != widget.value) { |
| 66 | + setState(() => _value = widget.value); |
| 67 | + widget.onChanged?.call(widget.value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + Color? _spinButtonColor(bool btnDisabled) => btnDisabled |
| 72 | + ? widget.iconDisabledColor ?? Theme.of(context).disabledColor |
| 73 | + : widget.iconActiveColor ?? Theme.of(context).textTheme.labelLarge?.color; |
| 74 | + |
| 75 | + void _adjustValue(num adjustment) { |
| 76 | + num newVal = _value + adjustment; |
| 77 | + setState(() => _value = newVal); |
| 78 | + widget.onChanged?.call(newVal); |
| 79 | + } |
| 80 | + |
| 81 | + @override |
| 82 | + Widget build(BuildContext context) { |
| 83 | + return Row( |
| 84 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 85 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 86 | + children: <Widget>[ |
| 87 | + IconButton( |
| 88 | + padding: widget.iconPadding, |
| 89 | + iconSize: widget.iconSize, |
| 90 | + color: _spinButtonColor(minusBtnDisabled), |
| 91 | + icon: widget.subtractIcon, |
| 92 | + onPressed: minusBtnDisabled ? null : () => _adjustValue(-widget.step), |
| 93 | + ), |
| 94 | + Text( |
| 95 | + widget.displayFormat?.format(_value) ?? _value.toString(), |
| 96 | + style: widget.textStyle, |
| 97 | + key: ValueKey(_value), |
| 98 | + ), |
| 99 | + IconButton( |
| 100 | + padding: widget.iconPadding, |
| 101 | + iconSize: widget.iconSize, |
| 102 | + color: _spinButtonColor(addBtnDisabled), |
| 103 | + icon: widget.addIcon, |
| 104 | + onPressed: addBtnDisabled ? null : () => _adjustValue(widget.step), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments