From 1d9891bc4d841c633d4769952f981dcc7e8d7557 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 12 May 2023 12:36:10 +0800 Subject: [PATCH 1/2] Check whether a focus state transition occurs --- lib/src/base_spin_box.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/src/base_spin_box.dart b/lib/src/base_spin_box.dart index fd986d6..f31157d 100644 --- a/lib/src/base_spin_box.dart +++ b/lib/src/base_spin_box.dart @@ -52,6 +52,9 @@ mixin SpinBoxMixin on State { late final FocusNode _focusNode; late final TextEditingController _controller; + // record previous focus state + bool _hasFocusPrev = false; + double get value => _value; bool get hasFocus => _focusNode.hasFocus; FocusNode get focusNode => _focusNode; @@ -165,8 +168,13 @@ mixin SpinBoxMixin on State { _selectAll(); } else { final value = fixupValue(_controller.text); - widget.onSubmitted?.call(value); + // Checking for valid state transitions is necessary + // because `_handleFocusChanged` is called when Spinbox are enabled + // or disabled. + if (_hasFocusPrev) widget.onSubmitted?.call(value); } + + _hasFocusPrev = hasFocus; }); } From 48d1f2cb20b31e694058d9803c42e3f246af3cc0 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 13 May 2023 11:22:02 +0800 Subject: [PATCH 2/2] move `_hasFocusPrev` initialization to `initState` --- lib/src/base_spin_box.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/base_spin_box.dart b/lib/src/base_spin_box.dart index f31157d..379019e 100644 --- a/lib/src/base_spin_box.dart +++ b/lib/src/base_spin_box.dart @@ -53,7 +53,7 @@ mixin SpinBoxMixin on State { late final TextEditingController _controller; // record previous focus state - bool _hasFocusPrev = false; + late bool _hasFocusPrev; double get value => _value; bool get hasFocus => _focusNode.hasFocus; @@ -89,6 +89,7 @@ mixin SpinBoxMixin on State { _controller.addListener(_updateValue); _focusNode = widget.focusNode ?? FocusNode(); _focusNode.addListener(_handleFocusChanged); + _hasFocusPrev = hasFocus; } @override