CupertinoTextField should not accept requestFocus when disabled (#65235)
This commit is contained in:
parent
408cd71d82
commit
dd41f41f34
@ -653,6 +653,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
|
||||
if (widget.controller == null) {
|
||||
_createLocalController();
|
||||
}
|
||||
_effectiveFocusNode.canRequestFocus = widget.enabled ?? true;
|
||||
}
|
||||
|
||||
@override
|
||||
@ -665,11 +666,7 @@ class _CupertinoTextFieldState extends State<CupertinoTextField> with Restoratio
|
||||
_controller!.dispose();
|
||||
_controller = null;
|
||||
}
|
||||
final bool isEnabled = widget.enabled ?? true;
|
||||
final bool wasEnabled = oldWidget.enabled ?? true;
|
||||
if (wasEnabled && !isEnabled) {
|
||||
_effectiveFocusNode.unfocus();
|
||||
}
|
||||
_effectiveFocusNode.canRequestFocus = widget.enabled ?? true;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -2707,6 +2707,43 @@ void main() {
|
||||
expect(tapCount, 1);
|
||||
});
|
||||
|
||||
testWidgets('Focus test when the text field is disabled', (WidgetTester tester) async {
|
||||
final FocusNode focusNode = FocusNode();
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp(
|
||||
home: Center(
|
||||
child: CupertinoTextField(
|
||||
focusNode: focusNode,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(focusNode.hasFocus, false); // initial status
|
||||
|
||||
// Should accept requestFocus.
|
||||
focusNode.requestFocus();
|
||||
await tester.pump();
|
||||
expect(focusNode.hasFocus, true);
|
||||
|
||||
// Disable the text field, now it should not accept requestFocus.
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp(
|
||||
home: Center(
|
||||
child: CupertinoTextField(
|
||||
enabled: false,
|
||||
focusNode: focusNode,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Should not accept requestFocus.
|
||||
focusNode.requestFocus();
|
||||
await tester.pump();
|
||||
expect(focusNode.hasFocus, false);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'text field respects theme',
|
||||
(WidgetTester tester) async {
|
||||
|
Loading…
x
Reference in New Issue
Block a user