Skip to content

Instantly share code, notes, and snippets.

@Hiteshpatel10
Created February 27, 2024 17:14
Show Gist options
  • Save Hiteshpatel10/7973afbd240d24b39cf41882a0669588 to your computer and use it in GitHub Desktop.
Save Hiteshpatel10/7973afbd240d24b39cf41882a0669588 to your computer and use it in GitHub Desktop.
The cupertinoCalenderDrawer function in Flutter displays a modal bottom sheet containing a Cupertino-style date picker. Users can select a date within a specified range, and the selected date is passed back via a callback function.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// final dateFormat = DateFormat('dd-MM-yyyy');
typedef DateSelectionCallback = void Function(DateTime selectedDate);
Future<void> cupertinoCalenderDrawer({
required BuildContext context,
required DateTime? initialDate,
required DateSelectionCallback onSave,
required String title,
DateTime? startDate,
DateTime? endDate,
}) async {
DateTime? selectedDate;
await showModalBottomSheet(
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16))
),
builder: (context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Column(
children: [
Text(
title,
style: Theme.of(context).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600),
),
Flexible(
child: CupertinoDatePicker(
initialDateTime: initialDate,
mode: CupertinoDatePickerMode.date,
use24hFormat: true,
minimumDate: startDate,
maximumDate: endDate,
onDateTimeChanged: (DateTime newDate) {
selectedDate = newDate;
},
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size(220, 62),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {
if (selectedDate != null) {
onSave(selectedDate!);
}
if (selectedDate == null && initialDate != null) {
onSave(initialDate);
}
Navigator.pop(context);
},
child: Text(
'Select Date',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Colors.white),
),
),
const SizedBox(height: 20),
],
),
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment