Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Density API to ThemeData, implement for buttons. #43547

Merged
merged 3 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions packages/flutter/lib/src/material/button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class RawMaterialButton extends StatefulWidget {
this.highlightElevation = 8.0,
this.disabledElevation = 0.0,
this.padding = EdgeInsets.zero,
this.visualDensity = const VisualDensity(),
this.constraints = const BoxConstraints(minWidth: 88.0, minHeight: 36.0),
this.shape = const RoundedRectangleBorder(),
this.animationDuration = kThemeChangeDuration,
Expand Down Expand Up @@ -207,6 +208,16 @@ class RawMaterialButton extends StatefulWidget {
/// The internal padding for the button's [child].
final EdgeInsetsGeometry padding;

/// Defines how compact the button's layout will be.
///
/// {@macro flutter.material.themedata.visualDensity}
///
/// See also:
///
/// * [ThemeData.visualDensity], which specifies the [visualDensity] for all widgets
/// within a [Theme].
final VisualDensity visualDensity;

/// Defines the button's size.
///
/// Typically used to constrain the button's minimum size.
Expand Down Expand Up @@ -354,9 +365,22 @@ class _RawMaterialButtonState extends State<RawMaterialButton> {
Widget build(BuildContext context) {
final Color effectiveTextColor = MaterialStateProperty.resolveAs<Color>(widget.textStyle?.color, _states);
final ShapeBorder effectiveShape = MaterialStateProperty.resolveAs<ShapeBorder>(widget.shape, _states);
final Offset densityAdjustment = widget.visualDensity.baseSizeAdjustment;
final BoxConstraints effectiveConstraints = widget.constraints.copyWith(
minWidth: widget.constraints.minWidth != null ? (widget.constraints.minWidth + densityAdjustment.dx).clamp(0.0, double.infinity) : null,
minHeight: widget.constraints.minWidth != null ? (widget.constraints.minHeight + densityAdjustment.dy).clamp(0.0, double.infinity) : null,
);
final EdgeInsetsGeometry padding = widget.padding.add(
gspencergoog marked this conversation as resolved.
Show resolved Hide resolved
EdgeInsets.only(
left: densityAdjustment.dx,
top: densityAdjustment.dy,
right: densityAdjustment.dx,
bottom: densityAdjustment.dy,
),
).clamp(EdgeInsets.zero, EdgeInsetsGeometry.infinity);

final Widget result = ConstrainedBox(
constraints: widget.constraints,
constraints: effectiveConstraints,
child: Material(
elevation: _effectiveElevation,
textStyle: widget.textStyle?.copyWith(color: effectiveTextColor),
Expand All @@ -383,7 +407,7 @@ class _RawMaterialButtonState extends State<RawMaterialButton> {
child: IconTheme.merge(
data: IconThemeData(color: effectiveTextColor),
child: Container(
padding: widget.padding,
padding: padding,
child: Center(
widthFactor: 1.0,
heightFactor: 1.0,
Expand All @@ -397,7 +421,12 @@ class _RawMaterialButtonState extends State<RawMaterialButton> {
Size minSize;
switch (widget.materialTapTargetSize) {
case MaterialTapTargetSize.padded:
minSize = const Size(48.0, 48.0);
minSize = Size(
kMinInteractiveDimension + densityAdjustment.dx,
gspencergoog marked this conversation as resolved.
Show resolved Hide resolved
kMinInteractiveDimension + densityAdjustment.dy,
);
assert(minSize.width >= 0.0);
assert(minSize.height >= 0.0);
break;
case MaterialTapTargetSize.shrinkWrap:
minSize = Size.zero;
Expand Down
3 changes: 3 additions & 0 deletions packages/flutter/lib/src/material/flat_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class FlatButton extends MaterialButton {
Color splashColor,
Brightness colorBrightness,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
ShapeBorder shape,
Clip clipBehavior = Clip.none,
FocusNode focusNode,
Expand All @@ -139,6 +140,7 @@ class FlatButton extends MaterialButton {
splashColor: splashColor,
colorBrightness: colorBrightness,
padding: padding,
visualDensity: visualDensity,
shape: shape,
clipBehavior: clipBehavior,
focusNode: focusNode,
Expand Down Expand Up @@ -199,6 +201,7 @@ class FlatButton extends MaterialButton {
highlightElevation: buttonTheme.getHighlightElevation(this),
disabledElevation: buttonTheme.getDisabledElevation(this),
padding: buttonTheme.getPadding(this),
visualDensity: visualDensity ?? theme.visualDensity,
constraints: buttonTheme.getConstraints(this),
shape: buttonTheme.getShape(this),
clipBehavior: clipBehavior,
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter/lib/src/material/material_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class MaterialButton extends StatelessWidget {
this.highlightElevation,
this.disabledElevation,
this.padding,
this.visualDensity,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
Expand Down Expand Up @@ -311,6 +312,16 @@ class MaterialButton extends StatelessWidget {
/// [ButtonThemeData.padding].
final EdgeInsetsGeometry padding;

/// Defines how compact the button's layout will be.
///
/// {@macro flutter.material.themedata.visualDensity}
///
/// See also:
///
/// * [ThemeData.visualDensity], which specifies the [density] for all widgets
/// within a [Theme].
final VisualDensity visualDensity;

/// The shape of the button's [Material].
///
/// The button's highlight and splash are clipped to this shape. If the
Expand Down Expand Up @@ -387,6 +398,7 @@ class MaterialButton extends StatelessWidget {
hoverElevation: buttonTheme.getHoverElevation(this),
highlightElevation: buttonTheme.getHighlightElevation(this),
padding: buttonTheme.getPadding(this),
visualDensity: visualDensity ?? theme.visualDensity,
constraints: buttonTheme.getConstraints(this).copyWith(
minWidth: minWidth,
minHeight: height,
Expand Down Expand Up @@ -416,6 +428,7 @@ class MaterialButton extends StatelessWidget {
properties.add(ColorProperty('splashColor', splashColor, defaultValue: null));
properties.add(DiagnosticsProperty<Brightness>('colorBrightness', colorBrightness, defaultValue: null));
properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('padding', padding, defaultValue: null));
properties.add(DiagnosticsProperty<VisualDensity>('visualDensity', visualDensity, defaultValue: null));
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
properties.add(DiagnosticsProperty<FocusNode>('focusNode', focusNode, defaultValue: null));
properties.add(DiagnosticsProperty<MaterialTapTargetSize>('materialTapTargetSize', materialTapTargetSize, defaultValue: null));
Expand Down
12 changes: 12 additions & 0 deletions packages/flutter/lib/src/material/outline_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'material_button.dart';
import 'material_state.dart';
import 'raised_button.dart';
import 'theme.dart';
import 'theme_data.dart';

// The total time to make the button's fill color opaque and change
// its elevation. Only applies when highlightElevation > 0.0.
Expand Down Expand Up @@ -75,6 +76,7 @@ class OutlineButton extends MaterialButton {
this.disabledBorderColor,
this.highlightedBorderColor,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
ShapeBorder shape,
Clip clipBehavior = Clip.none,
FocusNode focusNode,
Expand All @@ -97,6 +99,7 @@ class OutlineButton extends MaterialButton {
splashColor: splashColor,
highlightElevation: highlightElevation,
padding: padding,
visualDensity: visualDensity,
shape: shape,
clipBehavior: clipBehavior,
focusNode: focusNode,
Expand Down Expand Up @@ -129,6 +132,7 @@ class OutlineButton extends MaterialButton {
Color disabledBorderColor,
BorderSide borderSide,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
ShapeBorder shape,
Clip clipBehavior,
FocusNode focusNode,
Expand Down Expand Up @@ -187,6 +191,7 @@ class OutlineButton extends MaterialButton {
disabledBorderColor: disabledBorderColor,
highlightedBorderColor: highlightedBorderColor ?? buttonTheme.colorScheme.primary,
padding: buttonTheme.getPadding(this),
visualDensity: visualDensity,
shape: buttonTheme.getShape(this),
clipBehavior: clipBehavior,
focusNode: focusNode,
Expand Down Expand Up @@ -225,6 +230,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi
Color disabledBorderColor,
BorderSide borderSide,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
ShapeBorder shape,
Clip clipBehavior = Clip.none,
FocusNode focusNode,
Expand Down Expand Up @@ -253,6 +259,7 @@ class _OutlineButtonWithIcon extends OutlineButton with MaterialButtonWithIconMi
highlightedBorderColor: highlightedBorderColor,
borderSide: borderSide,
padding: padding,
visualDensity: visualDensity,
shape: shape,
clipBehavior: clipBehavior,
focusNode: focusNode,
Expand Down Expand Up @@ -287,6 +294,7 @@ class _OutlineButton extends StatefulWidget {
this.disabledBorderColor,
@required this.highlightedBorderColor,
this.padding,
this.visualDensity,
this.shape,
this.clipBehavior = Clip.none,
this.focusNode,
Expand Down Expand Up @@ -314,6 +322,7 @@ class _OutlineButton extends StatefulWidget {
final Color disabledBorderColor;
final Color highlightedBorderColor;
final EdgeInsetsGeometry padding;
final VisualDensity visualDensity;
final ShapeBorder shape;
final Clip clipBehavior;
final FocusNode focusNode;
Expand Down Expand Up @@ -435,6 +444,8 @@ class _OutlineButtonState extends State<_OutlineButton> with SingleTickerProvide

@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);

return AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget child) {
Expand All @@ -456,6 +467,7 @@ class _OutlineButtonState extends State<_OutlineButton> with SingleTickerProvide
highlightElevation: _getHighlightElevation(),
onHighlightChanged: _handleHighlightChanged,
padding: widget.padding,
visualDensity: widget.visualDensity ?? theme.visualDensity,
shape: _OutlineBorder(
shape: widget.shape,
side: _getOutline(),
Expand Down
3 changes: 3 additions & 0 deletions packages/flutter/lib/src/material/raised_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class RaisedButton extends MaterialButton {
double highlightElevation,
double disabledElevation,
EdgeInsetsGeometry padding,
VisualDensity visualDensity,
ShapeBorder shape,
Clip clipBehavior = Clip.none,
FocusNode focusNode,
Expand Down Expand Up @@ -161,6 +162,7 @@ class RaisedButton extends MaterialButton {
highlightElevation: highlightElevation,
disabledElevation: disabledElevation,
padding: padding,
visualDensity: visualDensity,
shape: shape,
clipBehavior: clipBehavior,
focusNode: focusNode,
Expand Down Expand Up @@ -227,6 +229,7 @@ class RaisedButton extends MaterialButton {
highlightElevation: buttonTheme.getHighlightElevation(this),
disabledElevation: buttonTheme.getDisabledElevation(this),
padding: buttonTheme.getPadding(this),
visualDensity: visualDensity ?? theme.visualDensity,
constraints: buttonTheme.getConstraints(this),
shape: buttonTheme.getShape(this),
focusNode: focusNode,
Expand Down
Loading