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..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 @@ -117,7 +117,10 @@ class OnscreenKeyboardTextFormField extends StatefulWidget { EditableText.defaultStylusHandwritingEnabled, this.canRequestFocus = true, this.hintLocales, - }); + }) : assert( + initialValue == null || controller == null, + '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 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..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 @@ -161,5 +161,46 @@ 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 == + // ignore: lines_longer_than_80_chars + '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); + }, + ); }); }