Unverified Commit bf551a31 authored by Mouad Debbar's avatar Mouad Debbar Committed by GitHub

EditableText's autofocus:true should not crash (#50372)

parent c388a261
......@@ -1154,8 +1154,12 @@ class EditableTextState extends State<EditableText> with AutomaticKeepAliveClien
void didChangeDependencies() {
super.didChangeDependencies();
if (!_didAutoFocus && widget.autofocus) {
FocusScope.of(context).autofocus(widget.focusNode);
_didAutoFocus = true;
SchedulerBinding.instance.addPostFrameCallback((_) {
if (mounted) {
FocusScope.of(context).autofocus(widget.focusNode);
}
});
}
}
......
......@@ -4178,6 +4178,37 @@ void main() {
}
expect(tester.testTextInput.editingState['text'], 'flutter is the best!...');
});
testWidgets('autofocus:true on first frame does not throw', (WidgetTester tester) async {
final TextEditingController controller = TextEditingController(text: testText);
controller.selection = const TextSelection(
baseOffset: 0,
extentOffset: 0,
affinity: TextAffinity.upstream,
);
await tester.pumpWidget(MaterialApp(
home: EditableText(
maxLines: 10,
controller: controller,
showSelectionHandles: true,
autofocus: true,
focusNode: FocusNode(),
style: Typography.material2018(platform: TargetPlatform.android).black.subtitle1,
cursorColor: Colors.blue,
backgroundCursorColor: Colors.grey,
selectionControls: materialTextSelectionControls,
keyboardType: TextInputType.text,
textAlign: TextAlign.right,
),
));
await tester.pumpAndSettle(); // Wait for autofocus to take effect.
final dynamic exception = tester.takeException();
expect(exception, isNull);
});
}
class MockTextSelectionControls extends Mock implements TextSelectionControls {
......
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