showGlassDatePicker function

Future<DateTime?> showGlassDatePicker({
  1. required BuildContext context,
  2. DateTime? initialDateTime,
  3. GlassDatePickerMode mode = GlassDatePickerMode.date,
  4. GlassDatePickerStyle style = GlassDatePickerStyle.graphical,
  5. DateTime? minimumDate,
  6. DateTime? maximumDate,
  7. int minuteInterval = 1,
  8. bool use24hFormat = false,
  9. Color? tintColor,
  10. String title = 'Select Date',
  11. LiquidGlassSettings? settings,
  12. GlassQuality? quality,
  13. GlassSurfaceStyle surfaceStyle = GlassSurfaceStyle.glass,
  14. double? maxWidth = 400,
})

Imperatively displays a modal liquid glass date/time picker sheet.

surfaceStyle is forwarded to the GlassDatePicker inside the sheet.

maxWidth caps the modal sheet width on wide tablet and desktop viewports (defaulting to 400.0 logical pixels) while letting the sheet fill the available width on phones. Pass null to opt out and stretch the sheet to the full viewport width.

When tintColor is provided, it tints both the GlassDatePicker accent elements and the Cancel / Done header actions (resolving CupertinoDynamicColor against the active brightness). When null, header actions keep the ambient CupertinoTheme.of(context).primaryColor.

Implementation

Future<DateTime?> showGlassDatePicker({
  required BuildContext context,
  DateTime? initialDateTime,
  GlassDatePickerMode mode = GlassDatePickerMode.date,
  GlassDatePickerStyle style = GlassDatePickerStyle.graphical,
  DateTime? minimumDate,
  DateTime? maximumDate,
  int minuteInterval = 1,
  bool use24hFormat = false,
  Color? tintColor,
  String title = 'Select Date',
  LiquidGlassSettings? settings,
  GlassQuality? quality,
  GlassSurfaceStyle surfaceStyle = GlassSurfaceStyle.glass,
  double? maxWidth = 400,
}) async {
  assert(maxWidth == null || maxWidth > 0, 'maxWidth must be greater than 0.');
  DateTime selected = initialDateTime ?? DateTime.now();
  final capturedThemes = CapturedModalThemes.forNavigator(
    context,
    useRootNavigator: true,
  );

  final result = await showCupertinoModalPopup<DateTime>(
    context: context,
    builder: (modalContext) => capturedThemes.wrapBuilder(
      (themedContext) {
        final actionColor = tintColor != null
            ? CupertinoDynamicColor.resolve(tintColor, themedContext)
            : null;
        final Widget sheet = Container(
          padding: const EdgeInsets.all(16),
          margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 24),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(24),
            color: CupertinoColors.systemBackground.resolveFrom(themedContext),
            border: Border.all(
              color: CupertinoColors.white.withValues(alpha: 0.15),
              width: 0.5,
            ),
            boxShadow: [
              BoxShadow(
                color: CupertinoColors.black.withValues(alpha: 0.35),
                blurRadius: 36,
                offset: const Offset(0, 12),
              ),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CupertinoButton(
                    padding: EdgeInsets.zero,
                    child: Text(
                      'Cancel',
                      style: actionColor != null
                          ? TextStyle(color: actionColor)
                          : null,
                    ),
                    onPressed: () => Navigator.of(themedContext).pop(null),
                  ),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8),
                      child: Text(
                        title,
                        textAlign: TextAlign.center,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: 17,
                          fontWeight: FontWeight.w600,
                          color:
                              CupertinoColors.label.resolveFrom(themedContext),
                        ),
                      ),
                    ),
                  ),
                  CupertinoButton(
                    padding: EdgeInsets.zero,
                    child: Text(
                      'Done',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        color: actionColor,
                      ),
                    ),
                    onPressed: () => Navigator.of(themedContext).pop(selected),
                  ),
                ],
              ),
              const SizedBox(height: 12),
              GlassDatePicker(
                initialDateTime: selected,
                mode: mode,
                style: style,
                minimumDate: minimumDate,
                maximumDate: maximumDate,
                minuteInterval: minuteInterval,
                use24hFormat: use24hFormat,
                tintColor: tintColor,
                settings: settings,
                quality: quality,
                surfaceStyle: surfaceStyle,
                onDateTimeChanged: (val) {
                  selected = val;
                },
              ),
            ],
          ),
        );

        return DefaultTextStyle(
          style: CupertinoTheme.of(themedContext).textTheme.textStyle,
          child: maxWidth != null
              ? Center(
                  heightFactor: 1.0,
                  child: ConstrainedBox(
                    constraints: BoxConstraints(maxWidth: maxWidth),
                    child: sheet,
                  ),
                )
              : sheet,
        );
      },
    ),
  );

  return result;
}