b90c7e2d1d
Blur sigma 16 -> 4 keeps CanvasKit on its direct blur path; under the 84% fill the look is unchanged (max 13/255 inside the pieces). SegmentedBar's pieces share a BackdropGroup so Impeller reads the content behind them once. Adds a scroll frame-time bench, a backdrop census and filler rows to the full-app probe. CPU CanvasKit, 560x900: run timeline p50 31.8 -> 22.1 ms, runs list 25.3 -> 19.9 ms.
90 lines
3.1 KiB
Dart
90 lines
3.1 KiB
Dart
import 'dart:ui' show ImageFilter;
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.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.
|
|
///
|
|
/// 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.
|
|
///
|
|
/// Shared by the segmented top bar and any floating bar (a tab bar) that
|
|
/// wants the same pieces.
|
|
class GlassPill extends StatelessWidget {
|
|
const GlassPill({
|
|
super.key,
|
|
required this.cupertino,
|
|
required this.child,
|
|
this.shape = const StadiumBorder(),
|
|
this.fill,
|
|
});
|
|
|
|
final bool cupertino;
|
|
final Widget child;
|
|
final OutlinedBorder shape;
|
|
|
|
/// Overrides the fill (a dark viewer's pieces); used as given.
|
|
final Color? fill;
|
|
|
|
/// Blur radius of the content behind a piece.
|
|
///
|
|
/// Under the [fillOpacity] fill, 4 looks the same as a larger radius (the
|
|
/// content behind is already an indistinct tint) and stays on the
|
|
/// renderer's direct blur path: above about 4, Skia (CanvasKit) downscales,
|
|
/// blurs and upscales every piece on every frame content scrolls behind it.
|
|
static const double blurSigma = 4;
|
|
|
|
/// How much of the fill covers the blurred content.
|
|
static const double fillOpacity = 0.84;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final solid = MediaQuery.maybeHighContrastOf(context) ?? false;
|
|
final Color base;
|
|
final Color edge;
|
|
if (cupertino) {
|
|
// Grey 5 stays visibly raised over both the black dark canvas and the
|
|
// white light canvas; Grey 6 disappears into either one when blurred.
|
|
base = CupertinoColors.systemGrey5.resolveFrom(context);
|
|
edge = CupertinoColors.separator.resolveFrom(context);
|
|
} else {
|
|
final scheme = Theme.of(context).colorScheme;
|
|
base = scheme.surfaceContainerHigh;
|
|
edge = scheme.outlineVariant;
|
|
}
|
|
final color = fill ?? (solid ? base : base.withValues(alpha: fillOpacity));
|
|
final border = shape.copyWith(
|
|
side: BorderSide(
|
|
color: solid ? edge : edge.withValues(alpha: edge.a * 0.8),
|
|
width: 0.5,
|
|
),
|
|
);
|
|
Widget surface = DecoratedBox(
|
|
decoration: ShapeDecoration(color: color, shape: border),
|
|
child: cupertino
|
|
? child
|
|
: Material(type: MaterialType.transparency, child: child),
|
|
);
|
|
if (!solid) {
|
|
// One blur per piece, clipped to it. 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, sigmaY: blurSigma),
|
|
child: surface,
|
|
);
|
|
}
|
|
return ClipPath(
|
|
clipper: ShapeBorderClipper(
|
|
shape: shape,
|
|
textDirection: Directionality.maybeOf(context),
|
|
),
|
|
child: surface,
|
|
);
|
|
}
|
|
}
|