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.
This commit is contained in:
parent
d328e0cea0
commit
f546aa7def
@ -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));
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user