Unfocus textfield if it is disabled (#18097)

This commit is contained in:
Michael Goderbauer 2018-06-01 09:40:52 -07:00 committed by GitHub
parent 6db3c6d1d1
commit 4c5303a204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -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

View File

@ -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);
});
}