pickDateRange static method

Future<DateTimeRange<DateTime>?> pickDateRange({
  1. required BuildContext context,
  2. DateTime? firstDate,
  3. DateTime? lastDate,
  4. PickerStyle style = PickerStyle.platform,
  5. String doneLabel = 'Done',
  6. String cancelLabel = 'Cancel',
  7. Color? backgroundColor,
  8. Color? accentColor,
})

Implementation

static Future<DateTimeRange?> pickDateRange({
  required BuildContext context,
  DateTime? firstDate,
  DateTime? lastDate,
  PickerStyle style = PickerStyle.platform,
  String doneLabel = 'Done',
  String cancelLabel = 'Cancel',
  Color? backgroundColor,
  Color? accentColor,
}) async {
  final PickerStyle pickerStyle = _resolveStyle(context, style);
  final DateTime now = DateTime.now();
  firstDate ??= DateTime(1900);
  lastDate ??= DateTime(2100);

  if (pickerStyle == PickerStyle.ios) {
    final DateTime? start = await showModalBottomSheet<DateTime>(
      context: context,
      backgroundColor: backgroundColor,
      builder: (_) {
        DateTime temp = now;
        return _CupertinoSinglePicker(
          title: 'Select Start Date',
          initial: now,
          min: firstDate!,
          max: lastDate!,
          cancelLabel: cancelLabel,
          doneLabel: doneLabel,
          accentColor: accentColor,
          onChanged: (DateTime val) => temp = val,
          onDone: () => Navigator.pop(context, temp),
        );
      },
    );

    if (start == null || !context.mounted) return null;

    final DateTime? end = await showModalBottomSheet<DateTime>(
      context: context,
      backgroundColor: backgroundColor,
      builder: (_) {
        DateTime temp = start;
        return _CupertinoSinglePicker(
          title: 'Select End Date',
          initial: start,
          min: start,
          max: lastDate!,
          cancelLabel: cancelLabel,
          doneLabel: doneLabel,
          accentColor: accentColor,
          onChanged: (DateTime val) => temp = val,
          onDone: () => Navigator.pop(context, temp),
        );
      },
    );

    if (end == null) return null;
    return DateTimeRange(start: start, end: end);
  }

  return showDateRangePicker(
    context: context,
    firstDate: firstDate,
    lastDate: lastDate,
    initialDateRange: DateTimeRange(
      start: now,
      end: now.add(const Duration(days: 7)),
    ),
    builder: (BuildContext ctx, Widget? child) =>
        _applyAccent(ctx, child, accentColor),
  );
}