Revert: Cache `BoxShadow` paints in `_BoxDecorationPainter` (#191346)
Reverts: [Cache `BoxShadow` paints in
`_BoxDecorationPainter`](https://github.com/flutter/flutter/pull/190194)
Initiated by: @cbracken
Reason for reverting: ```
Original PR Author: @fa0311
Reviewed By: @victorsanni
The original PR description is provided below:
`_BoxDecorationPainter._paintShadows` called `BoxShadow.toPaint()` —
allocating a new `Paint` and native `MaskFilter` — for every shadow on
every paint, even though `BoxDecoration` is immutable and the painter is
retained across frames by `RenderDecoratedBox`. This PR caches the
paints on first use, the same way `_ShapeDecorationPainter` already
caches them in `_shadowPaints` (`shape_decoration.dart`).
The one observable difference matches existing `ShapeDecoration`
behavior: toggling `debugDisableShadows` between paints of a live
painter is not picked up until the painter is recreated, which is
debug-only and already the case for `ShapeDecoration`.
Part of #190187.
This is a behavior-preserving refactor; existing tests in
`packages/flutter/test/painting/` and
`packages/flutter/test/widgets/box_decoration_test.dart` cover this code
path and pass, so I believe this qualifies as test-exempt.
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
diff --git a/packages/flutter/lib/src/painting/box_decoration.dart b/packages/flutter/lib/src/painting/box_decoration.dart
index f113b43..a9ca00e 100644
--- a/packages/flutter/lib/src/painting/box_decoration.dart
+++ b/packages/flutter/lib/src/painting/box_decoration.dart
@@ -446,19 +446,12 @@
}
}
- List<Paint>? _shadowPaints;
-
void _paintShadows(Canvas canvas, Rect rect, TextDirection? textDirection) {
- final List<BoxShadow>? boxShadows = _decoration.boxShadow;
- if (boxShadows == null) {
+ if (_decoration.boxShadow == null) {
return;
}
- final List<Paint> shadowPaints = _shadowPaints ??= <Paint>[
- for (final BoxShadow boxShadow in boxShadows) boxShadow.toPaint(),
- ];
- for (var i = 0; i < boxShadows.length; i += 1) {
- final BoxShadow boxShadow = boxShadows[i];
- final Paint paint = shadowPaints[i];
+ for (final BoxShadow boxShadow in _decoration.boxShadow!) {
+ final Paint paint = boxShadow.toPaint();
final Rect bounds = rect.shift(boxShadow.offset).inflate(boxShadow.spreadRadius);
assert(() {
if (debugDisableShadows && boxShadow.blurStyle == BlurStyle.outer) {