TextField enabled fix (#55775)

This commit is contained in:
Justin McCandless 2020-04-28 10:49:02 -07:00 committed by GitHub
parent f7ad30b777
commit 43ce3fd988
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 4 deletions

View file

@ -802,7 +802,7 @@ class _TextFieldState extends State<TextField> implements TextSelectionGestureDe
final InputDecoration effectiveDecoration = (widget.decoration ?? const InputDecoration())
.applyDefaults(themeData.inputDecorationTheme)
.copyWith(
enabled: widget.enabled,
enabled: _isEnabled,
hintMaxLines: widget.decoration?.hintMaxLines ?? widget.maxLines,
);

View file

@ -165,7 +165,7 @@ class TextFormField extends FormField<String> {
FormFieldSetter<String> onSaved,
FormFieldValidator<String> validator,
List<TextInputFormatter> inputFormatters,
bool enabled = true,
bool enabled,
double cursorWidth = 2.0,
Radius cursorRadius,
Color cursorColor,
@ -205,7 +205,7 @@ class TextFormField extends FormField<String> {
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
enabled: enabled,
enabled: enabled ?? decoration?.enabled ?? true,
builder: (FormFieldState<String> field) {
final _TextFormFieldState state = field as _TextFormFieldState;
final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration())
@ -248,7 +248,7 @@ class TextFormField extends FormField<String> {
onEditingComplete: onEditingComplete,
onSubmitted: onFieldSubmitted,
inputFormatters: inputFormatters,
enabled: enabled,
enabled: enabled ?? decoration?.enabled ?? true,
cursorWidth: cursorWidth,
cursorRadius: cursorRadius,
cursorColor: cursorColor,

View file

@ -3597,6 +3597,52 @@ void main() {
semantics.dispose();
});
testWidgets('Disabled text field hides helper and counter', (WidgetTester tester) async {
const String helperText = 'helper text';
const String counterText = 'counter text';
const String errorText = 'error text';
Widget buildFrame(bool enabled, bool hasError) {
return MaterialApp(
home: Material(
child: Center(
child: TextField(
decoration: InputDecoration(
labelText: 'label text',
helperText: helperText,
counterText: counterText,
errorText: hasError ? errorText : null,
enabled: enabled,
),
),
),
),
);
}
await tester.pumpWidget(buildFrame(true, false));
Text helperWidget = tester.widget(find.text(helperText));
Text counterWidget = tester.widget(find.text(counterText));
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
expect(counterWidget.style.color, isNot(equals(Colors.transparent)));
await tester.pumpWidget(buildFrame(true, true));
counterWidget = tester.widget(find.text(counterText));
Text errorWidget = tester.widget(find.text(errorText));
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
expect(errorWidget.style.color, isNot(equals(Colors.transparent)));
// When enabled is false, the helper/error and counter are not visible.
await tester.pumpWidget(buildFrame(false, false));
helperWidget = tester.widget(find.text(helperText));
counterWidget = tester.widget(find.text(counterText));
expect(helperWidget.style.color, equals(Colors.transparent));
expect(counterWidget.style.color, equals(Colors.transparent));
await tester.pumpWidget(buildFrame(false, true));
errorWidget = tester.widget(find.text(errorText));
counterWidget = tester.widget(find.text(counterText));
expect(counterWidget.style.color, equals(Colors.transparent));
expect(errorWidget.style.color, equals(Colors.transparent));
});
testWidgets('currentValueLength/maxValueLength are in the tree', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);
final TextEditingController controller = TextEditingController();

View file

@ -240,6 +240,54 @@ void main() {
expect(_validateCalled, 2);
});
testWidgets('Disabled field hides helper and counter', (WidgetTester tester) async {
const String helperText = 'helper text';
const String counterText = 'counter text';
const String errorText = 'error text';
Widget buildFrame(bool enabled, bool hasError) {
return MaterialApp(
home: Material(
child: Center(
child: TextFormField(
decoration: InputDecoration(
labelText: 'label text',
helperText: helperText,
counterText: counterText,
errorText: hasError ? errorText : null,
enabled: enabled,
),
),
),
),
);
}
// When enabled is true, the helper/error and counter are visible.
await tester.pumpWidget(buildFrame(true, false));
Text helperWidget = tester.widget(find.text(helperText));
Text counterWidget = tester.widget(find.text(counterText));
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
expect(counterWidget.style.color, isNot(equals(Colors.transparent)));
await tester.pumpWidget(buildFrame(true, true));
counterWidget = tester.widget(find.text(counterText));
Text errorWidget = tester.widget(find.text(errorText));
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
expect(errorWidget.style.color, isNot(equals(Colors.transparent)));
// When enabled is false, the helper/error and counter are not visible.
await tester.pumpWidget(buildFrame(false, false));
helperWidget = tester.widget(find.text(helperText));
counterWidget = tester.widget(find.text(counterText));
expect(helperWidget.style.color, equals(Colors.transparent));
expect(counterWidget.style.color, equals(Colors.transparent));
await tester.pumpWidget(buildFrame(false, true));
errorWidget = tester.widget(find.text(errorText));
counterWidget = tester.widget(find.text(counterText));
expect(counterWidget.style.color, equals(Colors.transparent));
expect(errorWidget.style.color, equals(Colors.transparent));
});
testWidgets('passing a buildCounter shows returned widget', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Material(