Glass: liquid-glass look for every glass surface

GlassPill (segmented bars, floating tabs, composer, viewer control bars,
top strips) now composes blur with a saturation boost in one backdrop
filter, paints a soft top highlight, a thin luminous gradient rim and an
outer-only depth shadow, tuned separately for light and dark fills
(GlassLook in glass_surface.dart). Fill opacity 0.84 -> 0.78; blur stays
at sigma 4 and one filter per piece. High contrast keeps the solid piece.
This commit is contained in:
2026-11-03 16:11:24 +00:00
parent 5f51bbf53b
commit 9283220c82
3 changed files with 312 additions and 24 deletions
+52 -24
View File
@@ -1,16 +1,19 @@
import 'dart:ui' show ImageFilter;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'glass_surface.dart';
/// A frosted-glass piece floating over content: the content behind is
/// blurred inside the piece's [shape], under a translucent fill in the host's
/// raised surface role (Material `surfaceContainerHigh`, Cupertino
/// `systemGrey5`) and a hairline border.
/// blurred and its colour lifted inside the piece's [shape], under a
/// translucent fill in the host's raised surface role (Material
/// `surfaceContainerHigh`, Cupertino `systemGrey5`), a soft highlight along
/// its top, a thin luminous rim over a hairline border, and a soft shadow
/// outside it ([GlassLook], tuned for light and dark fills).
///
/// Under the system's high-contrast setting the fill is solid and nothing is
/// blurred. The Cupertino piece uses no Material widget; the Material piece
/// is a transparent [Material] so ink responses inside it show.
/// Under the system's high-contrast setting the fill is solid, nothing is
/// blurred and only the hairline border is drawn. The Cupertino piece uses
/// no Material widget; the Material piece is a transparent [Material] so ink
/// responses inside it show.
///
/// Shared by the segmented top bar and any floating bar (a tab bar) that
/// wants the same pieces.
@@ -39,7 +42,7 @@ class GlassPill extends StatelessWidget {
static const double blurSigma = 4;
/// How much of the fill covers the blurred content.
static const double fillOpacity = 0.84;
static const double fillOpacity = 0.78;
@override
Widget build(BuildContext context) {
@@ -62,6 +65,7 @@ class GlassPill extends StatelessWidget {
Widget _build(BuildContext context, double reveal, Widget child) {
final solid = MediaQuery.maybeHighContrastOf(context) ?? false;
final textDirection = Directionality.maybeOf(context);
final Color base;
final Color edge;
if (cupertino) {
@@ -76,34 +80,58 @@ class GlassPill extends StatelessWidget {
}
final opaque = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
final color = opaque.withValues(alpha: opaque.a * reveal);
final look = GlassLook.forFill(opaque);
Color faded(Color c, [double by = 1]) =>
c.withValues(alpha: c.a * by * reveal);
final border = shape.copyWith(
side: BorderSide(
color: edge.withValues(alpha: edge.a * (solid ? 1 : 0.8) * reveal),
width: 0.5,
),
side: BorderSide(color: faded(edge, solid ? 1 : 0.6), width: 0.5),
);
Widget content = cupertino
? child
: Material(type: MaterialType.transparency, child: child);
if (!solid) {
// The highlight lies on the fill under the content; the rim is drawn
// over the content (and its ink) so the edge stays clean.
content = CustomPaint(
painter: GlassHighlightPainter(
shape: shape,
color: faded(look.highlight),
textDirection: textDirection,
),
foregroundPainter: GlassRimPainter(
shape: shape,
top: faded(look.rimTop),
bottom: faded(look.rimBottom),
textDirection: textDirection,
),
child: content,
);
}
Widget surface = DecoratedBox(
decoration: ShapeDecoration(color: color, shape: border),
child: cupertino
? child
: Material(type: MaterialType.transparency, child: child),
child: content,
);
if (!solid) {
// One blur per piece, clipped to it. Pieces under a common
// One filter per piece, clipped to it: the blur and the saturation
// boost are composed into one backdrop read. Pieces under a common
// [BackdropGroup] (a bar's row) share one read of the content behind
// them where the engine supports it (Impeller) instead of one each.
surface = BackdropFilter.grouped(
filter: ImageFilter.blur(
sigmaX: blurSigma * reveal,
sigmaY: blurSigma * reveal,
),
filter: GlassLook.backdrop(blurSigma * reveal),
child: surface,
);
}
return ClipPath(
clipper: ShapeBorderClipper(
surface = ClipPath(
clipper: ShapeBorderClipper(shape: shape, textDirection: textDirection),
child: surface,
);
if (solid) return surface;
// The shadow falls outside the clip, so it is painted beside it.
return CustomPaint(
painter: GlassShadowPainter(
shape: shape,
textDirection: Directionality.maybeOf(context),
color: faded(look.shadow),
textDirection: textDirection,
),
child: surface,
);
+202
View File
@@ -0,0 +1,202 @@
import 'dart:ui' show ImageFilter;
import 'package:flutter/widgets.dart';
/// The look of a frosted [GlassPill]-style surface, tuned once for light and
/// dark fills: how the content behind is filtered, the soft specular
/// highlight along its top edge, its luminous rim and its depth shadow.
///
/// Every value is chosen for a surface that keeps text and icons readable at
/// WCAG AA over whatever scrolls behind it: the fill still covers most of the
/// backdrop, and the highlight only lifts the fill's top a little.
@immutable
class GlassLook {
const GlassLook._({
required this.highlight,
required this.rimTop,
required this.rimBottom,
required this.shadow,
});
/// The look over a light fill (the light theme's raised surface).
static const GlassLook light = GlassLook._(
highlight: Color(0xA6FFFFFF),
rimTop: Color(0xE6FFFFFF),
rimBottom: Color(0x33FFFFFF),
shadow: Color(0x24000000),
);
/// The look over a dark fill (the dark theme's raised surface, a dark
/// viewer's pieces).
static const GlassLook dark = GlassLook._(
highlight: Color(0x1FFFFFFF),
rimTop: Color(0x47FFFFFF),
rimBottom: Color(0x0DFFFFFF),
shadow: Color(0x59000000),
);
/// The look for a surface filled with [fill]: dark below mid luminance.
static GlassLook forFill(Color fill) =>
fill.computeLuminance() < 0.3 ? dark : light;
/// The highlight's colour at the very top of the surface; it fades out by
/// [highlightExtent] of the surface's height.
final Color highlight;
/// The rim's colour along the top edge and along the bottom edge.
final Color rimTop;
final Color rimBottom;
/// The colour of the soft shadow under the surface.
final Color shadow;
/// How far down the surface the top highlight reaches, as a fraction of
/// its height.
static const double highlightExtent = 0.55;
/// The width of the luminous rim, inside the surface's edge.
static const double rimWidth = 1;
/// The shadow's blur and how far below the surface it falls.
static const double shadowSigma = 3;
static const Offset shadowOffset = Offset(0, 1.5);
/// How much the backdrop's colour is boosted after blurring, so a colour
/// behind the glass still reads through its fill.
static const double saturation = 1.6;
/// The backdrop filter: a blur of [sigma], then the saturation boost. Both
/// are one filter (one backdrop read) on every renderer.
static ImageFilter backdrop(double sigma) => ImageFilter.compose(
outer: const ColorFilter.matrix(_saturationMatrix),
inner: ImageFilter.blur(sigmaX: sigma, sigmaY: sigma),
);
// A standard saturation matrix (Rec. 709 luma weights) for [saturation].
static const double _r = 0.2126 * (1 - saturation);
static const double _g = 0.7152 * (1 - saturation);
static const double _b = 0.0722 * (1 - saturation);
static const List<double> _saturationMatrix = [
_r + saturation, _g, _b, 0, 0, //
_r, _g + saturation, _b, 0, 0, //
_r, _g, _b + saturation, 0, 0, //
0, 0, 0, 1, 0, //
];
}
/// The soft specular highlight along the top of a glass surface of [shape],
/// painted over its fill and under its content.
class GlassHighlightPainter extends CustomPainter {
const GlassHighlightPainter({
required this.shape,
required this.color,
required this.textDirection,
});
final ShapeBorder shape;
final Color color;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if (color.a == 0 || size.isEmpty) return;
final rect = Offset.zero & size;
final paint = Paint()
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [color, color.withValues(alpha: 0)],
stops: const [0, GlassLook.highlightExtent],
).createShader(rect);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
paint,
);
}
@override
bool shouldRepaint(GlassHighlightPainter oldDelegate) =>
color != oldDelegate.color ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}
/// The thin luminous rim of a glass surface of [shape]: a stroke just inside
/// its edge, bright along the top and faint along the bottom, painted over
/// the surface's content (ink included) so it stays a clean edge.
class GlassRimPainter extends CustomPainter {
const GlassRimPainter({
required this.shape,
required this.top,
required this.bottom,
required this.textDirection,
});
final ShapeBorder shape;
final Color top;
final Color bottom;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if ((top.a == 0 && bottom.a == 0) || size.isEmpty) return;
const inset = GlassLook.rimWidth / 2;
final rect = (Offset.zero & size).deflate(inset);
final paint = Paint()
..style = PaintingStyle.stroke
..strokeWidth = GlassLook.rimWidth
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [top, bottom, bottom],
stops: const [0, 0.5, 1],
).createShader(rect);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
paint,
);
}
@override
bool shouldRepaint(GlassRimPainter oldDelegate) =>
top != oldDelegate.top ||
bottom != oldDelegate.bottom ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}
/// The soft depth shadow of a glass surface of [shape], blurred only outside
/// its outline ([BlurStyle.outer]): under a translucent fill a shadow would
/// darken the glass itself.
class GlassShadowPainter extends CustomPainter {
const GlassShadowPainter({
required this.shape,
required this.color,
required this.textDirection,
});
final ShapeBorder shape;
final Color color;
final TextDirection? textDirection;
@override
void paint(Canvas canvas, Size size) {
if (color.a == 0 || size.isEmpty) return;
final rect = (Offset.zero & size).shift(GlassLook.shadowOffset);
canvas.drawPath(
shape.getOuterPath(rect, textDirection: textDirection),
Paint()
..color = color
..maskFilter = const MaskFilter.blur(
BlurStyle.outer,
GlassLook.shadowSigma,
),
);
}
@override
bool shouldRepaint(GlassShadowPainter oldDelegate) =>
color != oldDelegate.color ||
shape != oldDelegate.shape ||
textDirection != oldDelegate.textDirection;
}
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/adaptive/glass_pill.dart';
import 'package:pipelines_app/ui/adaptive/glass_surface.dart';
Widget _pill({Brightness brightness = Brightness.light, bool solid = false}) =>
MaterialApp(
theme: ThemeData(brightness: brightness),
home: MediaQuery(
data: MediaQueryData(highContrast: solid),
child: const Center(
child: GlassPill(cupertino: false, child: Text('Runs')),
),
),
);
List<CustomPainter> _painters(WidgetTester tester) => [
for (final paint in tester.widgetList<CustomPaint>(
find.ancestor(of: find.text('Runs'), matching: find.byType(CustomPaint)),
)) ...[?paint.painter, ?paint.foregroundPainter],
];
void main() {
testWidgets('a piece has its highlight, rim and shadow, and one backdrop '
'read that blurs and saturates', (tester) async {
await tester.pumpWidget(_pill());
final painters = _painters(tester);
expect(painters.whereType<GlassHighlightPainter>(), hasLength(1));
expect(painters.whereType<GlassRimPainter>(), hasLength(1));
expect(painters.whereType<GlassShadowPainter>(), hasLength(1));
final layers = tester.layers.whereType<BackdropFilterLayer>().toList();
expect(layers, hasLength(1));
expect(layers.single.filter, GlassLook.backdrop(GlassPill.blurSigma));
});
testWidgets('light and dark fills get their own look', (tester) async {
await tester.pumpWidget(_pill());
final light = _painters(tester).whereType<GlassRimPainter>().single;
expect(light.top, GlassLook.light.rimTop);
await tester.pumpWidget(_pill(brightness: Brightness.dark));
await tester.pumpAndSettle();
final dark = _painters(tester).whereType<GlassRimPainter>().single;
expect(dark.top, GlassLook.dark.rimTop);
expect(GlassLook.forFill(Colors.white), same(GlassLook.light));
expect(GlassLook.forFill(Colors.black), same(GlassLook.dark));
});
testWidgets('high contrast keeps a solid piece without blur or sheen', (
tester,
) async {
await tester.pumpWidget(_pill(solid: true));
expect(_painters(tester).whereType<GlassHighlightPainter>(), isEmpty);
expect(_painters(tester).whereType<GlassRimPainter>(), isEmpty);
expect(_painters(tester).whereType<GlassShadowPainter>(), isEmpty);
expect(tester.layers.whereType<BackdropFilterLayer>(), isEmpty);
});
}