Add extra customization options for datepicker component

- Option to customize icons to scroll to next anmd previous month
- Option to change formatting on weekdays/month (caps/no caps)
This commit is contained in:
bob 2023-02-09 11:29:08 +01:00
parent 36b323022b
commit 5cf0cc6b38
7 changed files with 59 additions and 21 deletions

View file

@ -1,3 +1,7 @@
## 2.3.1
- Added extra customization options for datetimepicker (Customizable buttons, text formatting for weekday and month)
## 2.3.0 ## 2.3.0
- Added availability of datetimepicker without button - Added availability of datetimepicker without button

View file

@ -44,32 +44,37 @@ class DatePickerDemo extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// set locale to Dutch // set locale to Dutch
const dateTimePickerTheme = DateTimePickerTheme( DateTimePickerTheme dateTimePickerTheme = DateTimePickerTheme(
dateBoxShape: DateBoxShape.roundedRectangle, dateFormatWeekday: (value) =>
value[0].toUpperCase() + value.substring(1).toLowerCase(),
dateFormatMonth: (value) => value.toUpperCase(),
prevIcon: (context) => const Icon(Icons.chevron_left_sharp),
nextIcon: (context) => const Icon(Icons.chevron_right_sharp),
dateBoxShape: DateBoxShape.circle,
backgroundColor: Colors.white, backgroundColor: Colors.white,
markedIndicatorColor: Colors.red, markedIndicatorColor: Colors.red,
baseTheme: DateBoxBaseTheme( baseTheme: const DateBoxBaseTheme(
Colors.white, Colors.white,
TextStyle(color: Colors.black), TextStyle(color: Colors.black),
), ),
selectedTheme: DateBoxSelectedTheme( selectedTheme: const DateBoxSelectedTheme(
Color(0x4BF44336), Color(0x4BF44336),
TextStyle( TextStyle(
color: Colors.red, color: Colors.black,
), ),
), ),
highlightTheme: DateBoxHighlightTheme( highlightTheme: const DateBoxHighlightTheme(
Colors.red, Colors.red,
TextStyle( TextStyle(
color: Colors.white, color: Colors.white,
), ),
), ),
barTheme: DateTimePickerBarTheme( barTheme: const DateTimePickerBarTheme(
barColor: Colors.black, barColor: Colors.pinkAccent,
barOpacity: 1, barOpacity: 1,
), textStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
paginationSize: 50, paginationSize: 25,
shapeBorder: ArrowedBorder(), shapeBorder: const ArrowedBorder(),
); );
return Scaffold( return Scaffold(
@ -139,7 +144,7 @@ class DatePickerDemo extends StatelessWidget {
return IconButton( return IconButton(
onPressed: onPressed, icon: const Icon(Icons.minimize)); onPressed: onPressed, icon: const Icon(Icons.minimize));
}, },
) ),
], ],
), ),
), ),

View file

@ -68,7 +68,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "2.2.3" version: "2.3.1"
flutter_lints: flutter_lints:
dependency: "direct dev" dependency: "direct dev"
description: description:

View file

@ -9,6 +9,10 @@ class DateTimePickerTheme {
/// The [DateTimePickerTheme] to style [DragDownDateTimePicker] in. Define a custom shape for the dates and specifically style /// The [DateTimePickerTheme] to style [DragDownDateTimePicker] in. Define a custom shape for the dates and specifically style
/// a basic, highlighted, selected and disabled date. /// a basic, highlighted, selected and disabled date.
const DateTimePickerTheme({ const DateTimePickerTheme({
this.prevIcon,
this.nextIcon,
this.dateFormatMonth,
this.dateFormatWeekday,
this.paginationSize = 25, this.paginationSize = 25,
this.weekDateBoxSize = 35, this.weekDateBoxSize = 35,
this.monthDateBoxSize = 45, this.monthDateBoxSize = 45,
@ -87,4 +91,14 @@ class DateTimePickerTheme {
/// If true the first letters of the weekdays will be displayed above the days of the month /// If true the first letters of the weekdays will be displayed above the days of the month
final bool monthWeekDayHeaders; final bool monthWeekDayHeaders;
/// This function allows you to change formatting of the month-text
final String Function(String date)? dateFormatMonth;
/// This function allows you to change formatting of weekday-text
final String Function(String date)? dateFormatWeekday;
final WidgetBuilder? nextIcon;
final WidgetBuilder? prevIcon;
} }

View file

@ -54,6 +54,11 @@ class DatePicker extends StatelessWidget {
child: Row( child: Row(
children: List.generate(DateTime.daysPerWeek, (index) { children: List.generate(DateTime.daysPerWeek, (index) {
DateFormat dateFormatter = DateFormat("EE"); DateFormat dateFormatter = DateFormat("EE");
var date = dateFormatter.format(DateTime(2022, 11, index));
if (theme.dateFormatWeekday != null) {
date = theme.dateFormatWeekday!
.call(dateFormatter.format(DateTime(2022, 11, index)));
}
return Expanded( return Expanded(
child: Center( child: Center(
child: Padding( child: Padding(
@ -61,7 +66,7 @@ class DatePicker extends StatelessWidget {
child: Text( child: Text(
// The first day in November 2022 is monday // The first day in November 2022 is monday
// We use it to properly show monday as the first day and sunday as the last one // We use it to properly show monday as the first day and sunday as the last one
dateFormatter.format(DateTime(2022, 11, index)), date,
style: textStyle.copyWith(fontWeight: FontWeight.bold), style: textStyle.copyWith(fontWeight: FontWeight.bold),
), ),
), ),

View file

@ -71,6 +71,18 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var monthText = DateFormat.yMMMM().format(
widget.controller.browsingDate,
);
if (widget.theme.dateFormatMonth != null) {
monthText = widget.theme.dateFormatMonth!.call(monthText);
}
Widget nextIcon = widget.theme.nextIcon?.call(context) ??
const Icon(Icons.arrow_circle_right_outlined);
Widget prevIcon = widget.theme.prevIcon?.call(context) ??
const Icon(Icons.arrow_circle_left_outlined);
return Column( return Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
@ -88,14 +100,12 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
(widget.dateTimeConstraint.inMonthRange(previousDate)) (widget.dateTimeConstraint.inMonthRange(previousDate))
? _goToPreviousPage ? _goToPreviousPage
: null, : null,
icon: const Icon(Icons.arrow_circle_left_outlined), icon: prevIcon,
color: widget.theme.barTheme.barColor, color: widget.theme.barTheme.barColor,
iconSize: widget.theme.paginationSize, iconSize: widget.theme.paginationSize,
), ),
Text( Text(
DateFormat.yMMMM().format( monthText,
widget.controller.browsingDate,
),
style: widget.theme.barTheme.textStyle, style: widget.theme.barTheme.textStyle,
), ),
(widget.onNextPageButtonChild != null) (widget.onNextPageButtonChild != null)
@ -109,7 +119,7 @@ class _OverlayDateTimeContentState extends State<OverlayDateTimeContent> {
(widget.dateTimeConstraint.inMonthRange(nextDate)) (widget.dateTimeConstraint.inMonthRange(nextDate))
? _goToNextPage ? _goToNextPage
: null, : null,
icon: const Icon(Icons.arrow_circle_right_outlined), icon: nextIcon,
color: widget.theme.barTheme.barColor, color: widget.theme.barTheme.barColor,
iconSize: widget.theme.paginationSize, iconSize: widget.theme.paginationSize,
), ),

View file

@ -1,6 +1,6 @@
name: flutter_date_time_picker name: flutter_date_time_picker
description: A Flutter package for date and time picker. description: A Flutter package for date and time picker.
version: 2.3.0 version: 2.3.1
homepage: https://iconica.nl/ homepage: https://iconica.nl/
environment: environment: