Files
pipelines/app/lib/ui/theme/glass_border.dart
T
2026-12-03 23:21:05 +00:00

138 lines
3.9 KiB
Dart

import 'package:flutter/painting.dart';
/// The edge of a glass surface: a rounded rectangle whose 1px line is a
/// light [highlight] along its top that falls off into a faint [edge] by
/// mid-height, like light catching the top of a pane of glass.
///
/// A [side] set on it (a focus ring, a widget's own outline through
/// `copyWith`) is drawn instead, as a [RoundedRectangleBorder] draws it.
/// Value-equal, so a theme holding it stays equal when rebuilt.
class GlassBorder extends OutlinedBorder {
const GlassBorder({
this.borderRadius = BorderRadius.zero,
required this.highlight,
required this.edge,
super.side,
});
final BorderRadius borderRadius;
/// The line's colour at the very top.
final Color highlight;
/// The line's colour from mid-height down.
final Color edge;
@override
EdgeInsetsGeometry get dimensions =>
EdgeInsets.all(side.style == BorderStyle.none ? 0 : side.strokeInset);
RRect _rrect(Rect rect) => borderRadius.toRRect(rect);
@override
Path getInnerPath(Rect rect, {TextDirection? textDirection}) => Path()
..addRRect(_rrect(rect).deflate(side.strokeInset).scaleRadii());
@override
Path getOuterPath(Rect rect, {TextDirection? textDirection}) =>
Path()..addRRect(_rrect(rect));
@override
void paintInterior(
Canvas canvas,
Rect rect,
Paint paint, {
TextDirection? textDirection,
}) {
canvas.drawRRect(_rrect(rect), paint);
}
@override
bool get preferPaintInterior => true;
@override
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
if (rect.isEmpty) return;
if (side.style != BorderStyle.none && side.width > 0) {
canvas.drawRRect(
_rrect(rect).inflate(side.strokeOffset / 2),
side.toPaint(),
);
return;
}
if (highlight.a == 0 && edge.a == 0) return;
final line = _rrect(rect).deflate(0.5);
canvas.drawRRect(
line,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..shader = LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [highlight, edge, edge],
// The highlight falls off within the top corners' reach (at most
// the top 24px), not over a tall card's whole height.
stops: [0, (28 / rect.height).clamp(0.05, 0.5), 1],
).createShader(rect),
);
}
@override
GlassBorder copyWith({
BorderSide? side,
BorderRadius? borderRadius,
Color? highlight,
Color? edge,
}) => GlassBorder(
borderRadius: borderRadius ?? this.borderRadius,
highlight: highlight ?? this.highlight,
edge: edge ?? this.edge,
side: side ?? this.side,
);
@override
ShapeBorder scale(double t) => GlassBorder(
borderRadius: borderRadius * t,
highlight: highlight,
edge: edge,
side: side.scale(t),
);
@override
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
if (a is GlassBorder) return _lerp(a, this, t);
return super.lerpFrom(a, t);
}
@override
ShapeBorder? lerpTo(ShapeBorder? b, double t) {
if (b is GlassBorder) return _lerp(this, b, t);
return super.lerpTo(b, t);
}
static GlassBorder _lerp(GlassBorder a, GlassBorder b, double t) =>
GlassBorder(
borderRadius: BorderRadius.lerp(a.borderRadius, b.borderRadius, t)!,
highlight: Color.lerp(a.highlight, b.highlight, t)!,
edge: Color.lerp(a.edge, b.edge, t)!,
side: BorderSide.lerp(a.side, b.side, t),
);
@override
bool operator ==(Object other) =>
other is GlassBorder &&
other.borderRadius == borderRadius &&
other.highlight == highlight &&
other.edge == edge &&
other.side == side;
@override
int get hashCode => Object.hash(borderRadius, highlight, edge, side);
@override
String toString() =>
'GlassBorder($borderRadius, highlight: $highlight, edge: $edge, '
'side: $side)';
}