Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:fossui/src/components/drawer/foss_drawer.dart';
3 : import 'package:fossui/src/foundation/foss_dialog_surface.dart';
4 : import 'package:fossui/src/foundation/foss_modal_route.dart';
5 : import 'package:fossui/src/icons/foss_glyph.dart';
6 : import 'package:fossui/src/theme/foss_theme.dart';
7 :
8 : part 'foss_alert_dialog.dart';
9 : part 'foss_alert_dialog_style.dart';
10 : part 'foss_dialog_show.dart';
11 : part 'foss_dialog_style.dart';
12 :
13 : /// Default maximum width of the centered card in logical pixels.
14 : const double _maxWidth = 512;
15 :
16 : /// Carries the [FossDialogPresentation] from the show function down to the
17 : /// surface, so the caller sets it once on the route and the widget reads it
18 : /// back, the same handoff the drawer makes for its side.
19 : class _DialogPresentationScope extends InheritedWidget {
20 5 : const _DialogPresentationScope({
21 : required this.presentation,
22 : required super.child,
23 : });
24 :
25 : final FossDialogPresentation presentation;
26 :
27 3 : static FossDialogPresentation of(BuildContext context) =>
28 : context
29 3 : .dependOnInheritedWidgetOfExactType<_DialogPresentationScope>()
30 3 : ?.presentation ??
31 : FossDialogPresentation.centered;
32 :
33 2 : @override
34 : bool updateShouldNotify(_DialogPresentationScope oldWidget) =>
35 6 : oldWidget.presentation != presentation;
36 : }
37 :
38 : /// {@category Overlays}
39 : /// {@template foss.dialog.preview}
40 : /// <img src="https://fossui.org/components/dialog/overview/light.png"
41 : /// alt="FossDialog, light theme" width="480"
42 : /// style="max-width:100%;height:auto" />
43 : /// <img src="https://fossui.org/components/dialog/overview/dark.png"
44 : /// alt="FossDialog, dark theme" width="480"
45 : /// style="max-width:100%;height:auto" />
46 : ///
47 : /// See the [dialog documentation ↗](https://fossui.org/docs/components/dialog) or
48 : /// try it live in the
49 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/dialog/fossdialog/playground).
50 : /// {@endtemplate}
51 : ///
52 : /// A modal surface with slots for a title, description, body, and actions,
53 : /// plus a default close affordance. Presents as a bottom sheet by default, or a
54 : /// centered card via [presentation].
55 : ///
56 : /// Show it with [showFossDialog]. The header, body, and footer are each
57 : /// optional; [actions] reuse `FossButton` and sit in a trailing-aligned row.
58 : /// Colors, type, radius, and shadow come from `context.fossTheme`.
59 : ///
60 : /// {@macro foss.customize}
61 : ///
62 : /// See also [FossDrawer] for an edge-anchored panel and [FossAlertDialog] for a
63 : /// decision that cannot be dismissed.
64 : ///
65 : /// ```dart
66 : /// showFossDialog<void>(
67 : /// context: context,
68 : /// builder: (context) => const FossDialog(
69 : /// title: Text('Saved'),
70 : /// description: Text('Your changes are live.'),
71 : /// ),
72 : /// );
73 : /// ```
74 : class FossDialog extends StatelessWidget {
75 : /// {@macro foss.dialog.preview}
76 : ///
77 : /// Creates a dialog surface. Build it inside a [showFossDialog] `builder`.
78 3 : const FossDialog({
79 : this.title,
80 : this.description,
81 : this.content,
82 : this.actions = const <Widget>[],
83 : this.footerVariant = FossDialogFooterVariant.filled,
84 : this.showCloseButton = true,
85 : this.closeIcon,
86 : this.closeLabel = 'Close',
87 : this.semanticLabel,
88 : this.presentation,
89 : this.style,
90 : super.key,
91 : });
92 :
93 : /// The title, rendered at the top of the header.
94 : final Widget? title;
95 :
96 : /// The description, rendered below the title.
97 : final Widget? description;
98 :
99 : /// The scrollable body between the header and the footer.
100 : final Widget? content;
101 :
102 : /// The footer actions; empty hides the footer.
103 : final List<Widget> actions;
104 :
105 : /// The footer treatment. Defaults to [FossDialogFooterVariant.filled].
106 : final FossDialogFooterVariant footerVariant;
107 :
108 : /// Whether to show the close affordance in the top corner.
109 : final bool showCloseButton;
110 :
111 : /// Overrides the default painted close glyph.
112 : final Widget? closeIcon;
113 :
114 : /// Semantic label for the close affordance.
115 : final String closeLabel;
116 :
117 : /// Names the modal route for assistive technology. Defaults to the [title]
118 : /// text when it is a [Text].
119 : final String? semanticLabel;
120 :
121 : /// How the dialog presents. Null inherits the value the show function set
122 : /// (bottom sheet by default); pass one to override for a standalone surface.
123 : final FossDialogPresentation? presentation;
124 :
125 : /// Per-instance visual overrides.
126 : final FossDialogStyle? style;
127 :
128 4 : @override
129 : Widget build(BuildContext context) {
130 8 : final colors = context.fossTheme.colors;
131 4 : return _buildDialogSurface(
132 : context,
133 4 : title: title,
134 4 : description: description,
135 4 : content: content,
136 4 : actions: actions,
137 4 : footerVariant: footerVariant,
138 6 : presentation: presentation ?? _DialogPresentationScope.of(context),
139 4 : semanticLabel: semanticLabel,
140 4 : style: style,
141 4 : closeButton: showCloseButton
142 2 : ? _CloseButton(
143 2 : icon: closeIcon,
144 4 : color: colors.foreground.withValues(alpha: 0.8),
145 2 : label: closeLabel,
146 : )
147 : : null,
148 : );
149 : }
150 : }
151 :
152 : /// Resolves the theme tokens under the optional [style] and builds the shared
153 : /// [FossDialogSurface]. Both [FossDialog] and [FossAlertDialog] route through
154 : /// here; [closeButton] is present only for the plain dialog.
155 5 : Widget _buildDialogSurface(
156 : BuildContext context, {
157 : required Widget? title,
158 : required Widget? description,
159 : required Widget? content,
160 : required List<Widget> actions,
161 : required FossDialogFooterVariant footerVariant,
162 : required FossDialogPresentation presentation,
163 : required String? semanticLabel,
164 : required FossDialogStyle? style,
165 : required Widget? closeButton,
166 : }) {
167 5 : final theme = context.fossTheme;
168 5 : final colors = theme.colors;
169 5 : return FossDialogSurface(
170 : presentation: presentation,
171 4 : semanticLabel: semanticLabel ?? dialogRouteLabel(title),
172 5 : header: buildDialogHeader(
173 : theme: theme,
174 : title: title,
175 : description: description,
176 2 : titleStyleOverride: style?.titleStyle,
177 2 : descriptionStyleOverride: style?.descriptionStyle,
178 : ),
179 : content: content,
180 : actions: actions,
181 : footerVariant: footerVariant,
182 5 : visual: FossDialogVisual(
183 2 : maxWidth: style?.maxWidth ?? _maxWidth,
184 7 : backgroundColor: style?.backgroundColor ?? colors.popover,
185 7 : borderColor: style?.borderColor ?? colors.border,
186 12 : borderRadius: style?.borderRadius ?? theme.radii.xl2,
187 12 : shadows: style?.shadows ?? theme.shadows.lg,
188 : ),
189 : closeButton: closeButton,
190 : );
191 : }
192 :
193 : /// The default ghost close affordance: a painted cross that tints on press,
194 : /// inside a 48px tap target.
195 : class _CloseButton extends StatefulWidget {
196 2 : const _CloseButton({
197 : required this.icon,
198 : required this.color,
199 : required this.label,
200 : });
201 :
202 : final Widget? icon;
203 : final Color color;
204 : final String label;
205 :
206 2 : @override
207 2 : State<_CloseButton> createState() => _CloseButtonState();
208 : }
209 :
210 : class _CloseButtonState extends State<_CloseButton> {
211 : bool _pressed = false;
212 :
213 2 : void _setPressed(bool value) {
214 10 : if (_pressed != value) setState(() => _pressed = value);
215 : }
216 :
217 2 : @override
218 : Widget build(BuildContext context) {
219 2 : final theme = context.fossTheme;
220 2 : return Semantics(
221 : button: true,
222 4 : label: widget.label,
223 2 : child: GestureDetector(
224 : behavior: HitTestBehavior.opaque,
225 4 : onTapDown: (_) => _setPressed(true),
226 4 : onTapUp: (_) => _setPressed(false),
227 2 : onTapCancel: () => _setPressed(false),
228 6 : onTap: () => Navigator.of(context).maybePop(),
229 2 : child: Center(
230 2 : child: DecoratedBox(
231 2 : decoration: ShapeDecoration(
232 4 : color: _pressed ? theme.colors.accent : null,
233 2 : shape: RoundedSuperellipseBorder(
234 6 : borderRadius: BorderRadius.circular(theme.radii.md),
235 : ),
236 : ),
237 2 : child: SizedBox(
238 : width: 36,
239 : height: 36,
240 2 : child: Center(
241 : child:
242 4 : widget.icon ??
243 8 : FossGlyphIcon(CloseGlyph(widget.color), size: 18),
244 : ),
245 : ),
246 : ),
247 : ),
248 : ),
249 : );
250 : }
251 : }
|