Skip to content

Commit ee8d329

Browse files
authored
Textfield input problem Fixed
Created a method check() which checks if the value is between minVal and maxVal if not it changes the value to minVal, maxVal or initVal depending upon conditions. Rather than onChanged now value is checked in onFieldSubmitted and onTapOutside using check().
1 parent 343d06f commit ee8d329

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

lib/src/input_qty.dart

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,28 @@ class _InputQtyState extends State<InputQty> {
311311
widget.onQtyChanged?.call(value);
312312
}
313313

314+
/// check value is within range of minVal and maxVal
315+
void checkValue() {
316+
num? temp = num.tryParse(_valCtrl.text);
317+
if (temp != null) {
318+
if (temp >= widget.maxVal) {
319+
temp = widget.maxVal;
320+
321+
_valCtrl.text = "$temp";
322+
} else if (temp < widget.minVal) {
323+
temp = widget.minVal;
324+
325+
_valCtrl.text = "$temp";
326+
}
327+
} else {
328+
temp = widget.initVal;
329+
330+
_valCtrl.text = "$temp";
331+
}
332+
widget.onQtyChanged?.call(temp);
333+
currentval.value = temp;
334+
}
335+
314336
/// setup decoration widget to textformfield
315337
InputDecoration decorationProps() {
316338
final defaultBorder = widget.decoration.border ??
@@ -462,7 +484,10 @@ class _InputQtyState extends State<InputQty> {
462484
/// widget textformfield
463485
Widget _buildtextfield() => TextFormField(
464486
decoration: decorationProps(),
465-
onTapOutside: (event) => FocusManager.instance.primaryFocus?.unfocus(),
487+
onTapOutside: (event) {
488+
checkValue();
489+
FocusManager.instance.primaryFocus?.unfocus();
490+
},
466491
controller: _valCtrl,
467492
readOnly: !widget.qtyFormProps.enableTyping,
468493
autovalidateMode: AutovalidateMode.onUserInteraction,
@@ -511,20 +536,8 @@ class _InputQtyState extends State<InputQty> {
511536
temp = temp;
512537
break;
513538
}
514-
// temp = temp.clamp(widget.minVal, widget.maxVal);
515-
// not using clamp, since need to update controller each limit
516-
if (temp >= widget.maxVal) {
517-
temp = widget.maxVal;
518-
519-
_valCtrl.text = "$temp";
520-
} else if (temp < widget.minVal) {
521-
temp = widget.minVal;
522-
523-
_valCtrl.text = "$temp";
524-
}
525-
widget.onQtyChanged?.call(temp);
526-
currentval.value = temp;
527539
},
540+
onFieldSubmitted: (_) => checkValue(),
528541
);
529542

530543
/// build message widget

0 commit comments

Comments
 (0)