8ca3d3e467
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
4.0 KiB
Dart
117 lines
4.0 KiB
Dart
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'app_design_tokens.dart';
|
|
|
|
/// Paints a glass design's still gradient ([GlassStyle.backdrop]) behind
|
|
/// [child], the app's pages, whose scaffolds are clear under that design.
|
|
///
|
|
/// The gradient is its own layer behind a [RepaintBoundary]: it is recorded
|
|
/// when the window resizes or the design changes, never while content
|
|
/// scrolls or animates over it. Under any other design nothing is painted,
|
|
/// and the tree keeps the same shape, so switching designs never remounts
|
|
/// the app below.
|
|
class GlassBackdrop extends StatelessWidget {
|
|
const GlassBackdrop({super.key, required this.child});
|
|
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final mesh = AppDesignTokens.of(context).glass?.backdrop;
|
|
return Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
RepaintBoundary(
|
|
child: CustomPaint(
|
|
key: const ValueKey('glass-backdrop'),
|
|
painter: mesh == null ? null : GlassMeshPainter(mesh),
|
|
),
|
|
),
|
|
child,
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Paints a [GlassBackdropMesh]: its base, then each glow as a radial wash
|
|
/// that fades to nothing at its radius (a fraction of the longer side).
|
|
///
|
|
/// The mesh is smooth, so it is drawn once into a small image
|
|
/// ([meshImage], cached per mesh and window shape) and stretched over the
|
|
/// window: one filtered image draw per frame instead of a full-window
|
|
/// gradient per glow, which a CPU rasteriser redraws on every frame.
|
|
class GlassMeshPainter extends CustomPainter {
|
|
const GlassMeshPainter(this.mesh);
|
|
|
|
final GlassBackdropMesh mesh;
|
|
|
|
/// The cached image's longer side, in pixels.
|
|
static const int imageExtent = 96;
|
|
|
|
static final Map<(GlassBackdropMesh, int, int), ui.Image> _images = {};
|
|
|
|
/// [mesh] drawn at [imageExtent] along the longer side of a window shaped
|
|
/// like [size]; the same image for the same mesh and shape.
|
|
static ui.Image meshImage(GlassBackdropMesh mesh, Size size) {
|
|
final landscape = size.width >= size.height;
|
|
final ratio = landscape ? size.height / size.width : size.width / size.height;
|
|
final short = (imageExtent * ratio).round().clamp(8, imageExtent);
|
|
final width = landscape ? imageExtent : short;
|
|
final height = landscape ? short : imageExtent;
|
|
final key = (mesh, width, height);
|
|
final cached = _images[key];
|
|
if (cached != null) return cached;
|
|
// Only a few shapes are ever live (a window, a phone in two
|
|
// orientations); drop the oldest rather than grow.
|
|
if (_images.length >= 8) _images.remove(_images.keys.first);
|
|
final recorder = ui.PictureRecorder();
|
|
paintMesh(
|
|
Canvas(recorder),
|
|
Size(width.toDouble(), height.toDouble()),
|
|
mesh,
|
|
);
|
|
final picture = recorder.endRecording();
|
|
final image = picture.toImageSync(width, height);
|
|
picture.dispose();
|
|
return _images[key] = image;
|
|
}
|
|
|
|
/// Paints [mesh] over [size] directly: the base and each glow.
|
|
static void paintMesh(Canvas canvas, Size size, GlassBackdropMesh mesh) {
|
|
final rect = Offset.zero & size;
|
|
canvas.drawRect(rect, Paint()..color = mesh.base);
|
|
final reach = size.longestSide;
|
|
for (final glow in mesh.glows) {
|
|
final center = glow.center.withinRect(rect);
|
|
final radius = glow.radius * reach;
|
|
final paint = Paint()
|
|
..shader = RadialGradient(
|
|
colors: [
|
|
glow.color,
|
|
glow.color.withValues(alpha: glow.color.a * 0.55),
|
|
glow.color.withValues(alpha: 0),
|
|
],
|
|
stops: const [0, 0.45, 1],
|
|
).createShader(Rect.fromCircle(center: center, radius: radius));
|
|
canvas.drawRect(rect, paint);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (size.isEmpty) return;
|
|
final image = meshImage(mesh, size);
|
|
canvas.drawImageRect(
|
|
image,
|
|
Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
|
|
Offset.zero & size,
|
|
Paint()..filterQuality = FilterQuality.low,
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(GlassMeshPainter oldDelegate) => oldDelegate.mesh != mesh;
|
|
}
|