samore_weekenddatetime_picker 0.0.3
samore_weekenddatetime_picker: ^0.0.3 copied to clipboard
A Flutter package that provides a customizable weekend date-time picker for selecting dates and times easily.
A Flutter package for selecting date and time while excluding weekends (Saturdays and Sundays). This package ensures that users can only pick weekdays and prevents the selection of past dates.
Features #
Date picker that automatically skips weekends. Integrated time picker. Customizable and easy to integrate into your Flutter project.
Getting started #
Add samore_weekenddatetime_picker to your pubspec.yaml file: dependencies: flutter: sdk: flutter samore_weekenddatetime_picker: ^1.0.0
Then, run: flutter pub get
Usage #
Import the package in your Dart file: import 'package:samore_weekenddatetime_picker/samore_weekenddatetime_picker.dart';
To use the samore_weekenddatetime_picker, simply include it within your widget tree:
import 'package:flutter/material.dart'; import 'package:intl/intl.dart';
void main() { runApp(const MyApp()); }
class MyApp extends StatelessWidget { const MyApp({super.key});
@override Widget build(BuildContext context) { return MaterialApp( title: 'DateTime 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
@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()); } }