Unverified Commit 27cc8c06 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Move ExpansionPanelList to Canvas.drawShadow (#80134)

parent 362eefee
...@@ -7,9 +7,9 @@ import 'dart:ui' show lerpDouble; ...@@ -7,9 +7,9 @@ import 'dart:ui' show lerpDouble;
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'colors.dart';
import 'divider.dart'; import 'divider.dart';
import 'material.dart'; import 'material.dart';
import 'shadows.dart';
import 'theme.dart'; import 'theme.dart';
/// The base type for [MaterialSlice] and [MaterialGap]. /// The base type for [MaterialSlice] and [MaterialGap].
...@@ -122,12 +122,8 @@ class MergeableMaterial extends StatefulWidget { ...@@ -122,12 +122,8 @@ class MergeableMaterial extends StatefulWidget {
/// The z-coordinate at which to place all the [Material] slices. /// The z-coordinate at which to place all the [Material] slices.
/// ///
/// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24.
///
/// Defaults to 2, the appropriate elevation for cards. /// Defaults to 2, the appropriate elevation for cards.
/// // TODO(ianh): Change this to double.
/// This uses [kElevationToShadow] to simulate shadows, it does not use
/// [Material]'s arbitrary elevation feature.
final int elevation; final int elevation;
/// Whether connected pieces of [MaterialSlice] have dividers between them. /// Whether connected pieces of [MaterialSlice] have dividers between them.
...@@ -620,7 +616,7 @@ class _MergeableMaterialState extends State<MergeableMaterial> with TickerProvid ...@@ -620,7 +616,7 @@ class _MergeableMaterialState extends State<MergeableMaterial> with TickerProvid
return _MergeableMaterialListBody( return _MergeableMaterialListBody(
mainAxis: widget.mainAxis, mainAxis: widget.mainAxis,
boxShadows: kElevationToShadow[widget.elevation]!, elevation: widget.elevation.toDouble(),
items: _children, items: _children,
children: widgets, children: widgets,
); );
...@@ -654,11 +650,11 @@ class _MergeableMaterialListBody extends ListBody { ...@@ -654,11 +650,11 @@ class _MergeableMaterialListBody extends ListBody {
required List<Widget> children, required List<Widget> children,
Axis mainAxis = Axis.vertical, Axis mainAxis = Axis.vertical,
required this.items, required this.items,
required this.boxShadows, required this.elevation,
}) : super(children: children, mainAxis: mainAxis); }) : super(children: children, mainAxis: mainAxis);
final List<MergeableMaterialItem> items; final List<MergeableMaterialItem> items;
final List<BoxShadow> boxShadows; final double elevation;
AxisDirection _getDirection(BuildContext context) { AxisDirection _getDirection(BuildContext context) {
return getAxisDirectionFromAxisReverseAndDirectionality(context, mainAxis, false); return getAxisDirectionFromAxisReverseAndDirectionality(context, mainAxis, false);
...@@ -668,7 +664,7 @@ class _MergeableMaterialListBody extends ListBody { ...@@ -668,7 +664,7 @@ class _MergeableMaterialListBody extends ListBody {
RenderListBody createRenderObject(BuildContext context) { RenderListBody createRenderObject(BuildContext context) {
return _RenderMergeableMaterialListBody( return _RenderMergeableMaterialListBody(
axisDirection: _getDirection(context), axisDirection: _getDirection(context),
boxShadows: boxShadows, elevation: elevation,
); );
} }
...@@ -677,7 +673,7 @@ class _MergeableMaterialListBody extends ListBody { ...@@ -677,7 +673,7 @@ class _MergeableMaterialListBody extends ListBody {
final _RenderMergeableMaterialListBody materialRenderListBody = renderObject as _RenderMergeableMaterialListBody; final _RenderMergeableMaterialListBody materialRenderListBody = renderObject as _RenderMergeableMaterialListBody;
materialRenderListBody materialRenderListBody
..axisDirection = _getDirection(context) ..axisDirection = _getDirection(context)
..boxShadows = boxShadows; ..elevation = elevation;
} }
} }
...@@ -685,38 +681,41 @@ class _RenderMergeableMaterialListBody extends RenderListBody { ...@@ -685,38 +681,41 @@ class _RenderMergeableMaterialListBody extends RenderListBody {
_RenderMergeableMaterialListBody({ _RenderMergeableMaterialListBody({
List<RenderBox>? children, List<RenderBox>? children,
AxisDirection axisDirection = AxisDirection.down, AxisDirection axisDirection = AxisDirection.down,
required this.boxShadows, double elevation = 0.0,
}) : super(children: children, axisDirection: axisDirection); }) : _elevation = elevation,
super(children: children, axisDirection: axisDirection);
List<BoxShadow> boxShadows;
double get elevation => _elevation;
double _elevation;
set elevation(double value) {
if (value == _elevation)
return;
_elevation = value;
markNeedsPaint();
}
void _paintShadows(Canvas canvas, Rect rect) { void _paintShadows(Canvas canvas, Rect rect) {
for (final BoxShadow boxShadow in boxShadows) { // TODO(ianh): We should interpolate the border radii of the shadows the same way we do those of the visible Material slices.
final Paint paint = boxShadow.toPaint(); canvas.drawShadow(
// TODO(dragostis): Right now, we are only interpolating the border radii Path()..addRRect(kMaterialEdges[MaterialType.card]!.toRRect(rect)),
// of the visible Material slices, not the shadows; they are not getting Colors.black,
// interpolated and always have the same rounded radii. Once shadow elevation,
// performance is better, shadows should be redrawn every single time the true, // occluding object is not (necessarily) opaque
// slices' radii get interpolated and use those radii not the defaults. );
canvas.drawRRect(kMaterialEdges[MaterialType.card]!.toRRect(rect), paint);
}
} }
@override @override
void paint(PaintingContext context, Offset offset) { void paint(PaintingContext context, Offset offset) {
RenderBox? child = firstChild; RenderBox? child = firstChild;
int i = 0; int index = 0;
while (child != null) { while (child != null) {
final ListBodyParentData childParentData = child.parentData! as ListBodyParentData; final ListBodyParentData childParentData = child.parentData! as ListBodyParentData;
final Rect rect = (childParentData.offset + offset) & child.size; final Rect rect = (childParentData.offset + offset) & child.size;
if (i.isEven) if (index.isEven)
_paintShadows(context.canvas, rect); _paintShadows(context.canvas, rect);
child = childParentData.nextSibling; child = childParentData.nextSibling;
index += 1;
i += 1;
} }
defaultPaint(context, offset); defaultPaint(context, offset);
} }
} }
...@@ -221,13 +221,14 @@ void main() { ...@@ -221,13 +221,14 @@ void main() {
), ),
); );
final BoxShadow boxShadow = kElevationToShadow[2]![0];
final RRect rrect = kMaterialEdges[MaterialType.card]!.toRRect( final RRect rrect = kMaterialEdges[MaterialType.card]!.toRRect(
const Rect.fromLTRB(0.0, 0.0, 800.0, 100.0) const Rect.fromLTRB(0.0, 0.0, 800.0, 100.0)
); );
expect( expect(
find.byType(MergeableMaterial), find.byType(MergeableMaterial),
paints..rrect(rrect: rrect, color: boxShadow.color, hasMaskFilter: true), paints
..shadow(elevation: 2.0)
..rrect(rrect: rrect, color: Colors.white, hasMaskFilter: false),
); );
debugDisableShadows = true; debugDisableShadows = true;
}); });
......
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