diff --git a/packages/flutter/lib/src/material/button_bar.dart b/packages/flutter/lib/src/material/button_bar.dart index 4e5298c6f3..54992e1b1e 100644 --- a/packages/flutter/lib/src/material/button_bar.dart +++ b/packages/flutter/lib/src/material/button_bar.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/widgets.dart'; +import 'package:flutter/rendering.dart'; import 'button_bar_theme.dart'; import 'button_theme.dart'; @@ -10,7 +11,8 @@ import 'dialog.dart'; import 'flat_button.dart'; import 'raised_button.dart'; -/// An end-aligned row of buttons. +/// An end-aligned row of buttons, laying out into a column if there is not +/// enough horizontal space. /// /// Places the buttons horizontally according to the [buttonPadding]. The /// children are laid out in a [Row] with [MainAxisAlignment.end]. When the @@ -19,6 +21,13 @@ import 'raised_button.dart'; /// [Directionality] [TextDirection.rtl] the children are left justified and /// the last child becomes the leftmost child. /// +/// If the button bar's width exceeds the maximum width constraint on the +/// widget, it aligns its buttons in a column. The key difference here +/// is that the [MainAxisAlignment] will then be treated as a +/// cross-axis/horizontal alignment. For example, if the buttons overflow and +/// [ButtonBar.alignment] was set to [MainAxisAligment.start], the buttons would +/// align to the horizontal start of the button bar. +/// /// The [ButtonBar] can be configured with a [ButtonBarTheme]. For any null /// property on the ButtonBar, the surrounding ButtonBarTheme's property /// will be used instead. If the ButtonBarTheme's property is null @@ -140,7 +149,7 @@ class ButtonBar extends StatelessWidget { final double paddingUnit = buttonTheme.padding.horizontal / 4.0; final Widget child = ButtonTheme.fromButtonThemeData( data: buttonTheme, - child: Row( + child: _ButtonBarRow( mainAxisAlignment: alignment ?? barTheme.alignment ?? MainAxisAlignment.end, mainAxisSize: mainAxisSize ?? barTheme.mainAxisSize ?? MainAxisSize.max, children: children.map((Widget child) { @@ -172,3 +181,185 @@ class ButtonBar extends StatelessWidget { return null; } } + +/// Attempts to display buttons in a row, but displays them in a column if +/// there is not enough horizontal space. +/// +/// It first attempts to lay out its buttons as though there were no +/// maximumm width constraints on the widget. If the button bar's width is +/// less than the maximum width constraints of the widget, it then lays +/// out the widget as though it were placed in a [Row]. +/// +/// However, if the button bar's width exceeds the maximum width constraint on +/// the widget, it then aligns its buttons in a column. The key difference here +/// is that the [MainAxisAlignment] will then be treated as a +/// cross-axis/horizontal alignment. For example, if the buttons overflow and +/// [ButtonBar.alignment] was set to [MainAxisAligment.start], the column of +/// buttons would align to the horizontal start of the button bar. +class _ButtonBarRow extends Flex { + /// Creates a button bar that attempts to display in a row, but displays in + /// a column if there is insufficient horizontal space. + _ButtonBarRow({ + List children, + Axis direction = Axis.horizontal, + MainAxisSize mainAxisSize = MainAxisSize.max, + MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start, + CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center, + TextDirection textDirection, + VerticalDirection verticalDirection = VerticalDirection.down, + TextBaseline textBaseline, + }) : super( + children: children, + direction: direction, + mainAxisSize: mainAxisSize, + mainAxisAlignment: mainAxisAlignment, + crossAxisAlignment: crossAxisAlignment, + textDirection: textDirection, + verticalDirection: verticalDirection, + textBaseline: textBaseline, + ); + + @override + _RenderButtonBarRow createRenderObject(BuildContext context) { + return _RenderButtonBarRow( + direction: direction, + mainAxisAlignment: mainAxisAlignment, + mainAxisSize: mainAxisSize, + crossAxisAlignment: crossAxisAlignment, + textDirection: getEffectiveTextDirection(context), + verticalDirection: verticalDirection, + textBaseline: textBaseline, + ); + } + + @override + void updateRenderObject(BuildContext context, covariant _RenderButtonBarRow renderObject) { + renderObject + ..direction = direction + ..mainAxisAlignment = mainAxisAlignment + ..mainAxisSize = mainAxisSize + ..crossAxisAlignment = crossAxisAlignment + ..textDirection = getEffectiveTextDirection(context) + ..verticalDirection = verticalDirection + ..textBaseline = textBaseline; + } +} + +/// Attempts to display buttons in a row, but displays them in a column if +/// there is not enough horizontal space. +/// +/// It first attempts to lay out its buttons as though there were no +/// maximumm width constraints on the widget. If the button bar's width is +/// less than the maximum width constraints of the widget, it then lays +/// out the widget as though it were placed in a [Row]. +/// +/// However, if the button bar's width exceeds the maximum width constraint on +/// the widget, it then aligns its buttons in a column. The key difference here +/// is that the [MainAxisAlignment] will then be treated as a +/// cross-axis/horizontal alignment. For example, if the buttons overflow and +/// [ButtonBar.alignment] was set to [MainAxisAligment.start], the buttons would +/// align to the horizontal start of the button bar. +class _RenderButtonBarRow extends RenderFlex { + /// Creates a button bar that attempts to display in a row, but displays in + /// a column if there is insufficient horizontal space. + _RenderButtonBarRow({ + List children, + Axis direction = Axis.horizontal, + MainAxisSize mainAxisSize = MainAxisSize.max, + MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start, + CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center, + @required TextDirection textDirection, + VerticalDirection verticalDirection = VerticalDirection.down, + TextBaseline textBaseline, + }) : assert(textDirection != null), + super( + children: children, + direction: direction, + mainAxisSize: mainAxisSize, + mainAxisAlignment: mainAxisAlignment, + crossAxisAlignment: crossAxisAlignment, + textDirection: textDirection, + verticalDirection: verticalDirection, + textBaseline: textBaseline, + ); + + bool _hasCheckedLayoutWidth = false; + + @override + BoxConstraints get constraints { + if (_hasCheckedLayoutWidth) + return super.constraints; + return super.constraints.copyWith(maxWidth: double.infinity); + } + + @override + void performLayout() { + // Set check layout width to false in reload or update cases. + _hasCheckedLayoutWidth = false; + + // Perform layout to ensure that button bar knows how wide it would + // ideally want to be. + super.performLayout(); + _hasCheckedLayoutWidth = true; + + // If the button bar is constrained by width and it overflows, set the + // buttons to align vertically. Otherwise, lay out the button bar + // horizontally. + if (size.width <= constraints.maxWidth) { + // A second performLayout is required to ensure that the original maximum + // width constraints are used. The original perform layout call assumes + // a maximum width constraint of infinity. + super.performLayout(); + } else { + final BoxConstraints childConstraints = constraints.copyWith(minWidth: 0.0); + RenderBox child = firstChild; + double currentHeight = 0.0; + + while (child != null) { + final FlexParentData childParentData = child.parentData; + + // Lay out the child with the button bar's original constraints, but + // with minimum width set to zero. + child.layout(childConstraints, parentUsesSize: true); + + // Set the cross axis alignment for the column to match the main axis + // alignment for a row. For [MainAxisAligment.spaceAround], + // [MainAxisAligment.spaceBetween] and [MainAxisAlignment.spaceEvenly] + // cases, use [MainAxisAligmnent.start]. + switch (textDirection) { + case TextDirection.ltr: + switch (mainAxisAlignment) { + case MainAxisAlignment.center: + final double midpoint = (constraints.maxWidth - child.size.width) / 2.0; + childParentData.offset = Offset(midpoint, currentHeight); + break; + case MainAxisAlignment.end: + childParentData.offset = Offset(constraints.maxWidth - child.size.width, currentHeight); + break; + default: + childParentData.offset = Offset(0, currentHeight); + break; + } + break; + case TextDirection.rtl: + switch (mainAxisAlignment) { + case MainAxisAlignment.center: + final double midpoint = constraints.maxWidth / 2.0 - child.size.width / 2.0; + childParentData.offset = Offset(midpoint, currentHeight); + break; + case MainAxisAlignment.end: + childParentData.offset = Offset(0, currentHeight); + break; + default: + childParentData.offset = Offset(constraints.maxWidth - child.size.width, currentHeight); + break; + } + break; + } + currentHeight += child.size.height; + child = childParentData.nextSibling; + } + size = constraints.constrain(Size(constraints.maxWidth, currentHeight)); + } + } +} diff --git a/packages/flutter/test/material/button_bar_test.dart b/packages/flutter/test/material/button_bar_test.dart index 05200f8d42..cc6606b9b7 100644 --- a/packages/flutter/test/material/button_bar_test.dart +++ b/packages/flutter/test/material/button_bar_test.dart @@ -83,65 +83,159 @@ void main() { group('mainAxisSize', () { - testWidgets('default mainAxisSize is MainAxisSize.max', (WidgetTester tester) async { + testWidgets('Default mainAxisSize is MainAxisSize.max', (WidgetTester tester) async { + const Key buttonBarKey = Key('row'); + const Key child0Key = Key('child0'); + const Key child1Key = Key('child1'); + const Key child2Key = Key('child2'); + await tester.pumpWidget( MaterialApp( - home: ButtonBar( - children: [ - Container(), - ], + home: Center( + child: ButtonBar( + key: buttonBarKey, + // buttonPadding set to zero to simplify test calculations. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: child0Key, width: 100.0, height: 100.0), + Container(key: child1Key, width: 100.0, height: 100.0), + Container(key: child2Key, width: 100.0, height: 100.0), + ], + ), ), ), ); - // ButtonBar uses a Row internally to implement this - final Row row = tester.widget(find.byType(Row)); - expect(row.mainAxisSize, equals(MainAxisSize.max)); + // ButtonBar should take up all the space it is provided by its parent. + final Rect buttonBarRect = tester.getRect(find.byKey(buttonBarKey)); + expect(buttonBarRect.size.width, equals(800.0)); + expect(buttonBarRect.size.height, equals(100.0)); + + // The children of [ButtonBar] are aligned by [MainAxisAligment.end] by + // default. + Rect childRect; + childRect = tester.getRect(find.byKey(child0Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0 - 200.0); + + childRect = tester.getRect(find.byKey(child1Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0 - 100.0); + + childRect = tester.getRect(find.byKey(child2Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0); }); testWidgets('ButtonBarTheme.mainAxisSize overrides default', (WidgetTester tester) async { + const Key buttonBarKey = Key('row'); + const Key child0Key = Key('child0'); + const Key child1Key = Key('child1'); + const Key child2Key = Key('child2'); await tester.pumpWidget( MaterialApp( home: ButtonBarTheme( data: const ButtonBarThemeData( mainAxisSize: MainAxisSize.min, ), - child: ButtonBar( - children: [ - Container(), - ], + child: Center( + child: ButtonBar( + key: buttonBarKey, + // buttonPadding set to zero to simplify test calculations. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: child0Key, width: 100.0, height: 100.0), + Container(key: child1Key, width: 100.0, height: 100.0), + Container(key: child2Key, width: 100.0, height: 100.0), + ], + ), ), ), ), ); - // ButtonBar uses a Row internally to implement this - final Row row = tester.widget(find.byType(Row)); - expect(row.mainAxisSize, equals(MainAxisSize.min)); + // ButtonBar should take up minimum space it requires. + final Rect buttonBarRect = tester.getRect(find.byKey(buttonBarKey)); + expect(buttonBarRect.size.width, equals(300.0)); + expect(buttonBarRect.size.height, equals(100.0)); + + Rect childRect; + childRect = tester.getRect(find.byKey(child0Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + // Should be a center aligned because of [Center] widget. + // First child is on the left side of the button bar. + expect(childRect.left, (800.0 - buttonBarRect.width) / 2.0); + + childRect = tester.getRect(find.byKey(child1Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + // Should be a center aligned because of [Center] widget. + // Second child is on the center the button bar. + expect(childRect.left, ((800.0 - buttonBarRect.width) / 2.0) + 100.0); + + childRect = tester.getRect(find.byKey(child2Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + // Should be a center aligned because of [Center] widget. + // Third child is on the right side of the button bar. + expect(childRect.left, ((800.0 - buttonBarRect.width) / 2.0) + 200.0); }); testWidgets('ButtonBar.mainAxisSize overrides ButtonBarTheme.mainAxisSize and default', (WidgetTester tester) async { + const Key buttonBarKey = Key('row'); + const Key child0Key = Key('child0'); + const Key child1Key = Key('child1'); + const Key child2Key = Key('child2'); await tester.pumpWidget( MaterialApp( home: ButtonBarTheme( data: const ButtonBarThemeData( mainAxisSize: MainAxisSize.min, ), - child: ButtonBar( - mainAxisSize: MainAxisSize.max, - children: [ - Container(), - ], + child: Center( + child: ButtonBar( + key: buttonBarKey, + // buttonPadding set to zero to simplify test calculations. + buttonPadding: EdgeInsets.zero, + mainAxisSize: MainAxisSize.max, + children: [ + Container(key: child0Key, width: 100.0, height: 100.0), + Container(key: child1Key, width: 100.0, height: 100.0), + Container(key: child2Key, width: 100.0, height: 100.0), + ], + ), ), ), ), ); - // ButtonBar uses a Row internally to implement this - final Row row = tester.widget(find.byType(Row)); - expect(row.mainAxisSize, equals(MainAxisSize.max)); - }); + // ButtonBar should take up all the space it is provided by its parent. + final Rect buttonBarRect = tester.getRect(find.byKey(buttonBarKey)); + expect(buttonBarRect.size.width, equals(800.0)); + expect(buttonBarRect.size.height, equals(100.0)); + // The children of [ButtonBar] are aligned by [MainAxisAligment.end] by + // default. + Rect childRect; + childRect = tester.getRect(find.byKey(child0Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0 - 200.0); + + childRect = tester.getRect(find.byKey(child1Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0 - 100.0); + + childRect = tester.getRect(find.byKey(child2Key)); + expect(childRect.size.width, equals(100.0)); + expect(childRect.size.height, equals(100.0)); + expect(childRect.right, 800.0); + }); }); group('button properies override ButtonTheme', () { @@ -290,7 +384,165 @@ void main() { final Finder buttonBar = find.byType(ButtonBar); expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 26.0); }); - }); + group("ButtonBar's children wrap when they overflow horizontally", () { + testWidgets("ButtonBar's children wrap when buttons overflow", (WidgetTester tester) async { + final Key keyOne = UniqueKey(); + final Key keyTwo = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + children: [ + Container(key: keyOne, height: 50.0, width: 800.0), + Container(key: keyTwo, height: 50.0, width: 800.0), + ], + ), + ), + ); + + // Second [Container] should wrap around to the next column since + // they take up max width constraint. + final Rect containerOneRect = tester.getRect(find.byKey(keyOne)); + final Rect containerTwoRect = tester.getRect(find.byKey(keyTwo)); + expect(containerOneRect.bottom, containerTwoRect.top); + expect(containerOneRect.left, containerTwoRect.left); + }); + + testWidgets( + "ButtonBar's children overflow defaults - MainAxisAlignment.end", (WidgetTester tester) async { + final Key keyOne = UniqueKey(); + final Key keyTwo = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + // Set padding to zero to align buttons with edge of button bar. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: keyOne, height: 50.0, width: 500.0), + Container(key: keyTwo, height: 50.0, width: 500.0), + ], + ), + ), + ); + + final Rect buttonBarRect = tester.getRect(find.byType(ButtonBar)); + final Rect containerOneRect = tester.getRect(find.byKey(keyOne)); + final Rect containerTwoRect = tester.getRect(find.byKey(keyTwo)); + // Second [Container] should wrap around to the next row. + expect(containerOneRect.bottom, containerTwoRect.top); + // Second [Container] should align to the start of the ButtonBar. + expect(containerOneRect.right, containerTwoRect.right); + expect(containerOneRect.right, buttonBarRect.right); + }, + ); + + testWidgets("ButtonBar's children overflow - MainAxisAlignment.start", (WidgetTester tester) async { + final Key keyOne = UniqueKey(); + final Key keyTwo = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + alignment: MainAxisAlignment.start, + // Set padding to zero to align buttons with edge of button bar. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: keyOne, height: 50.0, width: 500.0), + Container(key: keyTwo, height: 50.0, width: 500.0), + ], + ), + ), + ); + + final Rect buttonBarRect = tester.getRect(find.byType(ButtonBar)); + final Rect containerOneRect = tester.getRect(find.byKey(keyOne)); + final Rect containerTwoRect = tester.getRect(find.byKey(keyTwo)); + // Second [Container] should wrap around to the next row. + expect(containerOneRect.bottom, containerTwoRect.top); + // [Container]s should align to the end of the ButtonBar. + expect(containerOneRect.left, containerTwoRect.left); + expect(containerOneRect.left, buttonBarRect.left); + }); + + testWidgets("ButtonBar's children overflow - MainAxisAlignment.center", (WidgetTester tester) async { + final Key keyOne = UniqueKey(); + final Key keyTwo = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + alignment: MainAxisAlignment.center, + // Set padding to zero to align buttons with edge of button bar. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: keyOne, height: 50.0, width: 500.0), + Container(key: keyTwo, height: 50.0, width: 500.0), + ], + ), + ), + ); + + final Rect buttonBarRect = tester.getRect(find.byType(ButtonBar)); + final Rect containerOneRect = tester.getRect(find.byKey(keyOne)); + final Rect containerTwoRect = tester.getRect(find.byKey(keyTwo)); + // Second [Container] should wrap around to the next row. + expect(containerOneRect.bottom, containerTwoRect.top); + // [Container]s should center themselves in the ButtonBar. + expect(containerOneRect.center.dx, containerTwoRect.center.dx); + expect(containerOneRect.center.dx, buttonBarRect.center.dx); + }); + + testWidgets( + "ButtonBar's children default to MainAxisAlignment.start for horizontal " + 'alignment when overflowing in spaceBetween, spaceAround and spaceEvenly ' + 'cases when overflowing.', (WidgetTester tester) async { + final Key keyOne = UniqueKey(); + final Key keyTwo = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + alignment: MainAxisAlignment.spaceEvenly, + // Set padding to zero to align buttons with edge of button bar. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: keyOne, height: 50.0, width: 500.0), + Container(key: keyTwo, height: 50.0, width: 500.0), + ], + ), + ), + ); + + Rect buttonBarRect = tester.getRect(find.byType(ButtonBar)); + Rect containerOneRect = tester.getRect(find.byKey(keyOne)); + Rect containerTwoRect = tester.getRect(find.byKey(keyTwo)); + // Second [Container] should wrap around to the next row. + expect(containerOneRect.bottom, containerTwoRect.top); + // Should align horizontally to the start of the button bar. + expect(containerOneRect.left, containerTwoRect.left); + expect(containerOneRect.left, buttonBarRect.left); + + await tester.pumpWidget( + MaterialApp( + home: ButtonBar( + alignment: MainAxisAlignment.spaceAround, + // Set padding to zero to align buttons with edge of button bar. + buttonPadding: EdgeInsets.zero, + children: [ + Container(key: keyOne, height: 50.0, width: 500.0), + Container(key: keyTwo, height: 50.0, width: 500.0), + ], + ), + ), + ); + + buttonBarRect = tester.getRect(find.byType(ButtonBar)); + containerOneRect = tester.getRect(find.byKey(keyOne)); + containerTwoRect = tester.getRect(find.byKey(keyTwo)); + // Second [Container] should wrap around to the next row. + expect(containerOneRect.bottom, containerTwoRect.top); + // Should align horizontally to the start of the button bar. + expect(containerOneRect.left, containerTwoRect.left); + expect(containerOneRect.left, buttonBarRect.left); + }, + ); + }); }