diff --git a/packages/flutter/lib/src/cupertino/text_field.dart b/packages/flutter/lib/src/cupertino/text_field.dart index 8a9700da94..4c1d28e3f1 100644 --- a/packages/flutter/lib/src/cupertino/text_field.dart +++ b/packages/flutter/lib/src/cupertino/text_field.dart @@ -274,6 +274,7 @@ class CupertinoTextField extends StatefulWidget { !expands || (maxLines == null && minLines == null), 'minLines and maxLines must be null when expands is true.', ), + assert(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'), assert(maxLength == null || maxLength > 0), assert(clearButtonMode != null), assert(prefixMode != null), diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index be03b5b484..10a6ce984a 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -344,6 +344,7 @@ class TextField extends StatefulWidget { !expands || (maxLines == null && minLines == null), 'minLines and maxLines must be null when expands is true.', ), + assert(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'), assert(maxLength == null || maxLength == TextField.noMaxLength || maxLength > 0), keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline), toolbarOptions = toolbarOptions ?? (obscureText ? diff --git a/packages/flutter/lib/src/material/text_form_field.dart b/packages/flutter/lib/src/material/text_form_field.dart index f3ec6355cd..1868e61109 100644 --- a/packages/flutter/lib/src/material/text_form_field.dart +++ b/packages/flutter/lib/src/material/text_form_field.dart @@ -139,6 +139,7 @@ class TextFormField extends FormField { !expands || (maxLines == null && minLines == null), 'minLines and maxLines must be null when expands is true.', ), + assert(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'), assert(maxLength == null || maxLength > 0), assert(enableInteractiveSelection != null), super( diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index adc07789f0..7a1b0fcade 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -422,6 +422,7 @@ class EditableText extends StatefulWidget { !expands || (maxLines == null && minLines == null), 'minLines and maxLines must be null when expands is true.', ), + assert(!obscureText || maxLines == 1, 'Obscured fields cannot be multiline.'), assert(autofocus != null), assert(rendererIgnoresPointer != null), assert(scrollPadding != null), diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 363b75b958..d6582eba39 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -6530,7 +6530,7 @@ void main() { textAlign: TextAlign.end, textDirection: TextDirection.ltr, autofocus: true, - obscureText: true, + obscureText: false, autocorrect: false, maxLines: 10, maxLength: 100, @@ -6554,7 +6554,6 @@ void main() { 'decoration: InputDecoration(labelText: "foo")', 'style: TextStyle(inherit: true, color: Color(0xff00ff00))', 'autofocus: true', - 'obscureText: true', 'autocorrect: false', 'maxLines: 10', 'maxLength: 100', diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 98a006f4d2..af4fc84578 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -3350,6 +3350,38 @@ void main() { expect(scrollable.controller.position.pixels, equals(14)); expect(scrollable.controller.position.pixels, equals(renderEditable.maxScrollExtent)); }, skip: isBrowser); + + testWidgets('obscured multiline fields throw an exception', (WidgetTester tester) async { + final TextEditingController controller = TextEditingController(); + expect( + () { + EditableText( + backgroundCursorColor: cursorColor, + controller: controller, + cursorColor: cursorColor, + focusNode: focusNode, + maxLines: 1, + obscureText: true, + style: textStyle, + ); + }, + returnsNormally, + ); + expect( + () { + EditableText( + backgroundCursorColor: cursorColor, + controller: controller, + cursorColor: cursorColor, + focusNode: focusNode, + maxLines: 2, + obscureText: true, + style: textStyle, + ); + }, + throwsAssertionError, + ); + }); } class MockTextSelectionControls extends Mock implements TextSelectionControls {