LCOV - code coverage report
Current view: top level - components/date_picker - foss_date_picker.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 189 189
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:async';
       2              : 
       3              : import 'package:flutter/services.dart';
       4              : import 'package:flutter/widgets.dart';
       5              : import 'package:fossui/src/components/calendar/foss_calendar.dart';
       6              : import 'package:fossui/src/components/dialog/foss_dialog.dart';
       7              : import 'package:fossui/src/foundation/foss_dialog_surface.dart'
       8              :     show FossDialogPresentation;
       9              : import 'package:fossui/src/foundation/foss_field_box.dart';
      10              : import 'package:fossui/src/foundation/foss_since.dart';
      11              : import 'package:fossui/src/icons/foss_glyph.dart';
      12              : import 'package:fossui/src/theme/theme.dart';
      13              : 
      14              : part 'foss_date_picker_style.dart';
      15              : 
      16              : const double _glyphSize = 16;
      17              : const double _triggerMinHeight = 36;
      18              : const double _darkFillOpacity = 0.32;
      19              : 
      20              : const List<String> _monthNames = [
      21              :   'January',
      22              :   'February',
      23              :   'March',
      24              :   'April',
      25              :   'May',
      26              :   'June',
      27              :   'July',
      28              :   'August',
      29              :   'September',
      30              :   'October',
      31              :   'November',
      32              :   'December',
      33              : ];
      34              : 
      35              : /// Which selection shape the picker holds, and how its label formats.
      36              : enum _DatePickerMode { single, range }
      37              : 
      38              : /// The built-in single-date label: month name, ordinal day, and year, as in
      39              : /// `March 6th, 2026`.
      40            1 : String _defaultSingleFormat(DateTime date) =>
      41            7 :     '${_monthNames[date.month - 1]} ${_ordinalDay(date.day)}, ${date.year}';
      42              : 
      43              : /// The built-in range label: each end formatted as `Jul 09, 2026`, joined by a
      44              : /// dash. Collapses to a single end while the span covers one day.
      45            1 : String _defaultRangeFormat(FossDateRange range) {
      46            2 :   final start = _formatCompactDay(range.start);
      47            3 :   if (_isSameDay(range.start, range.end)) return start;
      48            3 :   return '$start - ${_formatCompactDay(range.end)}';
      49              : }
      50              : 
      51            1 : String _formatCompactDay(DateTime date) {
      52            4 :   final month = _monthNames[date.month - 1].substring(0, 3);
      53            3 :   final day = date.day.toString().padLeft(2, '0');
      54            2 :   return '$month $day, ${date.year}';
      55              : }
      56              : 
      57            1 : String _ordinalDay(int day) {
      58            2 :   final suffix = day >= 11 && day <= 13
      59              :       ? 'th'
      60            1 :       : switch (day % 10) {
      61            1 :           1 => 'st',
      62            1 :           2 => 'nd',
      63            1 :           3 => 'rd',
      64              :           _ => 'th',
      65              :         };
      66            1 :   return '$day$suffix';
      67              : }
      68              : 
      69            1 : bool _isSameDay(DateTime a, DateTime b) =>
      70            9 :     a.year == b.year && a.month == b.month && a.day == b.day;
      71              : 
      72              : /// {@category Inputs}
      73              : /// {@template foss.date_picker.preview}
      74              : /// <img src="https://fossui.org/components/date_picker/overview/light.png"
      75              : ///   alt="FossDatePicker, light theme" width="480"
      76              : ///   style="max-width:100%;height:auto" />
      77              : /// <img src="https://fossui.org/components/date_picker/overview/dark.png"
      78              : ///   alt="FossDatePicker, dark theme" width="480"
      79              : ///   style="max-width:100%;height:auto" />
      80              : ///
      81              : /// See the [date picker documentation ↗](https://fossui.org/docs/components/date-picker)
      82              : /// or try it live in the
      83              : /// [playground ↗](https://play.fossui.org/components/#/?path=components/date_picker/fossdatepicker/playground).
      84              : /// {@endtemplate}
      85              : ///
      86              : /// A date field that opens a calendar in a modal dialog and shows the chosen
      87              : /// date back in its trigger.
      88              : ///
      89              : /// Pick a mode with the named constructors, each typing its selection so the
      90              : /// value is never `dynamic`:
      91              : ///
      92              : /// - [FossDatePicker.single] holds one [DateTime].
      93              : /// - [FossDatePicker.range] holds a [FossDateRange] over two taps.
      94              : ///
      95              : /// The trigger is a full-width field with a leading calendar glyph and the
      96              : /// formatted date, or a [placeholder] while empty. Tapping it (or pressing
      97              : /// Enter, Space, or the down arrow) opens the calendar as a modal, presented as
      98              : /// a bottom sheet by default or a centered card via [presentation]. Picking a
      99              : /// date reports it through `onSelected`; a single pick closes the dialog, a
     100              : /// range closes once both ends are set, both governed by [closeOnSelect]. Drive
     101              : /// the open state yourself with [open] plus [onOpenChange], or leave it
     102              : /// uncontrolled.
     103              : ///
     104              : /// Bound and navigate the calendar through [minDate], [maxDate],
     105              : /// [isDateEnabled], and [firstDayOfWeek]. Supply a `format` callback for a
     106              : /// locale-aware label; the built-in default takes no date-library dependency.
     107              : /// Colors, type, and metrics come from `context.fossTheme`; pass a
     108              : /// [FossDatePickerStyle] to [style] for a one-off.
     109              : ///
     110              : /// {@macro foss.customize}
     111              : ///
     112              : /// ```dart
     113              : /// FossDatePicker.single(
     114              : ///   selected: picked,
     115              : ///   onSelected: (day) => setState(() => picked = day),
     116              : /// );
     117              : /// ```
     118              : @FossSince('0.1.1')
     119              : class FossDatePicker extends StatefulWidget {
     120            1 :   const FossDatePicker._({
     121              :     required _DatePickerMode mode,
     122              :     required this.placeholder,
     123              :     DateTime? singleSelected,
     124              :     ValueChanged<DateTime>? onSingle,
     125              :     String Function(DateTime)? singleFormat,
     126              :     FossDateRange? rangeSelected,
     127              :     ValueChanged<FossDateRange>? onRange,
     128              :     String Function(FossDateRange)? rangeFormat,
     129              :     this.open,
     130              :     this.onOpenChange,
     131              :     this.closeOnSelect = true,
     132              :     this.presentation = FossDialogPresentation.bottomSheet,
     133              :     this.firstDayOfWeek = DateTime.monday,
     134              :     this.minDate,
     135              :     this.maxDate,
     136              :     this.isDateEnabled,
     137              :     this.enabled = true,
     138              :     this.semanticsLabel,
     139              :     this.style,
     140              :     super.key,
     141              :   }) : assert(
     142            3 :          firstDayOfWeek >= DateTime.monday && firstDayOfWeek <= DateTime.sunday,
     143              :          'firstDayOfWeek must be a DateTime weekday (1 to 7).',
     144              :        ),
     145              :        _mode = mode,
     146              :        _singleSelected = singleSelected,
     147              :        _onSingle = onSingle,
     148              :        _singleFormat = singleFormat,
     149              :        _rangeSelected = rangeSelected,
     150              :        _onRange = onRange,
     151              :        _rangeFormat = rangeFormat;
     152              : 
     153              :   /// {@macro foss.date_picker.preview}
     154              :   ///
     155              :   /// Creates a single-date picker. [selected] is the chosen day (null for
     156              :   /// none); [onSelected] fires with the picked day. [format] overrides the
     157              :   /// built-in `March 6th, 2026` label.
     158              :   ///
     159              :   /// ```dart
     160              :   /// FossDatePicker.single(
     161              :   ///   selected: picked,
     162              :   ///   onSelected: (day) => setState(() => picked = day),
     163              :   /// );
     164              :   /// ```
     165            1 :   const FossDatePicker.single({
     166              :     required DateTime? selected,
     167              :     required ValueChanged<DateTime> onSelected,
     168              :     String Function(DateTime)? format,
     169              :     String placeholder = 'Pick a date',
     170              :     bool? open,
     171              :     ValueChanged<bool>? onOpenChange,
     172              :     bool closeOnSelect = true,
     173              :     FossDialogPresentation presentation = FossDialogPresentation.bottomSheet,
     174              :     int firstDayOfWeek = DateTime.monday,
     175              :     DateTime? minDate,
     176              :     DateTime? maxDate,
     177              :     bool Function(DateTime)? isDateEnabled,
     178              :     bool enabled = true,
     179              :     String? semanticsLabel,
     180              :     FossDatePickerStyle? style,
     181              :     Key? key,
     182            1 :   }) : this._(
     183              :          mode: _DatePickerMode.single,
     184              :          singleSelected: selected,
     185              :          onSingle: onSelected,
     186              :          singleFormat: format,
     187              :          placeholder: placeholder,
     188              :          open: open,
     189              :          onOpenChange: onOpenChange,
     190              :          closeOnSelect: closeOnSelect,
     191              :          presentation: presentation,
     192              :          firstDayOfWeek: firstDayOfWeek,
     193              :          minDate: minDate,
     194              :          maxDate: maxDate,
     195              :          isDateEnabled: isDateEnabled,
     196              :          enabled: enabled,
     197              :          semanticsLabel: semanticsLabel,
     198              :          style: style,
     199              :          key: key,
     200              :        );
     201              : 
     202              :   /// Creates a range picker. [selected] is the chosen span (null for none);
     203              :   /// [onSelected] fires as the range is set over two taps. [format] overrides
     204              :   /// the built-in `Jul 09, 2026 - Jul 16, 2026` label.
     205              :   ///
     206              :   /// ```dart
     207              :   /// FossDatePicker.range(
     208              :   ///   selected: span,
     209              :   ///   onSelected: (range) => setState(() => span = range),
     210              :   /// );
     211              :   /// ```
     212            1 :   const FossDatePicker.range({
     213              :     required FossDateRange? selected,
     214              :     required ValueChanged<FossDateRange> onSelected,
     215              :     String Function(FossDateRange)? format,
     216              :     String placeholder = 'Pick a date range',
     217              :     bool? open,
     218              :     ValueChanged<bool>? onOpenChange,
     219              :     bool closeOnSelect = true,
     220              :     FossDialogPresentation presentation = FossDialogPresentation.bottomSheet,
     221              :     int firstDayOfWeek = DateTime.monday,
     222              :     DateTime? minDate,
     223              :     DateTime? maxDate,
     224              :     bool Function(DateTime)? isDateEnabled,
     225              :     bool enabled = true,
     226              :     String? semanticsLabel,
     227              :     FossDatePickerStyle? style,
     228              :     Key? key,
     229            1 :   }) : this._(
     230              :          mode: _DatePickerMode.range,
     231              :          rangeSelected: selected,
     232              :          onRange: onSelected,
     233              :          rangeFormat: format,
     234              :          placeholder: placeholder,
     235              :          open: open,
     236              :          onOpenChange: onOpenChange,
     237              :          closeOnSelect: closeOnSelect,
     238              :          presentation: presentation,
     239              :          firstDayOfWeek: firstDayOfWeek,
     240              :          minDate: minDate,
     241              :          maxDate: maxDate,
     242              :          isDateEnabled: isDateEnabled,
     243              :          enabled: enabled,
     244              :          semanticsLabel: semanticsLabel,
     245              :          style: style,
     246              :          key: key,
     247              :        );
     248              : 
     249              :   final _DatePickerMode _mode;
     250              :   final DateTime? _singleSelected;
     251              :   final ValueChanged<DateTime>? _onSingle;
     252              :   final String Function(DateTime)? _singleFormat;
     253              :   final FossDateRange? _rangeSelected;
     254              :   final ValueChanged<FossDateRange>? _onRange;
     255              :   final String Function(FossDateRange)? _rangeFormat;
     256              : 
     257              :   /// Shown in the trigger while nothing is selected.
     258              :   final String placeholder;
     259              : 
     260              :   /// The controlled open state. Non-null puts the dialog in controlled mode:
     261              :   /// pair it with [onOpenChange] and rebuild on change. Null is uncontrolled.
     262              :   final bool? open;
     263              : 
     264              :   /// Called with the requested open state on every open or close, including
     265              :   /// dismissals. Required to observe changes in controlled mode.
     266              :   final ValueChanged<bool>? onOpenChange;
     267              : 
     268              :   /// Whether a completed pick closes the dialog: a single pick, or the second
     269              :   /// tap of a range. Defaults to true. Set false to keep it open.
     270              :   final bool closeOnSelect;
     271              : 
     272              :   /// How the calendar dialog presents: a bottom sheet (default) or a centered
     273              :   /// card.
     274              :   final FossDialogPresentation presentation;
     275              : 
     276              :   /// The leading weekday column, as a [DateTime] weekday (1 = Monday), passed
     277              :   /// to the calendar.
     278              :   final int firstDayOfWeek;
     279              : 
     280              :   /// The earliest selectable and navigable day, inclusive. Passed to the
     281              :   /// calendar.
     282              :   final DateTime? minDate;
     283              : 
     284              :   /// The latest selectable and navigable day, inclusive. Passed to the
     285              :   /// calendar.
     286              :   final DateTime? maxDate;
     287              : 
     288              :   /// Disables individual days on top of [minDate] and [maxDate]. Passed to the
     289              :   /// calendar.
     290              :   final bool Function(DateTime)? isDateEnabled;
     291              : 
     292              :   /// Whether the trigger accepts input. When false it dims and never opens.
     293              :   final bool enabled;
     294              : 
     295              :   /// Accessibility name for the trigger.
     296              :   final String? semanticsLabel;
     297              : 
     298              :   /// Per-instance overrides layered on the theme-resolved style.
     299              :   final FossDatePickerStyle? style;
     300              : 
     301            1 :   @override
     302            1 :   State<FossDatePicker> createState() => _FossDatePickerState();
     303              : }
     304              : 
     305              : class _FossDatePickerState extends State<FossDatePicker> {
     306              :   final FocusNode _triggerFocus = FocusNode(
     307              :     debugLabel: 'FossDatePicker trigger',
     308              :   );
     309              : 
     310              :   bool _open = false;
     311              :   bool _focused = false;
     312              : 
     313              :   // Whether the modal route is currently on the navigator. Guards double push
     314              :   // and reconciles the open state when the route closes on its own (barrier,
     315              :   // system back).
     316              :   bool _routeShowing = false;
     317              : 
     318            4 :   bool get _isOpen => widget.open ?? _open;
     319              : 
     320            1 :   @override
     321              :   void initState() {
     322            1 :     super.initState();
     323            2 :     if (widget.open ?? false) {
     324            3 :       WidgetsBinding.instance.addPostFrameCallback((_) {
     325            3 :         if (mounted && _isOpen) _showDialog();
     326              :       });
     327              :     }
     328              :   }
     329              : 
     330            1 :   @override
     331              :   void didUpdateWidget(FossDatePicker old) {
     332            1 :     super.didUpdateWidget(old);
     333              :     // Controlled mode: sync the route to the parent-owned open value after the
     334              :     // frame, since pushing a route during a build is unsafe.
     335            2 :     final target = widget.open;
     336            2 :     if (target != null && target != old.open) {
     337            3 :       WidgetsBinding.instance.addPostFrameCallback((_) {
     338            1 :         if (!mounted) return;
     339            2 :         if (target && !_routeShowing) _showDialog();
     340            2 :         if (!target && _routeShowing) _dismissDialog();
     341              :       });
     342              :     }
     343              :   }
     344              : 
     345            1 :   @override
     346              :   void dispose() {
     347            2 :     _triggerFocus.dispose();
     348            1 :     super.dispose();
     349              :   }
     350              : 
     351              :   /// The single intent entry point. Fires [FossDatePicker.onOpenChange]; in
     352              :   /// controlled mode the parent drives the route through [didUpdateWidget],
     353              :   /// otherwise this owns it.
     354            1 :   void _setOpen(bool next) {
     355            2 :     if (next == _isOpen) return;
     356            3 :     widget.onOpenChange?.call(next);
     357            2 :     if (widget.open != null) return;
     358            3 :     setState(() => _open = next);
     359            2 :     next ? _showDialog() : _dismissDialog();
     360              :   }
     361              : 
     362            1 :   void _openFromTrigger() {
     363            3 :     if (widget.enabled) _setOpen(true);
     364              :   }
     365              : 
     366            1 :   void _showDialog() {
     367            1 :     if (_routeShowing) return;
     368            1 :     _routeShowing = true;
     369            1 :     unawaited(
     370            1 :       showFossDialog<void>(
     371            1 :         context: context,
     372            2 :         presentation: widget.presentation,
     373            2 :         builder: (_) => FossDialog(
     374              :           showCloseButton: false,
     375            2 :           presentation: widget.presentation,
     376            2 :           semanticLabel: widget.semanticsLabel,
     377            1 :           content: _CalendarDialog(
     378            2 :             mode: widget._mode,
     379            2 :             singleInitial: widget._singleSelected,
     380            2 :             onSingle: widget._onSingle,
     381            2 :             rangeInitial: widget._rangeSelected,
     382            2 :             onRange: widget._onRange,
     383            2 :             closeOnSelect: widget.closeOnSelect,
     384            2 :             firstDayOfWeek: widget.firstDayOfWeek,
     385            2 :             minDate: widget.minDate,
     386            2 :             maxDate: widget.maxDate,
     387            2 :             isDateEnabled: widget.isDateEnabled,
     388              :           ),
     389              :         ),
     390            2 :       ).whenComplete(() {
     391            1 :         if (!mounted) return;
     392            1 :         _routeShowing = false;
     393              :         // The route closed on its own (a pick, a barrier tap, system back).
     394              :         // Reconcile the open state; harmless if it was already lowered.
     395            2 :         if (_isOpen) _setOpen(false);
     396              :       }),
     397              :     );
     398              :   }
     399              : 
     400            1 :   void _dismissDialog() {
     401            1 :     if (_routeShowing) {
     402            3 :       Navigator.of(context, rootNavigator: true).maybePop();
     403              :     }
     404              :   }
     405              : 
     406            3 :   bool get _hasValue => switch (widget._mode) {
     407            3 :     _DatePickerMode.single => widget._singleSelected != null,
     408            3 :     _DatePickerMode.range => widget._rangeSelected != null,
     409              :   };
     410              : 
     411            1 :   String _resolveLabel() {
     412            2 :     switch (widget._mode) {
     413            1 :       case _DatePickerMode.single:
     414            2 :         final value = widget._singleSelected;
     415            2 :         if (value == null) return widget.placeholder;
     416            3 :         return (widget._singleFormat ?? _defaultSingleFormat)(value);
     417            1 :       case _DatePickerMode.range:
     418            2 :         final value = widget._rangeSelected;
     419            2 :         if (value == null) return widget.placeholder;
     420            3 :         return (widget._rangeFormat ?? _defaultRangeFormat)(value);
     421              :     }
     422              :   }
     423              : 
     424            1 :   @override
     425              :   Widget build(BuildContext context) {
     426            1 :     final theme = context.fossTheme;
     427            3 :     final v = _resolve(theme, widget.style);
     428            1 :     final hasValue = _hasValue;
     429            1 :     final text = _resolveLabel();
     430            2 :     final textColor = hasValue ? v.foreground : v.placeholderColor;
     431              : 
     432            1 :     final box = FossFieldBox(
     433            2 :       enabled: widget.enabled,
     434              :       hasError: false,
     435            2 :       focused: _focused || _isOpen,
     436            1 :       background: v.background,
     437            1 :       borderColor: v.borderColor,
     438            1 :       ringColor: v.ringColor,
     439            1 :       destructiveColor: v.destructiveColor,
     440            1 :       borderRadius: v.borderRadius,
     441              :       minHeight: _triggerMinHeight,
     442            1 :       shadow: v.shadow,
     443            1 :       isDark: v.isDark,
     444              :       // The trigger node below carries the label as its value, so the painted
     445              :       // glyph and text stay out of the semantics tree.
     446            1 :       child: ExcludeSemantics(
     447            1 :         child: Padding(
     448            2 :           padding: EdgeInsets.symmetric(horizontal: v.horizontalPadding),
     449            1 :           child: Row(
     450            1 :             children: [
     451            2 :               FossGlyphIcon(CalendarGlyph(textColor), size: _glyphSize),
     452            2 :               SizedBox(width: v.gap),
     453            1 :               Expanded(
     454            1 :                 child: Text(
     455              :                   text,
     456              :                   maxLines: 1,
     457              :                   overflow: TextOverflow.ellipsis,
     458            2 :                   style: v.textStyle.copyWith(color: textColor),
     459              :                 ),
     460              :               ),
     461              :             ],
     462              :           ),
     463              :         ),
     464              :       ),
     465              :     );
     466              : 
     467            1 :     return Semantics(
     468              :       button: true,
     469            2 :       enabled: widget.enabled,
     470            2 :       label: widget.semanticsLabel,
     471              :       value: text,
     472            1 :       expanded: _isOpen,
     473            1 :       child: FocusableActionDetector(
     474            1 :         focusNode: _triggerFocus,
     475            2 :         enabled: widget.enabled,
     476            2 :         mouseCursor: widget.enabled
     477              :             ? SystemMouseCursors.click
     478              :             : SystemMouseCursors.basic,
     479            4 :         onShowFocusHighlight: (value) => setState(() => _focused = value),
     480              :         // Arrow Down opens the trigger, alongside the Enter and Space that
     481              :         // FocusableActionDetector maps to ActivateIntent by default.
     482              :         shortcuts: const {
     483              :           SingleActivator(LogicalKeyboardKey.arrowDown): ActivateIntent(),
     484              :         },
     485            1 :         actions: <Type, Action<Intent>>{
     486            1 :           ActivateIntent: CallbackAction<ActivateIntent>(
     487            1 :             onInvoke: (_) {
     488            1 :               _openFromTrigger();
     489              :               return null;
     490              :             },
     491              :           ),
     492              :         },
     493            1 :         child: GestureDetector(
     494              :           behavior: HitTestBehavior.opaque,
     495            3 :           onTap: widget.enabled ? _openFromTrigger : null,
     496              :           child: box,
     497              :         ),
     498              :       ),
     499              :     );
     500              :   }
     501              : }
     502              : 
     503              : /// The calendar shown inside the modal. Holds a working copy of the selection
     504              : /// so the grid updates live while the picker's parent stays controlled: each
     505              : /// pick rebuilds the calendar, reports through the picker's callback, and pops
     506              : /// the route when the pick completes under [closeOnSelect].
     507              : class _CalendarDialog extends StatefulWidget {
     508            1 :   const _CalendarDialog({
     509              :     required this.mode,
     510              :     required this.closeOnSelect,
     511              :     required this.firstDayOfWeek,
     512              :     this.singleInitial,
     513              :     this.onSingle,
     514              :     this.rangeInitial,
     515              :     this.onRange,
     516              :     this.minDate,
     517              :     this.maxDate,
     518              :     this.isDateEnabled,
     519              :   });
     520              : 
     521              :   final _DatePickerMode mode;
     522              :   final DateTime? singleInitial;
     523              :   final ValueChanged<DateTime>? onSingle;
     524              :   final FossDateRange? rangeInitial;
     525              :   final ValueChanged<FossDateRange>? onRange;
     526              :   final bool closeOnSelect;
     527              :   final int firstDayOfWeek;
     528              :   final DateTime? minDate;
     529              :   final DateTime? maxDate;
     530              :   final bool Function(DateTime)? isDateEnabled;
     531              : 
     532            1 :   @override
     533            1 :   State<_CalendarDialog> createState() => _CalendarDialogState();
     534              : }
     535              : 
     536              : class _CalendarDialogState extends State<_CalendarDialog> {
     537              :   DateTime? _single;
     538              :   FossDateRange? _range;
     539              : 
     540              :   // Range picks arrive over two taps; this flips on each pick so a completed
     541              :   // range can close.
     542              :   bool _awaitingRangeEnd = false;
     543              : 
     544            1 :   @override
     545              :   void initState() {
     546            1 :     super.initState();
     547            3 :     _single = widget.singleInitial;
     548            3 :     _range = widget.rangeInitial;
     549              :   }
     550              : 
     551            1 :   void _handleSingle(DateTime day) {
     552            3 :     setState(() => _single = day);
     553            3 :     widget.onSingle?.call(day);
     554            2 :     if (widget.closeOnSelect) {
     555            3 :       Navigator.of(context).maybePop();
     556              :     }
     557              :   }
     558              : 
     559            1 :   void _handleRange(FossDateRange range) {
     560            3 :     setState(() => _range = range);
     561            3 :     widget.onRange?.call(range);
     562            2 :     _awaitingRangeEnd = !_awaitingRangeEnd;
     563            3 :     if (widget.closeOnSelect && !_awaitingRangeEnd) {
     564            3 :       Navigator.of(context).maybePop();
     565              :     }
     566              :   }
     567              : 
     568            1 :   @override
     569              :   Widget build(BuildContext context) {
     570            2 :     final calendar = switch (widget.mode) {
     571            2 :       _DatePickerMode.single => FossCalendar.single(
     572            1 :         selected: _single,
     573            1 :         onSelected: _handleSingle,
     574            2 :         firstDayOfWeek: widget.firstDayOfWeek,
     575            2 :         minDate: widget.minDate,
     576            2 :         maxDate: widget.maxDate,
     577            2 :         isDateEnabled: widget.isDateEnabled,
     578              :       ),
     579            2 :       _DatePickerMode.range => FossCalendar.range(
     580            1 :         selected: _range,
     581            1 :         onSelected: _handleRange,
     582            2 :         firstDayOfWeek: widget.firstDayOfWeek,
     583            2 :         minDate: widget.minDate,
     584            2 :         maxDate: widget.maxDate,
     585            2 :         isDateEnabled: widget.isDateEnabled,
     586              :       ),
     587              :     };
     588            1 :     return Center(child: calendar);
     589              :   }
     590              : }
     591              : 
     592              : /// Builds the default trigger appearance from the theme tokens, then lays the
     593              : /// date-picker-specific fields of [override] over it.
     594            1 : _DatePickerVisuals _resolve(
     595              :   FossThemeData theme,
     596              :   FossDatePickerStyle? override,
     597              : ) {
     598            1 :   final c = theme.colors;
     599              :   // Dark lifts the resting trigger fill by the input color, matching the field
     600              :   // family; light is the bare surface.
     601            1 :   final background = c.isDark
     602            1 :       ? Color.alphaBlend(
     603            5 :           c.input.withValues(alpha: c.input.a * _darkFillOpacity),
     604            1 :           c.background,
     605              :         )
     606            1 :       : c.background;
     607            1 :   return _DatePickerVisuals(
     608              :     background: background,
     609            1 :     foreground: c.foreground,
     610            1 :     placeholderColor: override?.placeholderColor ?? c.mutedForeground,
     611            1 :     borderColor: c.input,
     612            1 :     ringColor: c.ring,
     613            1 :     destructiveColor: c.destructive,
     614            2 :     borderRadius: theme.radii.lg,
     615            2 :     horizontalPadding: theme.spacing(3),
     616            2 :     textStyle: theme.typography.sm,
     617            2 :     shadow: theme.shadows.xs,
     618            2 :     gap: override?.gap ?? theme.spacing(1),
     619            1 :     isDark: c.isDark,
     620              :   );
     621              : }
     622              : 
     623              : /// The fully resolved, non-null trigger appearance.
     624              : @immutable
     625              : class _DatePickerVisuals {
     626            1 :   const _DatePickerVisuals({
     627              :     required this.background,
     628              :     required this.foreground,
     629              :     required this.placeholderColor,
     630              :     required this.borderColor,
     631              :     required this.ringColor,
     632              :     required this.destructiveColor,
     633              :     required this.borderRadius,
     634              :     required this.horizontalPadding,
     635              :     required this.textStyle,
     636              :     required this.shadow,
     637              :     required this.gap,
     638              :     required this.isDark,
     639              :   });
     640              : 
     641              :   final Color background;
     642              :   final Color foreground;
     643              :   final Color placeholderColor;
     644              :   final Color borderColor;
     645              :   final Color ringColor;
     646              :   final Color destructiveColor;
     647              :   final double borderRadius;
     648              :   final double horizontalPadding;
     649              :   final TextStyle textStyle;
     650              :   final List<BoxShadow> shadow;
     651              :   final double gap;
     652              :   final bool isDark;
     653              : }
        

Generated by: LCOV version 2.0-1