Skip to content

Commit d8ef702

Browse files
authored
Cleanup InputDecoration.collapsed constructor (flutter#152165)
## Description This PR attemps to clarify the `InputDecoration.collapsed` documentation by explaining that it is not meant to be use with helper, label, counter, icons, prefixes and suffixes. It also adds some parameters that make sense for a collapsed decoration (`hintMaxLines`, `hintFadeDuration`, `constraints`). Removing parameters that should not have been added (`floatingLabelBehavior` and `floatingLabelAlignment`) will be part of another PR as it will require deprecations. ## Related Issue Fixes flutter#61331 ## Tests Adds 3 tests.
1 parent 1ec4907 commit d8ef702

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,19 +2622,26 @@ class InputDecoration {
26222622
///
26232623
/// This type of input decoration does not include a border by default.
26242624
///
2625+
/// A collapsed decoration cannot have [labelText], [errorText], [counter],
2626+
/// [icon], prefixes, and suffixes.
2627+
///
26252628
/// Sets the [isCollapsed] property to true.
2629+
/// Sets the [contentPadding] property to [EdgeInsets.zero].
26262630
const InputDecoration.collapsed({
26272631
required this.hintText,
26282632
this.floatingLabelBehavior,
26292633
this.floatingLabelAlignment,
26302634
this.hintStyle,
26312635
this.hintTextDirection,
2636+
this.hintMaxLines,
2637+
this.hintFadeDuration,
26322638
this.filled = false,
26332639
this.fillColor,
26342640
this.focusColor,
26352641
this.hoverColor,
26362642
this.border = InputBorder.none,
26372643
this.enabled = true,
2644+
this.constraints,
26382645
}) : icon = null,
26392646
iconColor = null,
26402647
label = null,
@@ -2645,8 +2652,6 @@ class InputDecoration {
26452652
helperText = null,
26462653
helperStyle = null,
26472654
helperMaxLines = null,
2648-
hintMaxLines = null,
2649-
hintFadeDuration = null,
26502655
error = null,
26512656
errorText = null,
26522657
errorStyle = null,
@@ -2675,8 +2680,7 @@ class InputDecoration {
26752680
disabledBorder = null,
26762681
enabledBorder = null,
26772682
semanticCounterText = null,
2678-
alignLabelWithHint = false,
2679-
constraints = null;
2683+
alignLabelWithHint = false;
26802684

26812685
/// An icon to show before the input field and outside of the decoration's
26822686
/// container.
@@ -3019,7 +3023,8 @@ class InputDecoration {
30193023

30203024
/// Whether the decoration is the same size as the input field.
30213025
///
3022-
/// A collapsed decoration cannot have [labelText], [errorText], an [icon].
3026+
/// A collapsed decoration cannot have [labelText], [errorText], [counter],
3027+
/// [icon], prefixes, and suffixes.
30233028
///
30243029
/// To create a collapsed input decoration, use [InputDecoration.collapsed].
30253030
final bool? isCollapsed;
@@ -3881,7 +3886,7 @@ class InputDecoration {
38813886
/// the current input decoration theme to initialize null [InputDecoration]
38823887
/// properties.
38833888
///
3884-
/// The [InputDecoration.applyDefaults] method is used to combine a input
3889+
/// The [InputDecoration.applyDefaults] method is used to combine an input
38853890
/// decoration theme with an [InputDecoration] object.
38863891
@immutable
38873892
class InputDecorationTheme with Diagnosticable {

packages/flutter/test/material/input_decorator_test.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6523,6 +6523,70 @@ void main() {
65236523
expect(getBorderWeight(tester), 0.0);
65246524
});
65256525

6526+
testWidgets('InputDecoration.collapsed accepts constraints', (WidgetTester tester) async {
6527+
await tester.pumpWidget(
6528+
buildInputDecorator(
6529+
decoration: const InputDecoration.collapsed(
6530+
hintText: hintText,
6531+
constraints: BoxConstraints.tightFor(width: 200.0, height: 32.0),
6532+
),
6533+
),
6534+
);
6535+
6536+
expect(getDecoratorRect(tester).size, const Size(200.0, 32.0));
6537+
});
6538+
6539+
testWidgets('InputDecoration.collapsed accepts hintMaxLines', (WidgetTester tester) async {
6540+
await tester.pumpWidget(
6541+
buildInputDecorator(
6542+
decoration: const InputDecoration.collapsed(
6543+
hintText: threeLines,
6544+
hintMaxLines: 2,
6545+
),
6546+
),
6547+
);
6548+
6549+
const double hintLineHeight = 24.0; // font size = 16 and font height = 1.5.
6550+
expect(getDecoratorRect(tester).size, const Size(800.0, 2 * hintLineHeight));
6551+
});
6552+
6553+
testWidgets('InputDecoration.collapsed accepts hintFadeDuration', (WidgetTester tester) async {
6554+
// Build once with empty content.
6555+
await tester.pumpWidget(
6556+
buildInputDecorator(
6557+
isEmpty: true,
6558+
decoration: const InputDecoration.collapsed(
6559+
hintText: hintText,
6560+
hintFadeDuration: Duration(milliseconds: 120),
6561+
),
6562+
),
6563+
);
6564+
6565+
// Hint is visible (opacity 1.0).
6566+
expect(getHintOpacity(tester), 1.0);
6567+
6568+
// Rebuild with non-empty content.
6569+
await tester.pumpWidget(
6570+
buildInputDecorator(
6571+
decoration: const InputDecoration.collapsed(
6572+
hintText: hintText,
6573+
hintFadeDuration: Duration(milliseconds: 120),
6574+
),
6575+
),
6576+
);
6577+
6578+
// The hint's opacity animates from 1.0 to 0.0.
6579+
// The animation's default duration is 20ms.
6580+
await tester.pump(const Duration(milliseconds: 50));
6581+
final double hintOpacity50ms = getHintOpacity(tester);
6582+
expect(hintOpacity50ms, inExclusiveRange(0.0, 1.0));
6583+
await tester.pump(const Duration(milliseconds: 50));
6584+
final double hintOpacity100ms = getHintOpacity(tester);
6585+
expect(hintOpacity100ms, inExclusiveRange(0.0, hintOpacity50ms));
6586+
await tester.pump(const Duration(milliseconds: 50));
6587+
expect(getHintOpacity(tester), 0.0);
6588+
});
6589+
65266590
test('InputDecorationTheme.isCollapsed is applied', () {
65276591
final InputDecoration decoration = const InputDecoration(
65286592
hintText: 'Hello, Flutter!',

0 commit comments

Comments
 (0)