From 3bf09d2ce2d0a0a227a6a92eeb28288079ff0a89 Mon Sep 17 00:00:00 2001 From: Albin <56157868+albinpk@users.noreply.github.com> Date: Fri, 28 Nov 2025 21:17:55 +0530 Subject: [PATCH 1/3] fix: handle `initialValue` in `OnscreenKeyboardTextFormField` --- .../lib/src/onscreen_keyboard_text_form_field.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart b/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart index 7d5928e..40ef70e 100644 --- a/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart +++ b/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart @@ -117,7 +117,10 @@ class OnscreenKeyboardTextFormField extends StatefulWidget { EditableText.defaultStylusHandwritingEnabled, this.canRequestFocus = true, this.hintLocales, - }); + }) : assert( + initialValue == null || controller == null, + 'You should not provide both an initialValue and a controller', + ); /// This key is used to identify the form field when it is attached to the /// onscreen keyboard. @@ -719,7 +722,8 @@ class _OnscreenKeyboardTextFormFieldState implements OnscreenKeyboardFieldState { /// The [TextEditingController] for the text field. TextEditingController get _effectiveController => - widget.controller ?? (_controller ??= TextEditingController()); + widget.controller ?? + (_controller ??= TextEditingController(text: widget.initialValue)); TextEditingController? _controller; /// The [FocusNode] for the text field. @@ -779,7 +783,6 @@ class _OnscreenKeyboardTextFormFieldState groupId: widget.groupId, controller: _effectiveController, focusNode: _effectiveFocusNode, - initialValue: widget.initialValue, forceErrorText: widget.forceErrorText, decoration: widget.decoration, // prevent the keyboard from opening From 4bc570a3d72c267dabe8bad9f71754281dd8ceb7 Mon Sep 17 00:00:00 2001 From: Albin <56157868+albinpk@users.noreply.github.com> Date: Fri, 28 Nov 2025 21:51:07 +0530 Subject: [PATCH 2/3] test: add test for `initialValue` in `OnscreenKeyboardTextFormField` --- ...nscreen_keyboard_text_form_field_test.dart | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart b/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart index 42a3afe..d61414a 100644 --- a/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart +++ b/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart @@ -161,5 +161,44 @@ void main() { expect(state.validate(), false); }, ); + + test( + 'should throw assertion error if initialValue and controller are provided', + () { + expect( + () => OnscreenKeyboardTextFormField( + initialValue: 'initial', + controller: TextEditingController(), + ), + throwsA( + predicate( + (e) => + e is AssertionError && + e.message == + 'You should not provide both an initialValue and a controller', + ), + ), + ); + }, + ); + + testWidgets( + 'initialValue in OnscreenKeyboardTextFormField', + (tester) async { + await tester.pumpWidget( + MaterialApp( + builder: OnscreenKeyboard.builder(width: (_) => 200), + home: const Scaffold( + body: OnscreenKeyboardTextFormField( + initialValue: 'initial value', + ), + ), + ), + ); + + await tester.pumpAndSettle(); + expect(find.text('initial value'), findsOneWidget); + }, + ); }); } From d6b2fbf357f9ac3764494e3f304d4889c3c3b838 Mon Sep 17 00:00:00 2001 From: Albin <56157868+albinpk@users.noreply.github.com> Date: Fri, 28 Nov 2025 22:32:46 +0530 Subject: [PATCH 3/3] update assertion message and format code --- .../lib/src/onscreen_keyboard_text_form_field.dart | 2 +- .../test/src/onscreen_keyboard_text_form_field_test.dart | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart b/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart index 40ef70e..a137ae2 100644 --- a/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart +++ b/packages/flutter_onscreen_keyboard/lib/src/onscreen_keyboard_text_form_field.dart @@ -119,7 +119,7 @@ class OnscreenKeyboardTextFormField extends StatefulWidget { this.hintLocales, }) : assert( initialValue == null || controller == null, - 'You should not provide both an initialValue and a controller', + 'Should not provide both an initialValue and a controller', ); /// This key is used to identify the form field when it is attached to the diff --git a/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart b/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart index d61414a..ecbf5b8 100644 --- a/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart +++ b/packages/flutter_onscreen_keyboard/test/src/onscreen_keyboard_text_form_field_test.dart @@ -163,7 +163,8 @@ void main() { ); test( - 'should throw assertion error if initialValue and controller are provided', + 'should throw assertion error if ' + 'initialValue and controller are provided', () { expect( () => OnscreenKeyboardTextFormField( @@ -175,7 +176,8 @@ void main() { (e) => e is AssertionError && e.message == - 'You should not provide both an initialValue and a controller', + // ignore: lines_longer_than_80_chars + 'Should not provide both an initialValue and a controller', ), ), );