diff --git a/packages/flutter/lib/src/material/elevated_button.dart b/packages/flutter/lib/src/material/elevated_button.dart index 94d8e8f8c0..66ea7c645e 100644 --- a/packages/flutter/lib/src/material/elevated_button.dart +++ b/packages/flutter/lib/src/material/elevated_button.dart @@ -467,7 +467,7 @@ class _ElevatedButtonWithIconChild extends StatelessWidget { final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, - children: [icon, SizedBox(width: gap), label], + children: [icon, SizedBox(width: gap), Flexible(child: label)], ); } } diff --git a/packages/flutter/lib/src/material/outlined_button.dart b/packages/flutter/lib/src/material/outlined_button.dart index 9875a18693..537aaa07ea 100644 --- a/packages/flutter/lib/src/material/outlined_button.dart +++ b/packages/flutter/lib/src/material/outlined_button.dart @@ -373,7 +373,7 @@ class _OutlinedButtonWithIconChild extends StatelessWidget { final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, - children: [icon, SizedBox(width: gap), label], + children: [icon, SizedBox(width: gap), Flexible(child: label)], ); } } diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index 7642435f57..499829d6c8 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -464,7 +464,7 @@ class _TextButtonWithIconChild extends StatelessWidget { final double gap = scale <= 1 ? 8 : lerpDouble(8, 4, math.min(scale - 1, 1))!; return Row( mainAxisSize: MainAxisSize.min, - children: [icon, SizedBox(width: gap), label], + children: [icon, SizedBox(width: gap), Flexible(child: label)], ); } } diff --git a/packages/flutter/test/material/elevated_button_test.dart b/packages/flutter/test/material/elevated_button_test.dart index 6b35b4c5fa..5aafee697e 100644 --- a/packages/flutter/test/material/elevated_button_test.dart +++ b/packages/flutter/test/material/elevated_button_test.dart @@ -1114,6 +1114,64 @@ void main() { await tester.pumpAndSettle(); } }); + + testWidgets('ElevatedButton.icon does not overflow', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/77815 + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: ElevatedButton.icon( + onPressed: () {}, + icon: const Icon(Icons.add), + label: const Text( // Much wider than 200 + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a euismod nibh. Morbi laoreet purus.', + ), + ), + ), + ), + ), + ); + expect(tester.takeException(), null); + }); + + testWidgets('ElevatedButton.icon icon,label layout', (WidgetTester tester) async { + final Key buttonKey = UniqueKey(); + final Key iconKey = UniqueKey(); + final Key labelKey = UniqueKey(); + final ButtonStyle style = ElevatedButton.styleFrom( + padding: EdgeInsets.zero, + visualDensity: const VisualDensity(), // dx=0, dy=0 + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: ElevatedButton.icon( + key: buttonKey, + style: style, + onPressed: () {}, + icon: SizedBox(key: iconKey, width: 50, height: 100), + label: SizedBox(key: labelKey, width: 50, height: 100), + ), + ), + ), + ), + ); + + // The button's label and icon are separated by a gap of 8: + // 46 [icon 50] 8 [label 50] 46 + // The overall button width is 200. So: + // icon.x = 46 + // label.x = 46 + 50 + 8 = 104 + + expect(tester.getRect(find.byKey(buttonKey)), const Rect.fromLTRB(0.0, 0.0, 200.0, 100.0)); + expect(tester.getRect(find.byKey(iconKey)), const Rect.fromLTRB(46.0, 0.0, 96.0, 100.0)); + expect(tester.getRect(find.byKey(labelKey)), const Rect.fromLTRB(104.0, 0.0, 154.0, 100.0)); + }); } TextStyle _iconStyle(WidgetTester tester, IconData icon) { diff --git a/packages/flutter/test/material/outlined_button_test.dart b/packages/flutter/test/material/outlined_button_test.dart index 428271c647..b10b12b230 100644 --- a/packages/flutter/test/material/outlined_button_test.dart +++ b/packages/flutter/test/material/outlined_button_test.dart @@ -1290,6 +1290,64 @@ void main() { await tester.pumpAndSettle(); } }); + + testWidgets('OultinedButton.icon does not overflow', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/77815 + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: OutlinedButton.icon( + onPressed: () {}, + icon: const Icon(Icons.add), + label: const Text( // Much wider than 200 + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a euismod nibh. Morbi laoreet purus.', + ), + ), + ), + ), + ), + ); + expect(tester.takeException(), null); + }); + + testWidgets('OultinedButton.icon icon,label layout', (WidgetTester tester) async { + final Key buttonKey = UniqueKey(); + final Key iconKey = UniqueKey(); + final Key labelKey = UniqueKey(); + final ButtonStyle style = OutlinedButton.styleFrom( + padding: EdgeInsets.zero, + visualDensity: const VisualDensity(), // dx=0, dy=0 + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: OutlinedButton.icon( + key: buttonKey, + style: style, + onPressed: () {}, + icon: SizedBox(key: iconKey, width: 50, height: 100), + label: SizedBox(key: labelKey, width: 50, height: 100), + ), + ), + ), + ), + ); + + // The button's label and icon are separated by a gap of 8: + // 46 [icon 50] 8 [label 50] 46 + // The overall button width is 200. So: + // icon.x = 46 + // label.x = 46 + 50 + 8 = 104 + + expect(tester.getRect(find.byKey(buttonKey)), const Rect.fromLTRB(0.0, 0.0, 200.0, 100.0)); + expect(tester.getRect(find.byKey(iconKey)), const Rect.fromLTRB(46.0, 0.0, 96.0, 100.0)); + expect(tester.getRect(find.byKey(labelKey)), const Rect.fromLTRB(104.0, 0.0, 154.0, 100.0)); + }); } PhysicalModelLayer _findPhysicalLayer(Element element) { diff --git a/packages/flutter/test/material/text_button_test.dart b/packages/flutter/test/material/text_button_test.dart index 7047df212b..dd3daed969 100644 --- a/packages/flutter/test/material/text_button_test.dart +++ b/packages/flutter/test/material/text_button_test.dart @@ -1087,6 +1087,64 @@ void main() { await tester.pumpAndSettle(); } }); + + testWidgets('TextButton.icon does not overflow', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/77815 + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: TextButton.icon( + onPressed: () {}, + icon: const Icon(Icons.add), + label: const Text( // Much wider than 200 + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a euismod nibh. Morbi laoreet purus.', + ), + ), + ), + ), + ), + ); + expect(tester.takeException(), null); + }); + + testWidgets('TextButton.icon icon,label layout', (WidgetTester tester) async { + final Key buttonKey = UniqueKey(); + final Key iconKey = UniqueKey(); + final Key labelKey = UniqueKey(); + final ButtonStyle style = TextButton.styleFrom( + padding: EdgeInsets.zero, + visualDensity: const VisualDensity(), // dx=0, dy=0 + ); + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SizedBox( + width: 200, + child: TextButton.icon( + key: buttonKey, + style: style, + onPressed: () {}, + icon: SizedBox(key: iconKey, width: 50, height: 100), + label: SizedBox(key: labelKey, width: 50, height: 100), + ), + ), + ), + ), + ); + + // The button's label and icon are separated by a gap of 8: + // 46 [icon 50] 8 [label 50] 46 + // The overall button width is 200. So: + // icon.x = 46 + // label.x = 46 + 50 + 8 = 104 + + expect(tester.getRect(find.byKey(buttonKey)), const Rect.fromLTRB(0.0, 0.0, 200.0, 100.0)); + expect(tester.getRect(find.byKey(iconKey)), const Rect.fromLTRB(46.0, 0.0, 96.0, 100.0)); + expect(tester.getRect(find.byKey(labelKey)), const Rect.fromLTRB(104.0, 0.0, 154.0, 100.0)); + }); } TextStyle? _iconStyle(WidgetTester tester, IconData icon) {