samore_weekenddatetime_picker 1.0.0
samore_weekenddatetime_picker: ^1.0.0 copied to clipboard
A Flutter package that provides a customizable weekend date-time picker for selecting dates and times easily.
example/main.dart
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:samore_weekenddatetime_picker/samore_weekenddatetime_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'samore_weekenddatetime_picker Example',
home: Scaffold(
appBar: AppBar(
title: const Text('DateTime Picker Example'),
),
body: const DateTimePickerExample(),
),
);
}
}
class DateTimePickerExample extends StatefulWidget {
const DateTimePickerExample({super.key});
@override
_DateTimePickerExampleState createState() => _DateTimePickerExampleState();
}
class _DateTimePickerExampleState extends State<DateTimePickerExample> {
DateTime? selectedDateTime;
TimeOfDay? selectedTime;
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () async {
final DateTime? pickedDateTime = await showDialog(
context: context,
builder: (BuildContext context) {
DateTime currentDate = DateTime.now();
while (!selectableDayPredicate(currentDate)) {
currentDate = currentDate.add(const Duration(days: 1));
}
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: const Text("Select Date and Time"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Date Picker
SizedBox(
width: 500,
child: CalendarDatePicker(
initialDate: currentDate,
firstDate: DateTime.now()
.subtract(const Duration(days: 365)),
lastDate:
DateTime.now().add(const Duration(days: 365)),
selectableDayPredicate: selectableDayPredicate,
onDateChanged: (DateTime newDate) {
setState(() {
currentDate = newDate;
});
},
),
),
// Time Picker
SizedBox(
width: 500,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () async {
final TimeOfDay? pickedTime =
await showTimePicker(
context: context,
initialTime:
selectedTime ?? TimeOfDay.now(),
);
if (pickedTime != null) {
setState(() {
selectedTime = pickedTime;
});
}
},
child: const Text(
'Select Time',
style: TextStyle(color: Colors.blue),
),
),
Text(
selectedTime != null
? '${selectedTime!.hour}:${selectedTime!.minute} ${selectedTime!.period == DayPeriod.am ? 'AM' : 'PM'}'
: DateFormat('hh:mm a').format(currentDate),
style: const TextStyle(fontSize: 16),
),
],
),
),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
if (selectedTime != null) {
currentDate = DateTime(
currentDate.year,
currentDate.month,
currentDate.day,
selectedTime!.hour,
selectedTime!.minute,
);
}
Navigator.of(context).pop(currentDate);
},
child: const Text('OK'),
),
],
);
},
);
},
);
if (pickedDateTime != null) {
setState(() {
selectedDateTime = pickedDateTime;
});
}
},
child: Text(selectedDateTime != null
? DateFormat('dd/MM/yyyy hh:mm a').format(selectedDateTime!)
: 'Select Date and Time'),
),
);
}
bool selectableDayPredicate(DateTime day) {
// Prevent selection of Saturdays, Sundays, and dates earlier than the current date
return day.weekday != DateTime.saturday &&
day.weekday != DateTime.sunday &&
!day.isBefore(DateTime.now());
}
}