-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbase_spin_box.dart
189 lines (164 loc) · 5.99 KB
/
base_spin_box.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// MIT License
//
// Copyright (c) 2020 J-P Nurmi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'spin_formatter.dart';
// ignore_for_file: public_member_api_docs
abstract class BaseSpinBox extends StatefulWidget {
const BaseSpinBox({Key? key}) : super(key: key);
double get min;
double get max;
double get step;
double? get pageStep;
double get value;
int get decimals;
int get digits;
void Function(double)? get onSubmitted;
ValueChanged<double>? get onChanged;
bool Function(double value)? get canChange;
VoidCallback? get beforeChange;
VoidCallback? get afterChange;
bool get readOnly;
FocusNode? get focusNode;
String get decimalSeparator;
}
mixin SpinBoxMixin<T extends BaseSpinBox> on State<T> {
late double _value;
late double _cachedValue;
late final FocusNode _focusNode;
late final TextEditingController _controller;
double get value => _value;
bool get hasFocus => _focusNode.hasFocus;
FocusNode get focusNode => _focusNode;
TextEditingController get controller => _controller;
SpinFormatter get formatter => SpinFormatter(
min: widget.min, max: widget.max, decimals: widget.decimals, decimalSeparator: widget.decimalSeparator);
double _parseValue(String text) => double.tryParse(text.replaceAll(widget.decimalSeparator, '.')) ?? 0;
String _formatText(double value) {
return value.toStringAsFixed(widget.decimals).padLeft(widget.digits, '0').replaceAll('.', widget.decimalSeparator);
}
Map<ShortcutActivator, VoidCallback> get bindings {
return {
// ### TODO: use SingleActivator fixed in Flutter 2.10+
// https://github.com/flutter/flutter/issues/92717
LogicalKeySet(LogicalKeyboardKey.arrowUp): _stepUp,
LogicalKeySet(LogicalKeyboardKey.arrowDown): _stepDown,
if (widget.pageStep != null) ...{
LogicalKeySet(LogicalKeyboardKey.pageUp): _pageStepUp,
LogicalKeySet(LogicalKeyboardKey.pageDown): _pageStepDown,
}
};
}
@override
void initState() {
super.initState();
_value = widget.value;
_cachedValue = widget.value;
_controller = TextEditingController(text: _formatText(_value));
_controller.addListener(_updateValue);
_focusNode = widget.focusNode ?? FocusNode();
_focusNode.addListener(_handleFocusChanged);
}
@override
void dispose() {
_focusNode.removeListener(_handleFocusChanged);
if (widget.focusNode == null) {
_focusNode.dispose();
}
_controller.dispose();
super.dispose();
}
void _stepUp() => setValue(value + widget.step);
void _stepDown() => setValue(value - widget.step);
void _pageStepUp() => setValue(value + widget.pageStep!);
void _pageStepDown() => setValue(value - widget.pageStep!);
void _updateValue() {
final v = _parseValue(_controller.text);
if (v == _value) return;
if (widget.canChange?.call(v) == false) {
controller.text = _formatText(_cachedValue);
setState(() {
_value = _cachedValue;
});
return;
}
setState(() => _value = v);
widget.onChanged?.call(v);
}
void setValue(double v) {
final newValue = v.clamp(widget.min, widget.max);
if (newValue == value) return;
if (widget.canChange?.call(newValue) == false) return;
widget.beforeChange?.call();
setState(() => _updateController(value, newValue));
widget.onSubmitted?.call(double.parse(_formatText(newValue)));
widget.afterChange?.call();
}
void _updateController(double oldValue, double newValue) {
final text = _formatText(newValue);
final selection = _controller.selection;
final oldOffset = value.isNegative ? 1 : 0;
final newOffset = _parseValue(text).isNegative ? 1 : 0;
_controller.value = _controller.value.copyWith(
text: text,
selection: selection.copyWith(
baseOffset: selection.baseOffset - oldOffset + newOffset,
extentOffset: selection.extentOffset - oldOffset + newOffset,
),
);
}
@protected
double fixupValue(String value) {
final v = _parseValue(value);
if (value.isEmpty || (v < widget.min || v > widget.max)) {
// will trigger notify to _updateValue()
_controller.text = _formatText(_cachedValue);
} else {
_cachedValue = _value;
}
return _cachedValue;
}
void _handleFocusChanged() {
setState(() {
if (hasFocus) {
_selectAll();
} else {
final value = fixupValue(_controller.text);
widget.onSubmitted?.call(value);
}
});
}
void _selectAll() {
_controller.selection = _controller.selection
.copyWith(baseOffset: 0, extentOffset: _controller.text.length);
}
@override
void didUpdateWidget(T oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.value != widget.value) {
_controller.removeListener(_updateValue);
_value = _cachedValue = widget.value;
_updateController(oldWidget.value, widget.value);
_controller.addListener(_updateValue);
}
}
}