Skip to content

Commit 10f7e05

Browse files
authored
Clean up focus change listener (#55)
In case of an internal focus node it doesn't matter, but an external focus node is not disposes and thus, the listener must be removed. Close: #44
1 parent baf6ed3 commit 10f7e05

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/src/base_spin_box.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ abstract class BaseSpinBoxState<T extends BaseSpinBox> extends State<T> {
7979
_controller = TextEditingController(text: _formatText(_value));
8080
_controller.addListener(_updateValue);
8181
_focusNode = widget.focusNode ?? FocusNode();
82-
_focusNode.addListener(() => setState(_selectAll));
83-
_focusNode.addListener(() {
84-
if (hasFocus) return;
85-
fixupValue(controller.text);
86-
});
82+
_focusNode.addListener(_handleFocusChanged);
8783
}
8884

8985
@override
9086
void dispose() {
87+
_focusNode.removeListener(_handleFocusChanged);
9188
if (widget.focusNode == null) {
9289
_focusNode.dispose();
9390
}
@@ -153,8 +150,15 @@ abstract class BaseSpinBoxState<T extends BaseSpinBox> extends State<T> {
153150
}
154151
}
155152

153+
void _handleFocusChanged() {
154+
if (hasFocus) {
155+
setState(_selectAll);
156+
} else {
157+
fixupValue(_controller.text);
158+
}
159+
}
160+
156161
void _selectAll() {
157-
if (!_focusNode.hasFocus) return;
158162
_controller.selection = _controller.selection
159163
.copyWith(baseOffset: 0, extentOffset: _controller.text.length);
160164
}

0 commit comments

Comments
 (0)