Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:fossui/src/theme/theme.dart';
3 :
4 : /// The axis a [FossSeparator] runs along.
5 : enum FossSeparatorOrientation {
6 : /// Fills the available width at 1 logical pixel tall (the default).
7 : horizontal,
8 :
9 : /// Fills the parent's height at 1 logical pixel wide.
10 : vertical,
11 : }
12 :
13 : /// {@category Layout}
14 : /// {@template foss.separator.preview}
15 : /// <img src="https://fossui.org/components/separator/overview/light.png"
16 : /// alt="FossSeparator, light theme" width="480"
17 : /// style="max-width:100%;height:auto" />
18 : /// <img src="https://fossui.org/components/separator/overview/dark.png"
19 : /// alt="FossSeparator, dark theme" width="480"
20 : /// style="max-width:100%;height:auto" />
21 : ///
22 : /// See the [separator documentation ↗](https://fossui.org/docs/components/separator) or
23 : /// try it live in the
24 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/separator/fossseparator/playground).
25 : /// {@endtemplate}
26 : ///
27 : /// A hairline rule that divides content along a row or a column. Static and
28 : /// non-interactive: a 1 logical pixel line in the `border` role.
29 : ///
30 : /// [orientation] picks the axis. The rule fills its long axis, so that axis
31 : /// must be bounded by the parent. [FossSeparatorOrientation.horizontal] (the
32 : /// default) fills the width, so it needs a bounded-width parent (a [Column]
33 : /// with stretch, or an [Expanded] inside a [Row]); vertical fills the height
34 : /// and needs a bounded-height parent (a [Row], for example, supplies it). An
35 : /// unbounded long axis surfaces a layout error rather than misrendering. The
36 : /// color comes from `context.fossTheme`; retheme globally through
37 : /// `FossColors.border` rather than per instance.
38 : ///
39 : /// Decorative by default: [decorative] true keeps the line out of the semantics
40 : /// tree, the common case for a purely visual rule. Set it false to mark a real
41 : /// content boundary; the line then emits a structural boundary node that groups
42 : /// and separates the surrounding content. The node carries no spoken label by
43 : /// design, since a divider names nothing.
44 : ///
45 : /// ```dart
46 : /// Column(
47 : /// crossAxisAlignment: CrossAxisAlignment.stretch,
48 : /// children: [
49 : /// Text('Above'),
50 : /// FossSeparator(),
51 : /// Text('Below'),
52 : /// ],
53 : /// );
54 : /// ```
55 : class FossSeparator extends StatelessWidget {
56 : /// {@macro foss.separator.preview}
57 : ///
58 : /// Creates a hairline separator.
59 1 : const FossSeparator({
60 : this.orientation = FossSeparatorOrientation.horizontal,
61 : this.decorative = true,
62 : super.key,
63 : });
64 :
65 : /// The axis the rule runs along; [FossSeparatorOrientation.horizontal] by
66 : /// default.
67 : final FossSeparatorOrientation orientation;
68 :
69 : /// Whether the rule is purely visual. When true (the default) the line is
70 : /// hidden from the semantics tree; set it false to expose a content boundary.
71 : final bool decorative;
72 :
73 1 : @override
74 : Widget build(BuildContext context) {
75 2 : final isHorizontal = orientation == FossSeparatorOrientation.horizontal;
76 :
77 : // double.infinity fills the long axis (clamped to the parent's bound); the
78 : // 1px short axis is the only baked metric. That long axis must be bounded
79 : // by the parent, else the infinite extent has nothing to clamp against.
80 1 : final line = SizedBox(
81 : width: isHorizontal ? double.infinity : 1,
82 : height: isHorizontal ? 1 : double.infinity,
83 4 : child: ColoredBox(color: context.fossTheme.colors.border),
84 : );
85 :
86 1 : return decorative
87 1 : ? ExcludeSemantics(child: line)
88 1 : : Semantics(container: true, child: line);
89 : }
90 : }
|