import "package:flutter/widgets.dart"; import "package:flutter_test/flutter_test.dart"; import "package:garage_ui/garage_ui.dart"; import "support/test_scheme.dart"; // A segmented control lives in a PropertyRow: bounded WIDTH, unbounded height. // That combination is the whole point of these tests - `expands: true` drops // the IntrinsicHeight that gives `stretch` something to stretch to, and the // buttons collapse to nothing. Widget _host(Widget child) => Directionality( textDirection: TextDirection.ltr, child: GarageTheme( data: ThemeData(colorScheme: testScheme, density: const Density()), child: Align( alignment: Alignment.topLeft, child: SizedBox(width: 300, child: child), ), ), ); Widget _seg(String label) => Button( style: const ButtonStyle.outline(), alignment: Alignment.center, onPressed: () {}, child: Text(label), ); void main() { testWidgets("fill splits the main axis evenly", (tester) async { await tester.pumpWidget( _host( ButtonGroup.horizontal( fill: true, children: [_seg("Off"), _seg("Optional"), _seg("Required")], ), ), ); expect(tester.takeException(), isNull); final off = tester.getSize(find.widgetWithText(Button, "Off")); final optional = tester.getSize(find.widgetWithText(Button, "Optional")); final required = tester.getSize(find.widgetWithText(Button, "Required")); // an even share of the 300, not a share of the label lengths expect(off.width, closeTo(100, 1)); expect(optional.width, closeTo(100, 1)); expect(required.width, closeTo(100, 1)); // and a real height, which is what collapsed before expect(off.height, greaterThan(0)); }); testWidgets("it survives a narrow field without overflowing", ( tester, ) async { // a phone-width property row. Three segments that each want ~90px of // label in 200px is where a fill would blow the right edge off. await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: GarageTheme( data: ThemeData(colorScheme: testScheme, density: const Density()), child: Align( alignment: Alignment.topLeft, child: SizedBox( width: 200, child: ButtonGroup.horizontal( fill: true, children: [_seg("Off"), _seg("Optional"), _seg("Required")], ), ), ), ), ), ); expect(tester.takeException(), isNull); final group = tester.getSize(find.byType(ButtonGroup)); expect(group.width, closeTo(200, 1)); for (final label in ["Off", "Optional", "Required"]) { final seg = tester.getSize(find.widgetWithText(Button, label)); expect(seg.width, closeTo(200 / 3, 1), reason: label); expect(seg.height, greaterThan(0), reason: label); } }); testWidgets("without fill it still shrink wraps to its labels", ( tester, ) async { await tester.pumpWidget( _host( ButtonGroup.horizontal( children: [_seg("Off"), _seg("Optional"), _seg("Required")], ), ), ); expect(tester.takeException(), isNull); final off = tester.getSize(find.widgetWithText(Button, "Off")); final required = tester.getSize(find.widgetWithText(Button, "Required")); expect(off.width, lessThan(required.width)); }); }