From f546aa7def299764cd8378ddd44728b6c395a12c Mon Sep 17 00:00:00 2001 From: Alex Vincent Date: Thu, 19 Dec 2019 02:45:29 +0800 Subject: [PATCH] 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. --- .../flutter/lib/src/material/text_field.dart | 25 ++++++++++------ .../test/material/text_field_test.dart | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index 101f30fac4..7cf3c73572 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -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 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); } diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 8d9feef30d..c56d747055 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -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: [ + 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)); + }); }