Skip to content

Commit b156a0f

Browse files
ameiyilHixie
authored andcommitted
Wrap the label widget of the Chip in a Flexible. (flutter#11523) (flutter#11554)
* Wrap the label widget of the Chip in a Flexible. (flutter#11523) * This allows the Chip to constrain the size of its content after taking into account the space occupied by the avatar and delete icon (if they are present) * Adding unit tests to evaluate correct size constraints of the Chip widget's label. (flutter#11523) * Minor change in formatting. * Adjust function formatting.
1 parent 309a2d7 commit b156a0f

2 files changed

Lines changed: 123 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ class Chip extends StatelessWidget {
123123
));
124124
}
125125

126-
children.add(new DefaultTextStyle(
127-
style: labelStyle ?? _kLabelStyle,
128-
child: label,
126+
children.add(new Flexible(
127+
child: new DefaultTextStyle(
128+
style: labelStyle ?? _kLabelStyle,
129+
child: label,
130+
),
129131
));
130132

131133
if (deletable) {

packages/flutter/test/material/chip_test.dart

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ import 'package:flutter_test/flutter_test.dart';
88
import 'feedback_tester.dart';
99

1010
void main() {
11+
/// Tests that a [Chip] that has its size constrained by its parent is
12+
/// further constraining the size of its child, the label widget.
13+
/// Optionally, adding an avatar or delete icon to the chip should not
14+
/// cause the chip or label to exceed its constrained size.
15+
Future<Null> _testConstrainedLabel(WidgetTester tester, {
16+
CircleAvatar avatar, VoidCallback onDeleted,
17+
}) async {
18+
const double labelWidth = 100.0;
19+
const double labelHeight = 50.0;
20+
const double chipParentWidth = 75.0;
21+
const double chipParentHeight = 25.0;
22+
final Key labelKey = new UniqueKey();
23+
24+
await tester.pumpWidget(
25+
new MaterialApp(
26+
home: new Material(
27+
child: new Center(
28+
child: new Container(
29+
width: chipParentWidth,
30+
height: chipParentHeight,
31+
child: new Chip(
32+
avatar: avatar,
33+
label: new Container(
34+
key: labelKey,
35+
width: labelWidth,
36+
height: labelHeight,
37+
),
38+
onDeleted: onDeleted,
39+
),
40+
),
41+
),
42+
),
43+
),
44+
);
45+
46+
final Size labelSize = tester.getSize(find.byKey(labelKey));
47+
expect(labelSize.width, lessThan(chipParentWidth));
48+
expect(labelSize.height, lessThanOrEqualTo(chipParentHeight));
49+
50+
final Size chipSize = tester.getSize(find.byType(Chip));
51+
expect(chipSize.width, chipParentWidth);
52+
expect(chipSize.height, chipParentHeight);
53+
}
54+
1155
testWidgets('Chip control test', (WidgetTester tester) async {
1256
final FeedbackTester feedback = new FeedbackTester();
1357
final List<String> deletedChipLabels = <String>[];
@@ -62,4 +106,78 @@ void main() {
62106

63107
feedback.dispose();
64108
});
109+
110+
testWidgets(
111+
'Chip does not constrain size of label widget if it does not exceed '
112+
'the available space', (WidgetTester tester) async {
113+
const double labelWidth = 50.0;
114+
const double labelHeight = 30.0;
115+
final Key labelKey = new UniqueKey();
116+
117+
await tester.pumpWidget(
118+
new Material(
119+
child: new Center(
120+
child: new Container(
121+
width: 500.0,
122+
height: 500.0,
123+
child: new Column(
124+
children: <Widget>[
125+
new Chip(
126+
label: new Container(
127+
key: labelKey,
128+
width: labelWidth,
129+
height: labelHeight,
130+
),
131+
),
132+
],
133+
),
134+
),
135+
),
136+
),
137+
);
138+
139+
final Size labelSize = tester.getSize(find.byKey(labelKey));
140+
expect(labelSize.width, labelWidth);
141+
expect(labelSize.height, labelHeight);
142+
});
143+
144+
testWidgets(
145+
'Chip constrains the size of the label widget when it exceeds the '
146+
'available space', (WidgetTester tester) async {
147+
await _testConstrainedLabel(tester);
148+
});
149+
150+
testWidgets(
151+
'Chip constrains the size of the label widget when it exceeds the '
152+
'available space and the avatar is present', (WidgetTester tester) async {
153+
await _testConstrainedLabel(
154+
tester,
155+
avatar: const CircleAvatar(
156+
child: const Text('A')
157+
),
158+
);
159+
});
160+
161+
testWidgets(
162+
'Chip constrains the size of the label widget when it exceeds the '
163+
'available space and the delete icon is present',
164+
(WidgetTester tester) async {
165+
await _testConstrainedLabel(
166+
tester,
167+
onDeleted: () {},
168+
);
169+
});
170+
171+
testWidgets(
172+
'Chip constrains the size of the label widget when it exceeds the '
173+
'available space and both avatar and delete icons are present',
174+
(WidgetTester tester) async {
175+
await _testConstrainedLabel(
176+
tester,
177+
avatar: const CircleAvatar(
178+
child: const Text('A')
179+
),
180+
onDeleted: () {},
181+
);
182+
});
65183
}

0 commit comments

Comments
 (0)