Skip to content

Commit 43ce3fd

Browse files
authored
TextField enabled fix (flutter#55775)
1 parent f7ad30b commit 43ce3fd

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

packages/flutter/lib/src/material/text_field.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ class _TextFieldState extends State<TextField> implements TextSelectionGestureDe
802802
final InputDecoration effectiveDecoration = (widget.decoration ?? const InputDecoration())
803803
.applyDefaults(themeData.inputDecorationTheme)
804804
.copyWith(
805-
enabled: widget.enabled,
805+
enabled: _isEnabled,
806806
hintMaxLines: widget.decoration?.hintMaxLines ?? widget.maxLines,
807807
);
808808

packages/flutter/lib/src/material/text_form_field.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class TextFormField extends FormField<String> {
165165
FormFieldSetter<String> onSaved,
166166
FormFieldValidator<String> validator,
167167
List<TextInputFormatter> inputFormatters,
168-
bool enabled = true,
168+
bool enabled,
169169
double cursorWidth = 2.0,
170170
Radius cursorRadius,
171171
Color cursorColor,
@@ -205,7 +205,7 @@ class TextFormField extends FormField<String> {
205205
onSaved: onSaved,
206206
validator: validator,
207207
autovalidate: autovalidate,
208-
enabled: enabled,
208+
enabled: enabled ?? decoration?.enabled ?? true,
209209
builder: (FormFieldState<String> field) {
210210
final _TextFormFieldState state = field as _TextFormFieldState;
211211
final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration())
@@ -248,7 +248,7 @@ class TextFormField extends FormField<String> {
248248
onEditingComplete: onEditingComplete,
249249
onSubmitted: onFieldSubmitted,
250250
inputFormatters: inputFormatters,
251-
enabled: enabled,
251+
enabled: enabled ?? decoration?.enabled ?? true,
252252
cursorWidth: cursorWidth,
253253
cursorRadius: cursorRadius,
254254
cursorColor: cursorColor,

packages/flutter/test/material/text_field_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,6 +3597,52 @@ void main() {
35973597
semantics.dispose();
35983598
});
35993599

3600+
testWidgets('Disabled text field hides helper and counter', (WidgetTester tester) async {
3601+
const String helperText = 'helper text';
3602+
const String counterText = 'counter text';
3603+
const String errorText = 'error text';
3604+
Widget buildFrame(bool enabled, bool hasError) {
3605+
return MaterialApp(
3606+
home: Material(
3607+
child: Center(
3608+
child: TextField(
3609+
decoration: InputDecoration(
3610+
labelText: 'label text',
3611+
helperText: helperText,
3612+
counterText: counterText,
3613+
errorText: hasError ? errorText : null,
3614+
enabled: enabled,
3615+
),
3616+
),
3617+
),
3618+
),
3619+
);
3620+
}
3621+
3622+
await tester.pumpWidget(buildFrame(true, false));
3623+
Text helperWidget = tester.widget(find.text(helperText));
3624+
Text counterWidget = tester.widget(find.text(counterText));
3625+
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
3626+
expect(counterWidget.style.color, isNot(equals(Colors.transparent)));
3627+
await tester.pumpWidget(buildFrame(true, true));
3628+
counterWidget = tester.widget(find.text(counterText));
3629+
Text errorWidget = tester.widget(find.text(errorText));
3630+
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
3631+
expect(errorWidget.style.color, isNot(equals(Colors.transparent)));
3632+
3633+
// When enabled is false, the helper/error and counter are not visible.
3634+
await tester.pumpWidget(buildFrame(false, false));
3635+
helperWidget = tester.widget(find.text(helperText));
3636+
counterWidget = tester.widget(find.text(counterText));
3637+
expect(helperWidget.style.color, equals(Colors.transparent));
3638+
expect(counterWidget.style.color, equals(Colors.transparent));
3639+
await tester.pumpWidget(buildFrame(false, true));
3640+
errorWidget = tester.widget(find.text(errorText));
3641+
counterWidget = tester.widget(find.text(counterText));
3642+
expect(counterWidget.style.color, equals(Colors.transparent));
3643+
expect(errorWidget.style.color, equals(Colors.transparent));
3644+
});
3645+
36003646
testWidgets('currentValueLength/maxValueLength are in the tree', (WidgetTester tester) async {
36013647
final SemanticsTester semantics = SemanticsTester(tester);
36023648
final TextEditingController controller = TextEditingController();

packages/flutter/test/material/text_form_field_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,54 @@ void main() {
240240
expect(_validateCalled, 2);
241241
});
242242

243+
244+
testWidgets('Disabled field hides helper and counter', (WidgetTester tester) async {
245+
const String helperText = 'helper text';
246+
const String counterText = 'counter text';
247+
const String errorText = 'error text';
248+
Widget buildFrame(bool enabled, bool hasError) {
249+
return MaterialApp(
250+
home: Material(
251+
child: Center(
252+
child: TextFormField(
253+
decoration: InputDecoration(
254+
labelText: 'label text',
255+
helperText: helperText,
256+
counterText: counterText,
257+
errorText: hasError ? errorText : null,
258+
enabled: enabled,
259+
),
260+
),
261+
),
262+
),
263+
);
264+
}
265+
266+
// When enabled is true, the helper/error and counter are visible.
267+
await tester.pumpWidget(buildFrame(true, false));
268+
Text helperWidget = tester.widget(find.text(helperText));
269+
Text counterWidget = tester.widget(find.text(counterText));
270+
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
271+
expect(counterWidget.style.color, isNot(equals(Colors.transparent)));
272+
await tester.pumpWidget(buildFrame(true, true));
273+
counterWidget = tester.widget(find.text(counterText));
274+
Text errorWidget = tester.widget(find.text(errorText));
275+
expect(helperWidget.style.color, isNot(equals(Colors.transparent)));
276+
expect(errorWidget.style.color, isNot(equals(Colors.transparent)));
277+
278+
// When enabled is false, the helper/error and counter are not visible.
279+
await tester.pumpWidget(buildFrame(false, false));
280+
helperWidget = tester.widget(find.text(helperText));
281+
counterWidget = tester.widget(find.text(counterText));
282+
expect(helperWidget.style.color, equals(Colors.transparent));
283+
expect(counterWidget.style.color, equals(Colors.transparent));
284+
await tester.pumpWidget(buildFrame(false, true));
285+
errorWidget = tester.widget(find.text(errorText));
286+
counterWidget = tester.widget(find.text(counterText));
287+
expect(counterWidget.style.color, equals(Colors.transparent));
288+
expect(errorWidget.style.color, equals(Colors.transparent));
289+
});
290+
243291
testWidgets('passing a buildCounter shows returned widget', (WidgetTester tester) async {
244292
await tester.pumpWidget(MaterialApp(
245293
home: Material(

0 commit comments

Comments
 (0)