Refactor Material build method (#147430)
Only a code quality change, improves readability using `switch`.
This commit is contained in:
@@ -431,31 +431,16 @@ class Material extends StatefulWidget {
|
|||||||
class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
||||||
final GlobalKey _inkFeatureRenderer = GlobalKey(debugLabel: 'ink renderer');
|
final GlobalKey _inkFeatureRenderer = GlobalKey(debugLabel: 'ink renderer');
|
||||||
|
|
||||||
Color? _getBackgroundColor(BuildContext context) {
|
|
||||||
final ThemeData theme = Theme.of(context);
|
|
||||||
Color? color = widget.color;
|
|
||||||
if (color == null) {
|
|
||||||
switch (widget.type) {
|
|
||||||
case MaterialType.canvas:
|
|
||||||
color = theme.canvasColor;
|
|
||||||
case MaterialType.card:
|
|
||||||
color = theme.cardColor;
|
|
||||||
case MaterialType.button:
|
|
||||||
case MaterialType.circle:
|
|
||||||
case MaterialType.transparency:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return color;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final ThemeData theme = Theme.of(context);
|
final ThemeData theme = Theme.of(context);
|
||||||
final Color? backgroundColor = _getBackgroundColor(context);
|
final Color? backgroundColor = widget.color ?? switch (widget.type) {
|
||||||
final Color modelShadowColor = widget.shadowColor ?? (theme.useMaterial3 ? theme.colorScheme.shadow : theme.shadowColor);
|
MaterialType.canvas => theme.canvasColor,
|
||||||
// If no shadow color is specified, use 0 for elevation in the model so a drop shadow won't be painted.
|
MaterialType.card => theme.cardColor,
|
||||||
final double modelElevation = widget.elevation;
|
MaterialType.button || MaterialType.circle || MaterialType.transparency => null,
|
||||||
|
};
|
||||||
|
final Color modelShadowColor = widget.shadowColor
|
||||||
|
?? (theme.useMaterial3 ? theme.colorScheme.shadow : theme.shadowColor);
|
||||||
assert(
|
assert(
|
||||||
backgroundColor != null || widget.type == MaterialType.transparency,
|
backgroundColor != null || widget.type == MaterialType.transparency,
|
||||||
'If Material type is not MaterialType.transparency, a color must '
|
'If Material type is not MaterialType.transparency, a color must '
|
||||||
@@ -463,6 +448,7 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
'in the theme (ex. canvasColor != null if type is set to '
|
'in the theme (ex. canvasColor != null if type is set to '
|
||||||
'MaterialType.canvas)',
|
'MaterialType.canvas)',
|
||||||
);
|
);
|
||||||
|
|
||||||
Widget? contents = widget.child;
|
Widget? contents = widget.child;
|
||||||
if (contents != null) {
|
if (contents != null) {
|
||||||
contents = AnimatedDefaultTextStyle(
|
contents = AnimatedDefaultTextStyle(
|
||||||
@@ -486,6 +472,10 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ShapeBorder? shape = widget.borderRadius != null
|
||||||
|
? RoundedRectangleBorder(borderRadius: widget.borderRadius!)
|
||||||
|
: widget.shape;
|
||||||
|
|
||||||
// PhysicalModel has a temporary workaround for a performance issue that
|
// PhysicalModel has a temporary workaround for a performance issue that
|
||||||
// speeds up rectangular non transparent material (the workaround is to
|
// speeds up rectangular non transparent material (the workaround is to
|
||||||
// skip the call to ui.Canvas.saveLayer if the border radius is 0).
|
// skip the call to ui.Canvas.saveLayer if the border radius is 0).
|
||||||
@@ -495,8 +485,8 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
// specified rectangles (e.g shape RoundedRectangleBorder with radius 0, but
|
// specified rectangles (e.g shape RoundedRectangleBorder with radius 0, but
|
||||||
// we choose not to as we want the change from the fast-path to the
|
// we choose not to as we want the change from the fast-path to the
|
||||||
// slow-path to be noticeable in the construction site of Material.
|
// slow-path to be noticeable in the construction site of Material.
|
||||||
if (widget.type == MaterialType.canvas && widget.shape == null && widget.borderRadius == null) {
|
if (widget.type == MaterialType.canvas && shape == null) {
|
||||||
final Color color = Theme.of(context).useMaterial3
|
final Color color = theme.useMaterial3
|
||||||
? ElevationOverlay.applySurfaceTint(backgroundColor!, widget.surfaceTintColor, widget.elevation)
|
? ElevationOverlay.applySurfaceTint(backgroundColor!, widget.surfaceTintColor, widget.elevation)
|
||||||
: ElevationOverlay.applyOverlay(context, backgroundColor!, widget.elevation);
|
: ElevationOverlay.applyOverlay(context, backgroundColor!, widget.elevation);
|
||||||
|
|
||||||
@@ -504,7 +494,7 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
curve: Curves.fastOutSlowIn,
|
curve: Curves.fastOutSlowIn,
|
||||||
duration: widget.animationDuration,
|
duration: widget.animationDuration,
|
||||||
clipBehavior: widget.clipBehavior,
|
clipBehavior: widget.clipBehavior,
|
||||||
elevation: modelElevation,
|
elevation: widget.elevation,
|
||||||
color: color,
|
color: color,
|
||||||
shadowColor: modelShadowColor,
|
shadowColor: modelShadowColor,
|
||||||
animateColor: false,
|
animateColor: false,
|
||||||
@@ -512,14 +502,22 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final ShapeBorder shape = _getShape();
|
shape ??= switch (widget.type) {
|
||||||
|
MaterialType.circle => const CircleBorder(),
|
||||||
|
MaterialType.canvas || MaterialType.transparency => const RoundedRectangleBorder(),
|
||||||
|
MaterialType.card || MaterialType.button => const RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.all(Radius.circular(2.0)),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
if (widget.type == MaterialType.transparency) {
|
if (widget.type == MaterialType.transparency) {
|
||||||
return _transparentInterior(
|
return ClipPath(
|
||||||
context: context,
|
clipper: ShapeBorderClipper(
|
||||||
shape: shape,
|
shape: shape,
|
||||||
|
textDirection: Directionality.maybeOf(context),
|
||||||
|
),
|
||||||
clipBehavior: widget.clipBehavior,
|
clipBehavior: widget.clipBehavior,
|
||||||
contents: contents,
|
child: _ShapeBorderPaint(shape: shape, child: contents),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -536,56 +534,6 @@ class _MaterialState extends State<Material> with TickerProviderStateMixin {
|
|||||||
child: contents,
|
child: contents,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Widget _transparentInterior({
|
|
||||||
required BuildContext context,
|
|
||||||
required ShapeBorder shape,
|
|
||||||
required Clip clipBehavior,
|
|
||||||
required Widget contents,
|
|
||||||
}) {
|
|
||||||
final _ShapeBorderPaint child = _ShapeBorderPaint(
|
|
||||||
shape: shape,
|
|
||||||
child: contents,
|
|
||||||
);
|
|
||||||
return ClipPath(
|
|
||||||
clipper: ShapeBorderClipper(
|
|
||||||
shape: shape,
|
|
||||||
textDirection: Directionality.maybeOf(context),
|
|
||||||
),
|
|
||||||
clipBehavior: clipBehavior,
|
|
||||||
child: child,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determines the shape for this Material.
|
|
||||||
//
|
|
||||||
// If a shape was specified, it will determine the shape.
|
|
||||||
// If a borderRadius was specified, the shape is a rounded
|
|
||||||
// rectangle.
|
|
||||||
// Otherwise, the shape is determined by the widget type as described in the
|
|
||||||
// Material class documentation.
|
|
||||||
ShapeBorder _getShape() {
|
|
||||||
if (widget.shape != null) {
|
|
||||||
return widget.shape!;
|
|
||||||
}
|
|
||||||
if (widget.borderRadius != null) {
|
|
||||||
return RoundedRectangleBorder(borderRadius: widget.borderRadius!);
|
|
||||||
}
|
|
||||||
switch (widget.type) {
|
|
||||||
case MaterialType.canvas:
|
|
||||||
case MaterialType.transparency:
|
|
||||||
return const RoundedRectangleBorder();
|
|
||||||
|
|
||||||
case MaterialType.card:
|
|
||||||
case MaterialType.button:
|
|
||||||
return RoundedRectangleBorder(
|
|
||||||
borderRadius: widget.borderRadius ?? kMaterialEdges[widget.type]!,
|
|
||||||
);
|
|
||||||
|
|
||||||
case MaterialType.circle:
|
|
||||||
return const CircleBorder();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class _RenderInkFeatures extends RenderProxyBox implements MaterialInkController {
|
class _RenderInkFeatures extends RenderProxyBox implements MaterialInkController {
|
||||||
@@ -899,7 +847,7 @@ class _MaterialInterior extends ImplicitlyAnimatedWidget {
|
|||||||
final Color color;
|
final Color color;
|
||||||
|
|
||||||
/// The target shadow color.
|
/// The target shadow color.
|
||||||
final Color? shadowColor;
|
final Color shadowColor;
|
||||||
|
|
||||||
/// The target surface tint color.
|
/// The target surface tint color.
|
||||||
final Color? surfaceTintColor;
|
final Color? surfaceTintColor;
|
||||||
@@ -930,13 +878,11 @@ class _MaterialInteriorState extends AnimatedWidgetBaseState<_MaterialInterior>
|
|||||||
widget.elevation,
|
widget.elevation,
|
||||||
(dynamic value) => Tween<double>(begin: value as double),
|
(dynamic value) => Tween<double>(begin: value as double),
|
||||||
) as Tween<double>?;
|
) as Tween<double>?;
|
||||||
_shadowColor = widget.shadowColor != null
|
_shadowColor = visitor(
|
||||||
? visitor(
|
_shadowColor,
|
||||||
_shadowColor,
|
widget.shadowColor,
|
||||||
widget.shadowColor,
|
(dynamic value) => ColorTween(begin: value as Color),
|
||||||
(dynamic value) => ColorTween(begin: value as Color),
|
) as ColorTween?;
|
||||||
) as ColorTween?
|
|
||||||
: null;
|
|
||||||
_surfaceTintColor = widget.surfaceTintColor != null
|
_surfaceTintColor = widget.surfaceTintColor != null
|
||||||
? visitor(
|
? visitor(
|
||||||
_surfaceTintColor,
|
_surfaceTintColor,
|
||||||
@@ -958,16 +904,15 @@ class _MaterialInteriorState extends AnimatedWidgetBaseState<_MaterialInterior>
|
|||||||
final Color color = Theme.of(context).useMaterial3
|
final Color color = Theme.of(context).useMaterial3
|
||||||
? ElevationOverlay.applySurfaceTint(widget.color, _surfaceTintColor?.evaluate(animation), elevation)
|
? ElevationOverlay.applySurfaceTint(widget.color, _surfaceTintColor?.evaluate(animation), elevation)
|
||||||
: ElevationOverlay.applyOverlay(context, widget.color, elevation);
|
: ElevationOverlay.applyOverlay(context, widget.color, elevation);
|
||||||
// If no shadow color is specified, use 0 for elevation in the model so a drop shadow won't be painted.
|
final Color shadowColor = _shadowColor!.evaluate(animation)!;
|
||||||
final double modelElevation = widget.shadowColor != null ? elevation : 0;
|
|
||||||
final Color shadowColor = _shadowColor?.evaluate(animation) ?? const Color(0x00000000);
|
|
||||||
return PhysicalShape(
|
return PhysicalShape(
|
||||||
clipper: ShapeBorderClipper(
|
clipper: ShapeBorderClipper(
|
||||||
shape: shape,
|
shape: shape,
|
||||||
textDirection: Directionality.maybeOf(context),
|
textDirection: Directionality.maybeOf(context),
|
||||||
),
|
),
|
||||||
clipBehavior: widget.clipBehavior,
|
clipBehavior: widget.clipBehavior,
|
||||||
elevation: modelElevation,
|
elevation: elevation,
|
||||||
color: color,
|
color: color,
|
||||||
shadowColor: shadowColor,
|
shadowColor: shadowColor,
|
||||||
child: _ShapeBorderPaint(
|
child: _ShapeBorderPaint(
|
||||||
|
|||||||
Reference in New Issue
Block a user