Line data Source code
1 : import 'dart:ui' show ImageFilter;
2 :
3 : import 'package:flutter/widgets.dart';
4 : import 'package:fossui/src/theme/foss_theme.dart';
5 : import 'package:fossui/src/theme/motion/foss_motion.dart';
6 :
7 : /// The scrim behind a modal overlay: black at 32% opacity (alpha 0x52).
8 : const _scrim = Color(0x52000000);
9 :
10 : /// Backdrop blur radius behind the scrim, in logical pixels.
11 : const double _blurSigma = 4;
12 :
13 : /// The spring-like curve a bottom sheet and a drawer ride in and out on.
14 : const Cubic kSheetCurve = Cubic(0.32, 0.72, 0, 1);
15 :
16 : /// Slides [child] up from the bottom edge on [kSheetCurve]; the bottom-sheet
17 : /// dialog presentation reuses the same route and motion as the drawer.
18 5 : Widget bottomSheetTransition(
19 : BuildContext context,
20 : Animation<double> animation,
21 : Animation<double> secondaryAnimation,
22 : Widget child,
23 5 : ) => SlideTransition(
24 10 : position: Tween<Offset>(begin: const Offset(0, 1), end: Offset.zero).animate(
25 5 : CurvedAnimation(parent: animation, curve: kSheetCurve),
26 : ),
27 : child: child,
28 : );
29 :
30 : /// Opens [builder] as a modal route, the shared foundation for the dialog,
31 : /// alert dialog, and drawer.
32 : ///
33 : /// Wraps [showGeneralDialog] so the framework supplies the focus trap and focus
34 : /// restoration. The scrim is painted here (a 32% black tint over a backdrop
35 : /// blur) so it can fade on its own while the content runs its own transition:
36 : /// the framework barrier is transparent and only handles taps, Esc, and focus.
37 : ///
38 : /// The active [FossThemeData] is captured here and re-provided inside the route
39 : /// (which mounts above the app's [FossTheme]), so `context.fossTheme` resolves
40 : /// the same tokens. By default the content runs a fade plus a slight scale over
41 : /// [FossMotion.overlay]; pass [transitionBuilder] and [transitionDuration] to
42 : /// override the content transition only (the drawer and the bottom sheet slide
43 : /// from an edge). The duration is zeroed under `MediaQuery.disableAnimations`.
44 6 : Future<T?> showFossModal<T>({
45 : required BuildContext context,
46 : required WidgetBuilder builder,
47 : bool barrierDismissible = true,
48 : String? barrierLabel,
49 : bool useRootNavigator = true,
50 : Duration? transitionDuration,
51 : RouteTransitionsBuilder? transitionBuilder,
52 : }) {
53 6 : final theme = context.fossTheme;
54 12 : final reduceMotion = MediaQuery.maybeOf(context)?.disableAnimations ?? false;
55 : final contentTransition = transitionBuilder ?? _fadeScale;
56 :
57 6 : return showGeneralDialog<T>(
58 : context: context,
59 : useRootNavigator: useRootNavigator,
60 : barrierDismissible: barrierDismissible,
61 : barrierLabel: barrierLabel ?? (barrierDismissible ? 'Dismiss' : null),
62 : // Transparent: the visible scrim is painted below, so it blurs and fades
63 : // independently of the content transition. This barrier only takes taps.
64 : barrierColor: const Color(0x00000000),
65 : transitionDuration: reduceMotion
66 : ? Duration.zero
67 6 : : (transitionDuration ?? theme.motion.overlay),
68 12 : pageBuilder: (context, animation, secondaryAnimation) => FossTheme(
69 : data: theme,
70 : // The route mounts outside any Material or DefaultTextStyle, so text
71 : // would fall back to the framework's debug style. Set a clean base.
72 6 : child: DefaultTextStyle(
73 18 : style: theme.typography.base.copyWith(
74 12 : color: theme.colors.popoverForeground,
75 : decoration: TextDecoration.none,
76 : ),
77 6 : child: Stack(
78 6 : children: [
79 6 : _Scrim(animation: animation),
80 6 : contentTransition(
81 : context,
82 : animation,
83 : secondaryAnimation,
84 6 : Builder(builder: builder),
85 : ),
86 : ],
87 : ),
88 : ),
89 : ),
90 : // The content transition is applied inside pageBuilder so the scrim can sit
91 : // outside it; the route-level transition is a pass-through.
92 6 : transitionBuilder: (context, animation, secondaryAnimation, child) => child,
93 : );
94 : }
95 :
96 : /// The blurred, tinted scrim, fading on the route animation. Ignores pointers
97 : /// so taps fall through to the framework barrier that dismisses the route.
98 : class _Scrim extends StatelessWidget {
99 6 : const _Scrim({required this.animation});
100 :
101 : final Animation<double> animation;
102 :
103 6 : @override
104 : Widget build(BuildContext context) {
105 6 : return Positioned.fill(
106 6 : child: IgnorePointer(
107 6 : child: FadeTransition(
108 12 : opacity: CurvedAnimation(parent: animation, curve: Curves.easeInOut),
109 6 : child: BackdropFilter(
110 6 : filter: ImageFilter.blur(sigmaX: _blurSigma, sigmaY: _blurSigma),
111 : child: const ColoredBox(color: _scrim),
112 : ),
113 : ),
114 : ),
115 : );
116 : }
117 : }
118 :
119 : /// The default content enter and exit: a fade plus a slight scale.
120 3 : Widget _fadeScale(
121 : BuildContext context,
122 : Animation<double> animation,
123 : Animation<double> secondaryAnimation,
124 : Widget child,
125 : ) {
126 3 : final curved = CurvedAnimation(parent: animation, curve: Curves.easeInOut);
127 3 : return FadeTransition(
128 : opacity: curved,
129 3 : child: ScaleTransition(
130 6 : scale: Tween<double>(begin: 0.98, end: 1).animate(curved),
131 : child: child,
132 : ),
133 : );
134 : }
|