Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:flutter/widgets.dart';
4 : import 'package:fossui/src/components/dialog/foss_dialog.dart';
5 : import 'package:fossui/src/foundation/foss_dialog_surface.dart'
6 : show dialogRouteLabel;
7 : import 'package:fossui/src/foundation/foss_modal_route.dart';
8 : import 'package:fossui/src/icons/foss_glyph.dart';
9 : import 'package:fossui/src/theme/colors/foss_colors.dart';
10 : import 'package:fossui/src/theme/foss_theme.dart';
11 : import 'package:fossui/src/theme/radii/foss_radii.dart';
12 : import 'package:fossui/src/theme/typography/foss_typography.dart';
13 :
14 : part 'foss_drawer_style.dart';
15 :
16 : /// Maximum width of a side panel in logical pixels.
17 : const double _sidePanelMaxWidth = 448;
18 :
19 : /// Pill dimensions of the drag handle, long axis by short axis.
20 : const double _handleLong = 48;
21 : const double _handleShort = 4;
22 :
23 : /// A release past this fraction of the panel extent dismisses it.
24 : const double _dismissFraction = 0.5;
25 :
26 : /// An outward fling at or above this velocity (px/s) dismisses regardless of
27 : /// how far the panel has travelled.
28 : const double _flingVelocity = 700;
29 :
30 : /// The edge a [FossDrawer] anchors to and slides in from.
31 : enum FossDrawerSide {
32 : /// The bottom edge: full width, slides up. The primary mobile shape.
33 : bottom,
34 :
35 : /// The top edge: full width, slides down.
36 : top,
37 :
38 : /// The leading edge: a side panel that slides in from the start.
39 : left,
40 :
41 : /// The trailing edge: a side panel that slides in from the end.
42 : right,
43 : }
44 :
45 : /// The corner treatment of a [FossDrawer] surface.
46 : enum FossDrawerVariant {
47 : /// Rounded exposed corners (the edge that meets the page).
48 : rounded,
49 :
50 : /// Square corners on every edge.
51 : straight,
52 : }
53 :
54 : /// The footer treatment of a [FossDrawer].
55 : enum FossDrawerFooterVariant {
56 : /// No bar: the actions sit on the plain surface.
57 : bare,
58 :
59 : /// A bordered bar tinted with the muted role behind the actions.
60 : filled,
61 : }
62 :
63 : /// Opens an edge [FossDrawer] and resolves to the value passed to
64 : /// `Navigator.pop`.
65 : ///
66 : /// Slides the surface in from [side] over the drawer motion duration, draws the
67 : /// scrim, traps focus, and restores it to the opener on close, all from the
68 : /// shared modal foundation. The active theme is captured and re-provided inside
69 : /// the route. The surface can be dragged back off its edge to dismiss.
70 : ///
71 : /// ```dart
72 : /// final applied = await showFossDrawer<bool>(
73 : /// context: context,
74 : /// builder: (context) => FossDrawer(
75 : /// showHandle: true,
76 : /// title: const Text('Filters'),
77 : /// content: const FilterForm(),
78 : /// actions: [
79 : /// FossButton(
80 : /// onPressed: () => Navigator.pop(context, true),
81 : /// child: const Text('Apply'),
82 : /// ),
83 : /// ],
84 : /// ),
85 : /// );
86 : /// ```
87 1 : Future<T?> showFossDrawer<T>({
88 : required BuildContext context,
89 : required WidgetBuilder builder,
90 : FossDrawerSide side = FossDrawerSide.bottom,
91 : bool barrierDismissible = true,
92 : String? barrierLabel,
93 : bool useRootNavigator = true,
94 1 : }) => showFossModal<T>(
95 : context: context,
96 : barrierDismissible: barrierDismissible,
97 : barrierLabel: barrierLabel,
98 : useRootNavigator: useRootNavigator,
99 3 : transitionDuration: context.fossTheme.motion.drawer,
100 1 : transitionBuilder: (context, animation, secondaryAnimation, child) =>
101 1 : _DrawerSlide(side: side, animation: animation, child: child),
102 2 : builder: (context) => _DrawerScope(
103 : side: side,
104 1 : child: Builder(builder: builder),
105 : ),
106 : );
107 :
108 : /// {@category Overlays}
109 : /// {@template foss.drawer.preview}
110 : /// <img src="https://fossui.org/components/drawer/overview/light.png"
111 : /// alt="FossDrawer, light theme" width="480"
112 : /// style="max-width:100%;height:auto" />
113 : /// <img src="https://fossui.org/components/drawer/overview/dark.png"
114 : /// alt="FossDrawer, dark theme" width="480"
115 : /// style="max-width:100%;height:auto" />
116 : ///
117 : /// See the [drawer documentation ↗](https://fossui.org/docs/components/drawer) or
118 : /// try it live in the
119 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/drawer/fossdrawer/playground).
120 : /// {@endtemplate}
121 : ///
122 : /// An edge-anchored modal panel with slots for a title, description, body, and
123 : /// actions, plus an optional drag handle and close affordance.
124 : ///
125 : /// Show it with [showFossDrawer], which sets the [FossDrawerSide]; the surface
126 : /// reads the side back from context so its corners, border edge, and drag axis
127 : /// match. The header, body, and footer are each optional; [actions] reuse
128 : /// `FossButton`. Colors, type, radius, and shadow resolve from the theme.
129 : ///
130 : /// {@macro foss.customize}
131 : ///
132 : /// See also [FossDialog] for a centered or bottom-sheet modal.
133 : ///
134 : /// ```dart
135 : /// showFossDrawer<void>(
136 : /// context: context,
137 : /// builder: (context) => const FossDrawer(
138 : /// showHandle: true,
139 : /// title: Text('Details'),
140 : /// content: Text('A panel that slides up from the bottom edge.'),
141 : /// ),
142 : /// );
143 : /// ```
144 : class FossDrawer extends StatelessWidget {
145 : /// {@macro foss.drawer.preview}
146 : ///
147 : /// Creates a drawer surface. Build it inside a [showFossDrawer] `builder`.
148 1 : const FossDrawer({
149 : this.title,
150 : this.description,
151 : this.content,
152 : this.actions = const <Widget>[],
153 : this.variant = FossDrawerVariant.rounded,
154 : this.footerVariant = FossDrawerFooterVariant.bare,
155 : this.showHandle = false,
156 : this.showCloseButton = false,
157 : this.closeIcon,
158 : this.style,
159 : super.key,
160 : });
161 :
162 : /// The title, rendered at the top of the header.
163 : final Widget? title;
164 :
165 : /// The description, rendered below the title.
166 : final Widget? description;
167 :
168 : /// The scrollable body between the header and the footer.
169 : final Widget? content;
170 :
171 : /// The footer actions; empty hides the footer.
172 : final List<Widget> actions;
173 :
174 : /// The corner treatment. Defaults to [FossDrawerVariant.rounded].
175 : final FossDrawerVariant variant;
176 :
177 : /// The footer treatment. Defaults to [FossDrawerFooterVariant.bare].
178 : final FossDrawerFooterVariant footerVariant;
179 :
180 : /// Whether to show the drag handle on the exposed edge.
181 : final bool showHandle;
182 :
183 : /// Whether to show the close affordance in the top corner.
184 : final bool showCloseButton;
185 :
186 : /// Overrides the default painted close glyph.
187 : final Widget? closeIcon;
188 :
189 : /// Per-instance visual overrides.
190 : final FossDrawerStyle? style;
191 :
192 1 : @override
193 : Widget build(BuildContext context) {
194 1 : final side = _DrawerScope.of(context);
195 1 : final theme = context.fossTheme;
196 1 : final colors = theme.colors;
197 1 : final sp = theme.spacing;
198 1 : final s = style;
199 :
200 : // Adjacent slots collapse their touching insets so the seam reads as one
201 : // spacing(4) gap, not two stacked spacing(6) pads: the header drops its
202 : // bottom pad above content, content drops its touching edges, and the
203 : // footer keeps its own top pad. A lone slot keeps the full inset.
204 2 : final hasHeader = title != null || description != null;
205 1 : final hasContent = content != null;
206 2 : final hasFooter = actions.isNotEmpty;
207 :
208 1 : return _DrawerSurface(
209 : side: side,
210 1 : variant: variant,
211 1 : footerVariant: footerVariant,
212 2 : backgroundColor: s?.backgroundColor ?? colors.popover,
213 2 : borderColor: s?.borderColor ?? colors.border,
214 3 : borderRadius: s?.borderRadius ?? theme.radii.xl2,
215 3 : shadows: s?.shadows ?? theme.shadows.lg,
216 1 : showHandle: showHandle,
217 2 : semanticLabel: dialogRouteLabel(title),
218 1 : header: _buildHeader(theme, colors, s, tightenBottom: hasContent),
219 1 : content: content == null
220 : ? null
221 1 : : Padding(
222 1 : padding: EdgeInsets.fromLTRB(
223 1 : sp(6),
224 1 : hasHeader ? 0 : sp(6),
225 1 : sp(6),
226 1 : hasFooter ? 0 : sp(6),
227 : ),
228 1 : child: content,
229 : ),
230 1 : actions: actions,
231 1 : closeButton: showCloseButton
232 3 : ? _CloseButton(icon: closeIcon, color: colors.mutedForeground)
233 : : null,
234 : );
235 : }
236 :
237 1 : Widget? _buildHeader(
238 : FossThemeData theme,
239 : FossColors colors,
240 : FossDrawerStyle? s, {
241 : required bool tightenBottom,
242 : }) {
243 2 : if (title == null && description == null) return null;
244 3 : final titleStyle = theme.typography.xl.semibold
245 2 : .copyWith(color: colors.popoverForeground)
246 2 : .merge(s?.titleStyle);
247 2 : final descriptionStyle = theme.typography.sm
248 2 : .copyWith(color: colors.mutedForeground)
249 2 : .merge(s?.descriptionStyle);
250 :
251 1 : return Padding(
252 1 : padding: EdgeInsets.fromLTRB(
253 2 : theme.spacing(6),
254 2 : theme.spacing(6),
255 2 : theme.spacing(6),
256 4 : tightenBottom ? theme.spacing(4) : theme.spacing(6),
257 : ),
258 1 : child: Column(
259 : crossAxisAlignment: CrossAxisAlignment.start,
260 : mainAxisSize: MainAxisSize.min,
261 2 : spacing: theme.spacing(2),
262 1 : children: [
263 1 : if (title case final title?)
264 1 : DefaultTextStyle.merge(style: titleStyle, child: title),
265 1 : if (description case final description?)
266 1 : DefaultTextStyle.merge(style: descriptionStyle, child: description),
267 : ],
268 : ),
269 : );
270 : }
271 : }
272 :
273 : /// Carries the [FossDrawerSide] from [showFossDrawer] down to the [FossDrawer]
274 : /// surface, so the side is set in one place and read in another.
275 : class _DrawerScope extends InheritedWidget {
276 1 : const _DrawerScope({required this.side, required super.child});
277 :
278 : final FossDrawerSide side;
279 :
280 1 : static FossDrawerSide of(BuildContext context) =>
281 2 : context.dependOnInheritedWidgetOfExactType<_DrawerScope>()?.side ??
282 : FossDrawerSide.bottom;
283 :
284 1 : @override
285 3 : bool updateShouldNotify(_DrawerScope oldWidget) => oldWidget.side != side;
286 : }
287 :
288 : /// Slides [child] in from [side] on the route animation, on the drawer curve.
289 : class _DrawerSlide extends StatelessWidget {
290 1 : const _DrawerSlide({
291 : required this.side,
292 : required this.animation,
293 : required this.child,
294 : });
295 :
296 : final FossDrawerSide side;
297 : final Animation<double> animation;
298 : final Widget child;
299 :
300 1 : @override
301 : Widget build(BuildContext context) {
302 1 : final begin = switch (side) {
303 1 : FossDrawerSide.bottom => const Offset(0, 1),
304 1 : FossDrawerSide.top => const Offset(0, -1),
305 1 : FossDrawerSide.left => const Offset(-1, 0),
306 1 : FossDrawerSide.right => const Offset(1, 0),
307 : };
308 1 : return SlideTransition(
309 2 : position: Tween<Offset>(begin: begin, end: Offset.zero).animate(
310 2 : CurvedAnimation(parent: animation, curve: kSheetCurve),
311 : ),
312 1 : child: child,
313 : );
314 : }
315 : }
316 :
317 : /// The anchored, drag-dismissable panel: shape, shadow, slots, and the gesture
318 : /// that tracks the finger off the edge.
319 : class _DrawerSurface extends StatefulWidget {
320 1 : const _DrawerSurface({
321 : required this.side,
322 : required this.variant,
323 : required this.footerVariant,
324 : required this.backgroundColor,
325 : required this.borderColor,
326 : required this.borderRadius,
327 : required this.shadows,
328 : required this.showHandle,
329 : required this.semanticLabel,
330 : required this.header,
331 : required this.content,
332 : required this.actions,
333 : required this.closeButton,
334 : });
335 :
336 : final FossDrawerSide side;
337 : final FossDrawerVariant variant;
338 : final FossDrawerFooterVariant footerVariant;
339 : final Color backgroundColor;
340 : final Color borderColor;
341 : final double borderRadius;
342 : final List<BoxShadow> shadows;
343 : final bool showHandle;
344 :
345 : /// Names the modal route for assistive technology, from the title text.
346 : final String? semanticLabel;
347 : final Widget? header;
348 : final Widget? content;
349 : final List<Widget> actions;
350 : final Widget? closeButton;
351 :
352 1 : @override
353 1 : State<_DrawerSurface> createState() => _DrawerSurfaceState();
354 : }
355 :
356 : class _DrawerSurfaceState extends State<_DrawerSurface>
357 : with SingleTickerProviderStateMixin {
358 : final GlobalKey _panelKey = GlobalKey();
359 : late final AnimationController _settle;
360 :
361 : /// Distance the panel has been dragged off its edge, in logical pixels. Held
362 : /// in a notifier so the drag drives the translate alone, without rebuilding
363 : /// the panel each frame.
364 : final ValueNotifier<double> _offset = ValueNotifier<double>(0);
365 : double _settleFrom = 0;
366 : double _settleTo = 0;
367 :
368 1 : @override
369 : void initState() {
370 1 : super.initState();
371 4 : _settle = AnimationController(vsync: this)..addListener(_onSettleTick);
372 : }
373 :
374 1 : void _onSettleTick() {
375 3 : final t = kSheetCurve.transform(_settle.value);
376 8 : _offset.value = _settleFrom + (_settleTo - _settleFrom) * t;
377 : }
378 :
379 1 : @override
380 : void dispose() {
381 2 : _settle.dispose();
382 2 : _offset.dispose();
383 1 : super.dispose();
384 : }
385 :
386 : /// Maps a positive primary drag delta (down / end) to outward travel.
387 1 : double get _outwardSign {
388 3 : final rtl = Directionality.of(context) == TextDirection.rtl;
389 2 : return switch (widget.side) {
390 1 : FossDrawerSide.bottom => 1,
391 1 : FossDrawerSide.top => -1,
392 1 : FossDrawerSide.left => rtl ? 1 : -1,
393 1 : FossDrawerSide.right => rtl ? -1 : 1,
394 : };
395 : }
396 :
397 1 : double get _panelExtent {
398 3 : final box = _panelKey.currentContext?.findRenderObject();
399 1 : if (box is! RenderBox) return 0;
400 4 : return _axisOf(widget.side) == Axis.vertical
401 2 : ? box.size.height
402 2 : : box.size.width;
403 : }
404 :
405 3 : void _onDragStart(DragStartDetails _) => _settle.stop();
406 :
407 1 : void _onDragUpdate(double primaryDelta) {
408 5 : final next = _offset.value + primaryDelta * _outwardSign;
409 3 : _offset.value = next < 0 ? 0 : next;
410 : }
411 :
412 1 : void _onDragEnd(double primaryVelocity) {
413 1 : final extent = _panelExtent;
414 2 : final outwardVelocity = primaryVelocity * _outwardSign;
415 : // A non-positive extent means the panel could not be measured; fail safe to
416 : // spring-back rather than dismissing on any release.
417 2 : final offset = _offset.value;
418 : final dismiss =
419 1 : extent > 0 &&
420 2 : (offset >= extent * _dismissFraction ||
421 1 : outwardVelocity >= _flingVelocity);
422 : if (dismiss) {
423 : // Continue off the edge (never animate back inward), then pop.
424 1 : final target = offset > extent ? offset : extent;
425 5 : _animateTo(target, then: () => Navigator.of(context).maybePop());
426 : } else {
427 1 : _animateTo(0);
428 : }
429 : }
430 :
431 1 : void _animateTo(double target, {VoidCallback? then}) {
432 : final reduceMotion =
433 3 : MediaQuery.maybeOf(context)?.disableAnimations ?? false;
434 3 : _settleFrom = _offset.value;
435 1 : _settleTo = target;
436 1 : _settle
437 1 : ..duration = reduceMotion
438 : ? Duration.zero
439 4 : : context.fossTheme.motion.drawer
440 1 : ..reset();
441 2 : final done = _settle.forward();
442 2 : unawaited(then == null ? done : done.whenComplete(then));
443 : }
444 :
445 1 : @override
446 : Widget build(BuildContext context) {
447 2 : final side = widget.side;
448 1 : final axis = _axisOf(side);
449 1 : final ltrSign = _ltrSign;
450 :
451 1 : Widget panel = ValueListenableBuilder<double>(
452 1 : valueListenable: _offset,
453 1 : builder: (context, offset, child) {
454 : final translation = switch (side) {
455 2 : FossDrawerSide.bottom => Offset(0, offset),
456 3 : FossDrawerSide.top => Offset(0, -offset),
457 4 : FossDrawerSide.left => Offset(-offset * ltrSign, 0),
458 3 : FossDrawerSide.right => Offset(offset * ltrSign, 0),
459 : };
460 1 : return Transform.translate(offset: translation, child: child);
461 : },
462 1 : child: _buildPanel(context),
463 : );
464 :
465 1 : panel = GestureDetector(
466 2 : onVerticalDragStart: axis == Axis.vertical ? _onDragStart : null,
467 1 : onVerticalDragUpdate: axis == Axis.vertical
468 3 : ? (d) => _onDragUpdate(d.primaryDelta ?? 0)
469 : : null,
470 1 : onVerticalDragEnd: axis == Axis.vertical
471 3 : ? (d) => _onDragEnd(d.primaryVelocity ?? 0)
472 : : null,
473 2 : onHorizontalDragStart: axis == Axis.horizontal ? _onDragStart : null,
474 1 : onHorizontalDragUpdate: axis == Axis.horizontal
475 3 : ? (d) => _onDragUpdate(d.primaryDelta ?? 0)
476 : : null,
477 1 : onHorizontalDragEnd: axis == Axis.horizontal
478 3 : ? (d) => _onDragEnd(d.primaryVelocity ?? 0)
479 : : null,
480 : child: panel,
481 : );
482 :
483 2 : return Align(alignment: _alignmentOf(side), child: panel);
484 : }
485 :
486 : /// The translate axis sign for a side panel, before the RTL flip handled by
487 : /// the directional anchor: start-anchored panels travel toward lower x.
488 1 : double get _ltrSign =>
489 3 : Directionality.of(context) == TextDirection.rtl ? -1 : 1;
490 :
491 : /// The system insets the panel must clear on the edges it touches the screen:
492 : /// the top for every side but bottom, the bottom for every side but top.
493 1 : EdgeInsets get _safeInsets {
494 2 : final padding = MediaQuery.paddingOf(context);
495 1 : return EdgeInsets.only(
496 4 : top: widget.side == FossDrawerSide.bottom ? 0 : padding.top,
497 4 : bottom: widget.side == FossDrawerSide.top ? 0 : padding.bottom,
498 : );
499 : }
500 :
501 1 : Widget _buildPanel(BuildContext context) {
502 1 : final theme = context.fossTheme;
503 2 : final side = widget.side;
504 3 : final corners = widget.variant == FossDrawerVariant.straight
505 : ? BorderRadius.zero
506 3 : : _exposedCorners(side, widget.borderRadius);
507 1 : final shape = RoundedSuperellipseBorder(
508 3 : side: BorderSide(color: widget.borderColor),
509 : borderRadius: corners,
510 : );
511 :
512 1 : final body = ClipPath(
513 1 : clipper: ShapeBorderClipper(
514 : shape: shape,
515 1 : textDirection: Directionality.of(context),
516 : ),
517 : // Name the route for assistive tech, and keep the title, body, and close
518 : // affordance as distinct nodes rather than merging into one panel node.
519 1 : child: Semantics(
520 : scopesRoute: true,
521 : namesRoute: true,
522 2 : label: widget.semanticLabel,
523 : explicitChildNodes: true,
524 1 : child: Stack(
525 1 : children: [
526 1 : _buildColumn(theme),
527 4 : if (widget.showHandle && _axisOf(side) == Axis.horizontal)
528 1 : Align(
529 1 : alignment: _exposedEdgeAlignment(side),
530 1 : child: _handle(theme, side),
531 : ),
532 2 : if (widget.closeButton case final button?)
533 1 : PositionedDirectional(
534 5 : top: theme.spacing(2) + _safeInsets.top,
535 2 : end: theme.spacing(2),
536 : child: button,
537 : ),
538 : ],
539 : ),
540 : ),
541 : );
542 :
543 1 : final decorated = DecoratedBox(
544 1 : decoration: ShapeDecoration(
545 2 : color: widget.backgroundColor,
546 : shape: shape,
547 2 : shadows: widget.shadows,
548 : ),
549 : child: body,
550 : );
551 :
552 1 : return KeyedSubtree(
553 1 : key: _panelKey,
554 2 : child: _axisOf(side) == Axis.vertical
555 : ? decorated
556 1 : : LayoutBuilder(
557 1 : builder: (context, constraints) {
558 4 : final available = constraints.maxWidth - theme.spacing(12);
559 1 : final width = available < _sidePanelMaxWidth
560 : ? available
561 : : _sidePanelMaxWidth;
562 1 : return SizedBox(width: width, child: decorated);
563 : },
564 : ),
565 : );
566 : }
567 :
568 1 : Widget _buildColumn(FossThemeData theme) {
569 2 : final side = widget.side;
570 2 : final vertical = _axisOf(side) == Axis.vertical;
571 2 : final handleInline = widget.showHandle && vertical;
572 1 : final insets = _safeInsets;
573 :
574 1 : return Column(
575 : mainAxisSize: vertical ? MainAxisSize.min : MainAxisSize.max,
576 : crossAxisAlignment: CrossAxisAlignment.stretch,
577 1 : children: [
578 2 : if (insets.top > 0) SizedBox(height: insets.top),
579 2 : if (handleInline && side == FossDrawerSide.bottom) _handle(theme, side),
580 2 : ?widget.header,
581 2 : if (widget.content case final content?)
582 1 : Flexible(
583 1 : child: _ScrollFade(
584 2 : fadeExtent: theme.spacing(6),
585 1 : child: SingleChildScrollView(child: content),
586 : ),
587 : ),
588 3 : if (widget.actions.isNotEmpty)
589 1 : _Footer(
590 2 : variant: widget.footerVariant,
591 1 : safeBottom: insets.bottom,
592 2 : actions: widget.actions,
593 : )
594 2 : else if (insets.bottom > 0)
595 2 : SizedBox(height: insets.bottom),
596 1 : if (handleInline && side == FossDrawerSide.top) _handle(theme, side),
597 : ],
598 : );
599 : }
600 :
601 1 : Widget _handle(FossThemeData theme, FossDrawerSide side) {
602 2 : final vertical = _axisOf(side) == Axis.vertical;
603 1 : return ExcludeSemantics(
604 1 : child: Padding(
605 3 : padding: EdgeInsets.all(theme.spacing(3)),
606 1 : child: Align(
607 1 : child: SizedBox(
608 : width: vertical ? _handleLong : _handleShort,
609 : height: vertical ? _handleShort : _handleLong,
610 1 : child: DecoratedBox(
611 1 : decoration: BoxDecoration(
612 2 : color: theme.colors.input,
613 1 : borderRadius: BorderRadius.circular(FossRadii.full),
614 : ),
615 : ),
616 : ),
617 : ),
618 : ),
619 : );
620 : }
621 : }
622 :
623 : class _Footer extends StatelessWidget {
624 1 : const _Footer({
625 : required this.variant,
626 : required this.safeBottom,
627 : required this.actions,
628 : });
629 :
630 : final FossDrawerFooterVariant variant;
631 : final double safeBottom;
632 : final List<Widget> actions;
633 :
634 1 : @override
635 : Widget build(BuildContext context) {
636 1 : final theme = context.fossTheme;
637 1 : final colors = theme.colors;
638 1 : final sp = theme.spacing;
639 2 : final filled = variant == FossDrawerFooterVariant.filled;
640 2 : final basePad = filled ? sp(4) : sp(6);
641 : // Absorb the system inset rather than stacking it on the base padding.
642 2 : final bottom = safeBottom > basePad ? safeBottom : basePad;
643 :
644 1 : return DecoratedBox(
645 1 : decoration: BoxDecoration(
646 : // The bar is the muted role at 72% of its own opacity; muted is already
647 : // translucent, so multiply rather than replace its alpha.
648 : color: filled
649 5 : ? colors.muted.withValues(alpha: colors.muted.a * 0.72)
650 : : null,
651 3 : border: filled ? Border(top: BorderSide(color: colors.border)) : null,
652 : ),
653 1 : child: Padding(
654 1 : padding: EdgeInsets.only(
655 1 : left: sp(6),
656 1 : right: sp(6),
657 1 : top: sp(4),
658 : bottom: bottom,
659 : ),
660 : // Trailing-aligned row, each action hugging its content.
661 1 : child: Row(
662 : mainAxisAlignment: MainAxisAlignment.end,
663 1 : spacing: sp(2),
664 1 : children: actions,
665 : ),
666 : ),
667 : );
668 : }
669 : }
670 :
671 : /// The default ghost close affordance: a painted cross in a 48px tap target.
672 : class _CloseButton extends StatelessWidget {
673 1 : const _CloseButton({required this.icon, required this.color});
674 :
675 : final Widget? icon;
676 : final Color color;
677 :
678 1 : @override
679 : Widget build(BuildContext context) {
680 1 : return Semantics(
681 : button: true,
682 : label: 'Close',
683 1 : child: GestureDetector(
684 : behavior: HitTestBehavior.opaque,
685 3 : onTap: () => Navigator.of(context).maybePop(),
686 1 : child: SizedBox(
687 : width: 48,
688 : height: 48,
689 1 : child: Center(
690 4 : child: icon ?? FossGlyphIcon(CloseGlyph(color), size: 16),
691 : ),
692 : ),
693 : ),
694 : );
695 : }
696 : }
697 :
698 : /// Fades the scroll edges of [child] that still have content beyond them: the
699 : /// top once it has been scrolled past, the bottom while more remains below. A
700 : /// non-scrolling child renders untouched. The fade tracks only edge crossings,
701 : /// so it repaints on the transition, not on every scroll frame.
702 : class _ScrollFade extends StatefulWidget {
703 1 : const _ScrollFade({required this.fadeExtent, required this.child});
704 :
705 : /// Length of each edge fade, in logical pixels.
706 : final double fadeExtent;
707 : final Widget child;
708 :
709 1 : @override
710 1 : State<_ScrollFade> createState() => _ScrollFadeState();
711 : }
712 :
713 : class _ScrollFadeState extends State<_ScrollFade> {
714 : bool _fadeTop = false;
715 : bool _fadeBottom = false;
716 :
717 : // depth 0 is the wrapped scroll view itself; a nested scrollable inside the
718 : // content bubbles up at a higher depth and must not drive the edge fade.
719 1 : bool _sync(ScrollMetrics metrics, int depth) {
720 1 : if (depth != 0 ||
721 1 : !metrics.hasContentDimensions ||
722 3 : axisDirectionToAxis(metrics.axisDirection) != Axis.vertical) {
723 : return false;
724 : }
725 2 : final top = metrics.extentBefore > 0;
726 2 : final bottom = metrics.extentAfter > 0;
727 4 : if (top != _fadeTop || bottom != _fadeBottom) {
728 2 : setState(() {
729 1 : _fadeTop = top;
730 1 : _fadeBottom = bottom;
731 : });
732 : }
733 : return false;
734 : }
735 :
736 1 : @override
737 : Widget build(BuildContext context) {
738 : // ScrollMetricsNotification covers size changes with no scroll (content or
739 : // viewport resize); ScrollNotification covers the scroll itself.
740 1 : final listener = NotificationListener<ScrollMetricsNotification>(
741 4 : onNotification: (n) => _sync(n.metrics, n.depth),
742 1 : child: NotificationListener<ScrollNotification>(
743 4 : onNotification: (n) => _sync(n.metrics, n.depth),
744 2 : child: widget.child,
745 : ),
746 : );
747 2 : if (!_fadeTop && !_fadeBottom) return listener;
748 :
749 1 : return ShaderMask(
750 : blendMode: BlendMode.dstIn,
751 1 : shaderCallback: (rect) {
752 5 : final f = (widget.fadeExtent / rect.height).clamp(0.0, 0.5);
753 1 : return LinearGradient(
754 : begin: Alignment.topCenter,
755 : end: Alignment.bottomCenter,
756 1 : colors: [
757 3 : if (_fadeTop) const Color(0x00000000) else const Color(0xFF000000),
758 1 : const Color(0xFF000000),
759 1 : const Color(0xFF000000),
760 1 : if (_fadeBottom)
761 1 : const Color(0x00000000)
762 : else
763 1 : const Color(0xFF000000),
764 : ],
765 2 : stops: [0, f, 1 - f, 1],
766 1 : ).createShader(rect);
767 : },
768 : child: listener,
769 : );
770 : }
771 : }
772 :
773 1 : Axis _axisOf(FossDrawerSide side) => switch (side) {
774 2 : FossDrawerSide.bottom || FossDrawerSide.top => Axis.vertical,
775 2 : FossDrawerSide.left || FossDrawerSide.right => Axis.horizontal,
776 : };
777 :
778 1 : AlignmentGeometry _alignmentOf(FossDrawerSide side) => switch (side) {
779 1 : FossDrawerSide.bottom => Alignment.bottomCenter,
780 1 : FossDrawerSide.top => Alignment.topCenter,
781 1 : FossDrawerSide.left => AlignmentDirectional.centerStart,
782 1 : FossDrawerSide.right => AlignmentDirectional.centerEnd,
783 : };
784 :
785 : /// Where the drag handle sits for a side panel (its inner, exposed edge).
786 1 : AlignmentGeometry _exposedEdgeAlignment(FossDrawerSide side) => switch (side) {
787 1 : FossDrawerSide.bottom => Alignment.topCenter,
788 1 : FossDrawerSide.top => Alignment.bottomCenter,
789 1 : FossDrawerSide.left => AlignmentDirectional.centerEnd,
790 1 : FossDrawerSide.right => AlignmentDirectional.centerStart,
791 : };
792 :
793 1 : BorderRadiusGeometry _exposedCorners(FossDrawerSide side, double r) {
794 1 : final radius = Radius.circular(r);
795 : return switch (side) {
796 2 : FossDrawerSide.bottom => BorderRadius.only(
797 : topLeft: radius,
798 : topRight: radius,
799 : ),
800 2 : FossDrawerSide.top => BorderRadius.only(
801 : bottomLeft: radius,
802 : bottomRight: radius,
803 : ),
804 2 : FossDrawerSide.left => BorderRadiusDirectional.only(
805 : topEnd: radius,
806 : bottomEnd: radius,
807 : ),
808 2 : FossDrawerSide.right => BorderRadiusDirectional.only(
809 : topStart: radius,
810 : bottomStart: radius,
811 : ),
812 : };
813 : }
|