Unverified Commit dd1c22e3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

fix_some_padding (#20967)

parent de7ad06d
...@@ -47,23 +47,37 @@ class ButtonBar extends StatelessWidget { ...@@ -47,23 +47,37 @@ class ButtonBar extends StatelessWidget {
@override @override
Widget build(BuildContext context) { 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. // 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; final double paddingUnit = buttonTheme.padding.horizontal / 4.0;
return Padding( final Widget child = Row(
padding: EdgeInsets.symmetric( mainAxisAlignment: alignment,
vertical: 2.0 * paddingUnit, mainAxisSize: mainAxisSize,
horizontal: paddingUnit children: children.map<Widget>((Widget child) {
), return Padding(
child: Row( padding: EdgeInsets.symmetric(horizontal: paddingUnit),
mainAxisAlignment: alignment, child: child
mainAxisSize: mainAxisSize, );
children: children.map<Widget>((Widget child) { }).toList()
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;
} }
} }
...@@ -26,6 +26,19 @@ enum ButtonTextTheme { ...@@ -26,6 +26,19 @@ enum ButtonTextTheme {
primary, 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. /// Used with [ButtonThemeData] to configure the color and geometry of buttons.
/// ///
/// A button theme can be specified as part of the overall Material theme /// A button theme can be specified as part of the overall Material theme
...@@ -60,6 +73,7 @@ class ButtonTheme extends InheritedWidget { ...@@ -60,6 +73,7 @@ class ButtonTheme extends InheritedWidget {
ButtonTheme({ ButtonTheme({
Key key, Key key,
ButtonTextTheme textTheme = ButtonTextTheme.normal, ButtonTextTheme textTheme = ButtonTextTheme.normal,
ButtonBarLayoutBehavior layoutBehavior = ButtonBarLayoutBehavior.padded,
double minWidth = 88.0, double minWidth = 88.0,
double height = 36.0, double height = 36.0,
EdgeInsetsGeometry padding, EdgeInsetsGeometry padding,
...@@ -70,13 +84,15 @@ class ButtonTheme extends InheritedWidget { ...@@ -70,13 +84,15 @@ class ButtonTheme extends InheritedWidget {
assert(minWidth != null && minWidth >= 0.0), assert(minWidth != null && minWidth >= 0.0),
assert(height != null && height >= 0.0), assert(height != null && height >= 0.0),
assert(alignedDropdown != null), assert(alignedDropdown != null),
assert(layoutBehavior != null),
data = ButtonThemeData( data = ButtonThemeData(
textTheme: textTheme, textTheme: textTheme,
minWidth: minWidth, minWidth: minWidth,
height: height, height: height,
padding: padding, padding: padding,
shape: shape, shape: shape,
alignedDropdown: alignedDropdown alignedDropdown: alignedDropdown,
layoutBehavior: layoutBehavior,
), ),
super(key: key, child: child); super(key: key, child: child);
...@@ -113,6 +129,7 @@ class ButtonTheme extends InheritedWidget { ...@@ -113,6 +129,7 @@ class ButtonTheme extends InheritedWidget {
ShapeBorder shape, ShapeBorder shape,
bool alignedDropdown = false, bool alignedDropdown = false,
Widget child, Widget child,
ButtonBarLayoutBehavior layoutBehavior = ButtonBarLayoutBehavior.padded,
}) : assert(textTheme != null), }) : assert(textTheme != null),
assert(minWidth != null && minWidth >= 0.0), assert(minWidth != null && minWidth >= 0.0),
assert(height != null && height >= 0.0), assert(height != null && height >= 0.0),
...@@ -124,6 +141,7 @@ class ButtonTheme extends InheritedWidget { ...@@ -124,6 +141,7 @@ class ButtonTheme extends InheritedWidget {
padding: padding, padding: padding,
shape: shape, shape: shape,
alignedDropdown: alignedDropdown, alignedDropdown: alignedDropdown,
layoutBehavior: layoutBehavior,
), ),
super(key: key, child: child); super(key: key, child: child);
...@@ -162,11 +180,13 @@ class ButtonThemeData extends Diagnosticable { ...@@ -162,11 +180,13 @@ class ButtonThemeData extends Diagnosticable {
this.height = 36.0, this.height = 36.0,
EdgeInsetsGeometry padding, EdgeInsetsGeometry padding,
ShapeBorder shape, ShapeBorder shape,
this.layoutBehavior = ButtonBarLayoutBehavior.padded,
this.alignedDropdown = false, this.alignedDropdown = false,
}) : assert(textTheme != null), }) : assert(textTheme != null),
assert(minWidth != null && minWidth >= 0.0), assert(minWidth != null && minWidth >= 0.0),
assert(height != null && height >= 0.0), assert(height != null && height >= 0.0),
assert(alignedDropdown != null), assert(alignedDropdown != null),
assert(layoutBehavior != null),
_padding = padding, _padding = padding,
_shape = shape; _shape = shape;
...@@ -187,6 +207,12 @@ class ButtonThemeData extends Diagnosticable { ...@@ -187,6 +207,12 @@ class ButtonThemeData extends Diagnosticable {
/// size, internal padding, and shape. /// size, internal padding, and shape.
final ButtonTextTheme textTheme; 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 /// Simply a convenience that returns [minWidth] and [height] as a
/// [BoxConstraints] object: /// [BoxConstraints] object:
/// ```dart /// ```dart
......
...@@ -14,4 +14,54 @@ void main() { ...@@ -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: <Widget>[
ButtonTheme.bar(
layoutBehavior: ButtonBarLayoutBehavior.constrained,
child: const Directionality(
textDirection: TextDirection.ltr,
child: ButtonBar(
children: <Widget>[
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: <Widget>[
ButtonTheme.bar(
layoutBehavior: ButtonBarLayoutBehavior.padded,
child: const Directionality(
textDirection: TextDirection.ltr,
child: ButtonBar(
children: <Widget>[
SizedBox(width: 10.0, height: 10.0),
],
),
),
),
],
),
),
);
final Finder buttonBar = find.byType(ButtonBar);
expect(tester.getBottomRight(buttonBar).dy - tester.getTopRight(buttonBar).dy, 26.0);
});
} }
...@@ -15,6 +15,7 @@ void main() { ...@@ -15,6 +15,7 @@ void main() {
borderRadius: BorderRadius.all(Radius.circular(2.0)), borderRadius: BorderRadius.all(Radius.circular(2.0)),
)); ));
expect(theme.alignedDropdown, false); expect(theme.alignedDropdown, false);
expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
}); });
test('ButtonThemeData default overrides', () { test('ButtonThemeData default overrides', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment