From dd1c22e3893d8300467b260c36760494339343c0 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 12 Sep 2018 14:07:02 -0700 Subject: [PATCH] fix_some_padding (#20967) --- .../flutter/lib/src/material/button_bar.dart | 46 +++++++++++------ .../lib/src/material/button_theme.dart | 28 ++++++++++- .../test/material/button_bar_test.dart | 50 +++++++++++++++++++ .../test/material/button_theme_test.dart | 1 + 4 files changed, 108 insertions(+), 17 deletions(-) diff --git a/packages/flutter/lib/src/material/button_bar.dart b/packages/flutter/lib/src/material/button_bar.dart index f4a3af3c86..ec7d013425 100644 --- a/packages/flutter/lib/src/material/button_bar.dart +++ b/packages/flutter/lib/src/material/button_bar.dart @@ -47,23 +47,37 @@ class ButtonBar extends StatelessWidget { @override Widget build(BuildContext context) { + final ButtonThemeData buttonTheme = ButtonTheme.of(context); // We divide by 4.0 because we want half of the average of the left and right padding. - final double paddingUnit = ButtonTheme.of(context).padding.horizontal / 4.0; - return Padding( - padding: EdgeInsets.symmetric( - vertical: 2.0 * paddingUnit, - horizontal: paddingUnit - ), - child: Row( - mainAxisAlignment: alignment, - mainAxisSize: mainAxisSize, - children: children.map((Widget child) { - return Padding( - padding: EdgeInsets.symmetric(horizontal: paddingUnit), - child: child - ); - }).toList() - ) + final double paddingUnit = buttonTheme.padding.horizontal / 4.0; + final Widget child = Row( + mainAxisAlignment: alignment, + mainAxisSize: mainAxisSize, + children: children.map((Widget child) { + return Padding( + padding: EdgeInsets.symmetric(horizontal: paddingUnit), + child: child + ); + }).toList() ); + switch (buttonTheme.layoutBehavior) { + case ButtonBarLayoutBehavior.padded: + return Padding( + padding: EdgeInsets.symmetric( + vertical: 2.0 * paddingUnit, + horizontal: paddingUnit + ), + child: child, + ); + case ButtonBarLayoutBehavior.constrained: + return Container( + padding: EdgeInsets.symmetric(horizontal: paddingUnit), + constraints: const BoxConstraints(minHeight: 52.0), + alignment: Alignment.center, + child: child, + ); + } + assert(false); + return null; } } diff --git a/packages/flutter/lib/src/material/button_theme.dart b/packages/flutter/lib/src/material/button_theme.dart index 51e52b15fc..7f4ae92561 100644 --- a/packages/flutter/lib/src/material/button_theme.dart +++ b/packages/flutter/lib/src/material/button_theme.dart @@ -26,6 +26,19 @@ enum ButtonTextTheme { primary, } +/// Used with [ButtonTheme] and [ButtonThemeData] to define how the button bar +/// should size itself with either constraints or internal padding. +enum ButtonBarLayoutBehavior { + /// Button bars will be constrained to a minimum height of 52. + /// + /// This setting is require to create button bars which conform to the + /// material specification. + constrained, + + /// Button bars will calculate their padding from the button theme padding. + padded, +} + /// Used with [ButtonThemeData] to configure the color and geometry of buttons. /// /// A button theme can be specified as part of the overall Material theme @@ -60,6 +73,7 @@ class ButtonTheme extends InheritedWidget { ButtonTheme({ Key key, ButtonTextTheme textTheme = ButtonTextTheme.normal, + ButtonBarLayoutBehavior layoutBehavior = ButtonBarLayoutBehavior.padded, double minWidth = 88.0, double height = 36.0, EdgeInsetsGeometry padding, @@ -70,13 +84,15 @@ class ButtonTheme extends InheritedWidget { assert(minWidth != null && minWidth >= 0.0), assert(height != null && height >= 0.0), assert(alignedDropdown != null), + assert(layoutBehavior != null), data = ButtonThemeData( textTheme: textTheme, minWidth: minWidth, height: height, padding: padding, shape: shape, - alignedDropdown: alignedDropdown + alignedDropdown: alignedDropdown, + layoutBehavior: layoutBehavior, ), super(key: key, child: child); @@ -113,6 +129,7 @@ class ButtonTheme extends InheritedWidget { ShapeBorder shape, bool alignedDropdown = false, Widget child, + ButtonBarLayoutBehavior layoutBehavior = ButtonBarLayoutBehavior.padded, }) : assert(textTheme != null), assert(minWidth != null && minWidth >= 0.0), assert(height != null && height >= 0.0), @@ -124,6 +141,7 @@ class ButtonTheme extends InheritedWidget { padding: padding, shape: shape, alignedDropdown: alignedDropdown, + layoutBehavior: layoutBehavior, ), super(key: key, child: child); @@ -162,11 +180,13 @@ class ButtonThemeData extends Diagnosticable { this.height = 36.0, EdgeInsetsGeometry padding, ShapeBorder shape, + this.layoutBehavior = ButtonBarLayoutBehavior.padded, this.alignedDropdown = false, }) : assert(textTheme != null), assert(minWidth != null && minWidth >= 0.0), assert(height != null && height >= 0.0), assert(alignedDropdown != null), + assert(layoutBehavior != null), _padding = padding, _shape = shape; @@ -187,6 +207,12 @@ class ButtonThemeData extends Diagnosticable { /// size, internal padding, and shape. final ButtonTextTheme textTheme; + /// Defines whether a button bar should size itself with a minimum size + /// constraint or padding. + /// + /// Defaults to [ButtonBarLayoutBehavior.padded]. + final ButtonBarLayoutBehavior layoutBehavior; + /// Simply a convenience that returns [minWidth] and [height] as a /// [BoxConstraints] object: /// ```dart diff --git a/packages/flutter/test/material/button_bar_test.dart b/packages/flutter/test/material/button_bar_test.dart index 5b9d5b6d45..6b3476917e 100644 --- a/packages/flutter/test/material/button_bar_test.dart +++ b/packages/flutter/test/material/button_bar_test.dart @@ -14,4 +14,54 @@ void main() { ), ); }); + + testWidgets('ButtonBar has a min height of 52 when using ButtonBarLayoutBehavior.constrained', (WidgetTester tester) async { + await tester.pumpWidget( + SingleChildScrollView( + child: ListBody( + children: [ + ButtonTheme.bar( + layoutBehavior: ButtonBarLayoutBehavior.constrained, + child: const Directionality( + textDirection: TextDirection.ltr, + child: ButtonBar( + children: [ + SizedBox(width: 10.0, height: 10.0), + ], + ), + ), + ), + ], + ), + ), + ); + + final Finder buttonBar = find.byType(ButtonBar); + expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 52.0); + }); + + testWidgets('ButtonBar has padding applied when using ButtonBarLayoutBehavior.padded', (WidgetTester tester) async { + await tester.pumpWidget( + SingleChildScrollView( + child: ListBody( + children: [ + ButtonTheme.bar( + layoutBehavior: ButtonBarLayoutBehavior.padded, + child: const Directionality( + textDirection: TextDirection.ltr, + child: ButtonBar( + children: [ + SizedBox(width: 10.0, height: 10.0), + ], + ), + ), + ), + ], + ), + ), + ); + + final Finder buttonBar = find.byType(ButtonBar); + expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 26.0); + }); } diff --git a/packages/flutter/test/material/button_theme_test.dart b/packages/flutter/test/material/button_theme_test.dart index ff6749254f..b8e6fa1da7 100644 --- a/packages/flutter/test/material/button_theme_test.dart +++ b/packages/flutter/test/material/button_theme_test.dart @@ -15,6 +15,7 @@ void main() { borderRadius: BorderRadius.all(Radius.circular(2.0)), )); expect(theme.alignedDropdown, false); + expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded); }); test('ButtonThemeData default overrides', () {