Commit f546aa7d authored by Alex Vincent's avatar Alex Vincent Committed by Justin McCandless

Fix buildCounter returns a widget when set to return null. (#45749)

When buildCounter returns null it no longer produces a widget that takes up space.
parent d328e0ce
......@@ -671,6 +671,9 @@ class TextField extends StatefulWidget {
/// }
/// ```
/// {@end-tool}
///
/// If buildCounter returns null, then no counter and no Semantics widget will
/// be created at all.
final InputCounterWidgetBuilder buildCounter;
/// {@macro flutter.widgets.editableText.scrollPhysics}
......@@ -771,16 +774,20 @@ class _TextFieldState extends State<TextField> implements TextSelectionGestureDe
&& effectiveDecoration.counterText == null
&& widget.buildCounter != null) {
final bool isFocused = _effectiveFocusNode.hasFocus;
counter = Semantics(
container: true,
liveRegion: isFocused,
child: widget.buildCounter(
context,
currentLength: currentLength,
maxLength: widget.maxLength,
isFocused: isFocused,
),
final Widget builtCounter = widget.buildCounter(
context,
currentLength: currentLength,
maxLength: widget.maxLength,
isFocused: isFocused,
);
// If buildCounter returns null, don't add a counter widget to the field.
if (builtCounter != null) {
counter = Semantics(
container: true,
liveRegion: isFocused,
child: builtCounter,
);
}
return effectiveDecoration.copyWith(counter: counter);
}
......
......@@ -7402,4 +7402,34 @@ void main() {
await tester.pump();
expect(focusNode3.hasPrimaryFocus, isTrue);
});
testWidgets("A buildCounter that returns null doesn't affect the size of the TextField", (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/44909
final GlobalKey textField1Key = GlobalKey();
final GlobalKey textField2Key = GlobalKey();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
TextField(key: textField1Key),
TextField(
key: textField2Key,
maxLength: 1,
buildCounter: (BuildContext context, {int currentLength, bool isFocused, int maxLength}) => null,
),
],
),
),
),
);
await tester.pumpAndSettle();
final Size textFieldSize1 = tester.getSize(find.byKey(textField1Key));
final Size textFieldSize2 = tester.getSize(find.byKey(textField2Key));
expect(textFieldSize1, equals(textFieldSize2));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment