Unfocus textfield if it is disabled (#18097)
This commit is contained in:
parent
6db3c6d1d1
commit
4c5303a204
@ -344,6 +344,11 @@ class _TextFieldState extends State<TextField> with AutomaticKeepAliveClientMixi
|
|||||||
_controller = new TextEditingController.fromValue(oldWidget.controller.value);
|
_controller = new TextEditingController.fromValue(oldWidget.controller.value);
|
||||||
else if (widget.controller != null && oldWidget.controller == null)
|
else if (widget.controller != null && oldWidget.controller == null)
|
||||||
_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
|
@override
|
||||||
|
@ -2138,4 +2138,30 @@ void main() {
|
|||||||
expect(exception.toString(), endsWith(':\n $textField\nThe ancestors of this widget were:\n [root]'));
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user