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 {
@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>((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>((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;
}
}
......@@ -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
......
......@@ -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() {
borderRadius: BorderRadius.all(Radius.circular(2.0)),
));
expect(theme.alignedDropdown, false);
expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
});
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