A Flutter package that provides a flexible period date picker widget with support for different date types (day, week, month) and range selection. The widget includes always-selectable year functionality and provides comprehensive date range management.
- Multiple Date Types: Support for day, week, and month period selections
- Range Selection: Select start and end dates with automatic validation
- Year Selection: Always-selectable year picker with customizable range
- Smart Validation: Automatic date range validation and adjustment
- Customizable UI: Flexible styling and configuration options
- Range Information: Display calculated duration based on selected period type
- Easy Integration: Simple widget integration with callback support
Add this to your package's pubspec.yaml file:
dependencies:
period_date_flutter: ^0.0.1Then run:
flutter pub getimport 'package:period_date_flutter/period_date_flutter.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Period Date Picker')),
body: Padding(
padding: EdgeInsets.all(16.0),
child: PeriodDatePicker(
onChanged: (PeriodDateRange range) {
print('Selected range: ${range.startDate} to ${range.endDate}');
print('Type: ${range.type}');
print('Valid: ${range.isValid}');
},
),
),
);
}
}PeriodDatePicker(
initialType: PeriodDateType.week,
initialStartDate: DateTime(2024, 1, 1),
initialEndDate: DateTime(2024, 1, 7),
firstDate: DateTime(2020),
lastDate: DateTime(2030),
title: Text(
'Select Period',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
hintText: 'Choose dates',
onChanged: (PeriodDateRange range) {
if (range.isValid) {
// Handle valid date range
_handleDateRange(range);
}
},
)void _handleDateRange(PeriodDateRange range) {
// Check if range is valid
if (!range.isValid) {
print('Invalid date range');
return;
}
// Get duration
final Duration? duration = range.duration;
if (duration != null) {
print('Duration: ${duration.inDays} days');
}
// Create a copy with modifications
final PeriodDateRange newRange = range.copyWith(
type: PeriodDateType.month,
);
// Access individual properties
print('Start: ${range.startDate}');
print('End: ${range.endDate}');
print('Type: ${range.type}');
}// Day selection
PeriodDatePicker(
initialType: PeriodDateType.day,
onChanged: (range) {
if (range.type == PeriodDateType.day) {
print('Selected ${range.duration?.inDays} days');
}
},
)
// Week selection
PeriodDatePicker(
initialType: PeriodDateType.week,
onChanged: (range) {
if (range.type == PeriodDateType.week) {
final weeks = (range.duration?.inDays ?? 0) / 7;
print('Selected ${weeks.ceil()} weeks');
}
},
)
// Month selection
PeriodDatePicker(
initialType: PeriodDateType.month,
onChanged: (range) {
if (range.type == PeriodDateType.month) {
final months = _calculateMonths(range.startDate!, range.endDate!);
print('Selected $months months');
}
},
)enum PeriodDateType {
day, // Select by days
week, // Select by weeks
month, // Select by months
}class PeriodDateRange {
final DateTime? startDate; // Start date of the range
final DateTime? endDate; // End date of the range
final PeriodDateType type; // Type of period selection
bool get isValid; // Check if range is valid
Duration? get duration; // Get duration of the range
PeriodDateRange copyWith({ // Create a copy with modifications
DateTime? startDate,
DateTime? endDate,
PeriodDateType? type,
});
}class PeriodDatePicker extends StatefulWidget {
final PeriodDateType initialType; // Initial period type
final DateTime? initialStartDate; // Initial start date
final DateTime? initialEndDate; // Initial end date
final DateTime? firstDate; // Earliest selectable date
final DateTime? lastDate; // Latest selectable date
final Function(PeriodDateRange)? onChanged; // Callback for changes
final Widget? title; // Optional title widget
final String? hintText; // Hint text for date inputs
}This package is designed to be lightweight and easy to integrate into existing Flutter applications. It follows Flutter's material design guidelines and provides a consistent user experience.
Contributions are welcome! Please feel free to submit issues and pull requests.
This package is released under the MIT License.