Line data Source code
1 : import 'package:flutter/widgets.dart';
2 :
3 : part 'glyphs/calendar.dart';
4 : part 'glyphs/check.dart';
5 : part 'glyphs/chevron_left.dart';
6 : part 'glyphs/chevron_right.dart';
7 : part 'glyphs/chevron_up_down.dart';
8 : part 'glyphs/clock.dart';
9 : part 'glyphs/close.dart';
10 : part 'glyphs/ellipsis.dart';
11 : part 'glyphs/error.dart';
12 : part 'glyphs/info.dart';
13 : part 'glyphs/minus.dart';
14 : part 'glyphs/star.dart';
15 : part 'glyphs/success.dart';
16 : part 'glyphs/warning.dart';
17 :
18 : /// Base for the built-in glyphs the components paint when no icon is supplied.
19 : ///
20 : /// The package takes no icon dependency, so the default marks (a close cross, a
21 : /// status symbol, a selection check) are stroked in code. Each glyph is a small
22 : /// subclass describing only its shape and weight; the scaffolding here maps a
23 : /// unit box to the paint size once.
24 : abstract class FossGlyph extends CustomPainter {
25 : /// Creates a glyph painted in [color].
26 26 : const FossGlyph(this.color);
27 :
28 : /// The stroke and fill color.
29 : final Color color;
30 :
31 : // The stroke weight as a fraction of the shortest side.
32 : double get _stroke;
33 :
34 : // Strokes the glyph in a unit box, top-left (0, 0) to bottom-right (1, 1).
35 : void _draw(_Pen pen);
36 :
37 26 : @override
38 : void paint(Canvas canvas, Size size) {
39 26 : final s = size.shortestSide;
40 130 : _draw(_Pen(canvas, s, color, s * _stroke));
41 : }
42 :
43 17 : @override
44 51 : bool shouldRepaint(covariant FossGlyph old) => old.color != color;
45 : }
46 :
47 : /// Unit-box drawing helper shared by the glyphs. Holds the scale, the stroke
48 : /// paint, and a fill, and exposes the primitives each glyph is built from, so
49 : /// the paint setup is written once rather than copied per glyph.
50 : class _Pen {
51 26 : _Pen(this.canvas, this.s, Color color, double strokeWidth)
52 26 : : _stroke = Paint()
53 26 : ..color = color
54 26 : ..style = PaintingStyle.stroke
55 26 : ..strokeWidth = strokeWidth
56 26 : ..strokeCap = StrokeCap.round
57 26 : ..strokeJoin = StrokeJoin.round,
58 52 : _fill = Paint()..color = color;
59 :
60 : final Canvas canvas;
61 : final double s;
62 : final Paint _stroke;
63 : final Paint _fill;
64 :
65 156 : Offset _p(double x, double y) => Offset(x * s, y * s);
66 :
67 : /// A stroked segment between two unit-box points.
68 14 : void line(double x1, double y1, double x2, double y2) =>
69 70 : canvas.drawLine(_p(x1, y1), _p(x2, y2), _stroke);
70 :
71 : /// A stroked polyline through [points], optionally closed into a loop.
72 20 : void path(List<(double, double)> points, {bool close = false}) {
73 20 : final (fx, fy) = points.first;
74 120 : final path = Path()..moveTo(_p(fx, fy).dx, _p(fx, fy).dy);
75 40 : for (final (x, y) in points.skip(1)) {
76 100 : path.lineTo(_p(x, y).dx, _p(x, y).dy);
77 : }
78 5 : if (close) path.close();
79 60 : canvas.drawPath(path, _stroke);
80 : }
81 :
82 : /// A filled polygon through [points], closed.
83 2 : void shape(List<(double, double)> points) {
84 2 : final (fx, fy) = points.first;
85 12 : final path = Path()..moveTo(_p(fx, fy).dx, _p(fx, fy).dy);
86 4 : for (final (x, y) in points.skip(1)) {
87 10 : path.lineTo(_p(x, y).dx, _p(x, y).dy);
88 : }
89 8 : canvas.drawPath(path..close(), _fill);
90 : }
91 :
92 : /// A small filled dot at a unit-box point.
93 49 : void dot(double x, double y) => canvas.drawCircle(_p(x, y), s * 0.055, _fill);
94 :
95 : /// A stroked circle centered in the box.
96 42 : void ring() => canvas.drawCircle(_p(0.5, 0.5), s * 0.42, _stroke);
97 : }
98 :
99 : /// Paints a [FossGlyph] at [size] in the glyph's own color. A lightweight
100 : /// stand-in for an icon font, used only for the components' default marks.
101 : ///
102 : /// Pass a [semanticLabel] when the glyph carries meaning (a status); leave it
103 : /// null for decorative marks, which are then excluded from the semantics tree.
104 : /// With no [size] the glyph fills the parent's constraints.
105 : class FossGlyphIcon extends StatelessWidget {
106 : /// Creates a glyph icon for [glyph].
107 23 : const FossGlyphIcon(this.glyph, {this.size, this.semanticLabel, super.key});
108 :
109 : /// The glyph to paint. Carries its own color.
110 : final FossGlyph glyph;
111 :
112 : /// The square extent in logical pixels, or null to fill the parent.
113 : final double? size;
114 :
115 : /// An optional label for assistive technology.
116 : final String? semanticLabel;
117 :
118 23 : @override
119 : Widget build(BuildContext context) {
120 23 : final extent = size;
121 23 : final child = CustomPaint(
122 21 : size: extent == null ? Size.zero : Size.square(extent),
123 23 : painter: glyph,
124 : );
125 23 : if (semanticLabel case final label?) {
126 5 : return Semantics(label: label, image: true, child: child);
127 : }
128 20 : return ExcludeSemantics(child: child);
129 : }
130 : }
|