Unverified Commit 4c5303a2 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Unfocus textfield if it is disabled (#18097)

parent 6db3c6d1
......@@ -344,6 +344,11 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
_controller = new TextEditingController.fromValue(oldWidget.controller.value);
else if (widget.controller != null && oldWidget.controller == null)
_controller = null;
final bool isEnabled = widget.enabled ?? widget.decoration?.enabled ?? true;
final bool wasEnabled = oldWidget.enabled ?? oldWidget.decoration?.enabled ?? true;
if (wasEnabled && !isEnabled) {
_effectiveFocusNode.unfocus();
}
}
@override
......
......@@ -2138,4 +2138,30 @@ void main() {
expect(exception.toString(), endsWith(':\n $textField\nThe ancestors of this widget were:\n [root]'));
});
testWidgets('TextField loses focus when disabled', (WidgetTester tester) async {
final FocusNode focusNode = new FocusNode();
await tester.pumpWidget(
boilerplate(
child: new TextField(
focusNode: focusNode,
autofocus: true,
enabled: true,
),
),
);
expect(focusNode.hasFocus, isTrue);
await tester.pumpWidget(
boilerplate(
child: new TextField(
focusNode: focusNode,
autofocus: true,
enabled: false,
),
),
);
expect(focusNode.hasFocus, isFalse);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment