updated some code

This commit is contained in:
Thomas Klein Langenhorst 2022-08-29 10:56:41 +02:00
parent c8d86163e2
commit 2e7532497f
6 changed files with 86 additions and 55 deletions

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart';
import 'package:flutter_date_time_picker/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart'; import 'package:flutter_date_time_picker/src/widgets/month_date_time_picker.dart/month_date_time_picker_sheet.dart';
import 'package:flutter_date_time_picker/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart'; import 'package:flutter_date_time_picker/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart';
import 'package:intl/date_symbol_data_local.dart';
/// A widget that displays a date picker from a sheet form the top of the screen. /// A widget that displays a date picker from a sheet form the top of the screen.
/// This sheet displays initialy displays a week of days but can be dragged down to show full months. /// This sheet displays initialy displays a week of days but can be dragged down to show full months.
@ -134,6 +135,7 @@ class _DateTimePickerState extends State<DateTimePicker> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
initializeDateFormatting();
_dateTimePickerController = DateTimePickerController( _dateTimePickerController = DateTimePickerController(
highlightToday: widget.highlightToday, highlightToday: widget.highlightToday,

View file

@ -1,46 +1,54 @@
extension DatePickerUtil on DateTime { extension DatePickerUtil on DateTime {
bool isSameDayAs(DateTime selectedDate) { // Check if the current date is the same as the given date
bool sameDayAs(DateTime selectedDate) {
return selectedDate.day == day && return selectedDate.day == day &&
selectedDate.month == month && selectedDate.month == month &&
selectedDate.year == year; selectedDate.year == year;
} }
bool isDayPartOf(List<DateTime> dates) { // Check if the current date is contained in the given list
return dates.any((element) => element.isSameDayAs(this)); bool isContainedIn(List<DateTime> dates) {
return dates.any((element) => element.sameDayAs(this));
} }
List<DateTime> getDaysOfWeek() { List<DateTime> daysOfWeek() {
var startFrom = subtract(Duration(days: weekday)); var startFrom = subtract(Duration(days: weekday));
return List.generate( return List.generate(
7, 7,
(i) => startFrom.add( (i) => startFrom.add(
Duration(days: i + 1), Duration(days: i + 1),
), ),
growable: false,
); );
} }
int getDaysInMonth() { bool get isLeapYear =>
if (month == DateTime.february) { (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
final bool isLeapYear =
(year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); int daysInMonth() {
return isLeapYear ? 29 : 28; late int amountOfDays;
switch (month) {
case DateTime.january:
case DateTime.march:
case DateTime.may:
case DateTime.july:
case DateTime.august:
case DateTime.october:
case DateTime.december:
amountOfDays = 31;
break;
case DateTime.april:
case DateTime.june:
case DateTime.september:
case DateTime.november:
amountOfDays = 30;
break;
case DateTime.february:
amountOfDays = isLeapYear ? 29 : 28;
break;
} }
const List<int> daysInMonth = <int>[ return amountOfDays;
31,
-1,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
return daysInMonth[month - 1];
} }
} }

View file

@ -21,21 +21,24 @@ class MonthDateTimePicker extends StatelessWidget {
12, 12,
).weekday; ).weekday;
return SizedBox( return Container(
width: MediaQuery.of(context).size.width, width: MediaQuery.of(context).size.width,
height: 300, margin: const EdgeInsets.symmetric(horizontal: 30),
child: Center( child: Center(
child: Wrap( child: GridView.count(
spacing: 10, crossAxisSpacing: 5,
crossAxisCount: 7,
children: List.generate( children: List.generate(
DateTime(date.year, date.month).getDaysInMonth() + daysToSkip, DateTime(date.year, date.month).daysInMonth() + daysToSkip,
(index) { (index) {
if (index < daysToSkip) { if (index < daysToSkip) {
return Container( return Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.red.withOpacity(0.4), color: Colors.red.withOpacity(0.4),
border: Border.all(color: Colors.black, width: 1.5)), border: Border.all(color: Colors.black, width: 1.5),
margin: const EdgeInsets.symmetric(vertical: 5), ),
margin:
const EdgeInsets.symmetric(vertical: 5, horizontal: 5),
height: 45, height: 45,
width: 45, width: 45,
child: Center( child: Center(
@ -50,6 +53,7 @@ class MonthDateTimePicker extends StatelessWidget {
} }
return GestureDetector( return GestureDetector(
onTap: () async { onTap: () async {
// await dateTimePickerController.getDragController().animateTo( // await dateTimePickerController.getDragController().animateTo(
// 0.26, // 0.26,
@ -69,10 +73,16 @@ class MonthDateTimePicker extends StatelessWidget {
)); ));
}, },
child: Container( child: Container(
margin: const EdgeInsets.symmetric(vertical: 5), margin:
const EdgeInsets.symmetric(vertical: 5, horizontal: 5),
decoration: BoxDecoration( decoration: BoxDecoration(
color: color:
// isDisabled()
// ? Theme.of(context).disabledColor
// : Colors.transparent,
// isSelected(index, daysToSkip) // isSelected(index, daysToSkip)
// ? Theme.of(context).primaryColor.withOpacity(0.2)
// : Colors.transparent,
shouldHighlight(index, daysToSkip) shouldHighlight(index, daysToSkip)
? Theme.of(context).primaryColor ? Theme.of(context).primaryColor
: Colors.transparent, : Colors.transparent,
@ -90,7 +100,12 @@ class MonthDateTimePicker extends StatelessWidget {
style: style:
Theme.of(context).textTheme.bodyText1!.copyWith( Theme.of(context).textTheme.bodyText1!.copyWith(
color: color:
// isDisabled()
// ? Colors.white
// : Colors.transparent,
// isSelected(index, daysToSkip) // isSelected(index, daysToSkip)
// ? Theme.of(context).primaryColor
// : Colors.black,
shouldHighlight(index, daysToSkip) shouldHighlight(index, daysToSkip)
? Colors.white ? Colors.white
: Colors.black, : Colors.black,
@ -126,19 +141,23 @@ class MonthDateTimePicker extends StatelessWidget {
date.year, date.year,
date.month, date.month,
index + 1 - daysToSkip, index + 1 - daysToSkip,
).isSameDayAs( ).sameDayAs(
dateTimePickerController.highlightToday dateTimePickerController.highlightToday
? DateTime.now() ? DateTime.now()
: dateTimePickerController.selectedDate, : dateTimePickerController.selectedDate,
); );
} }
bool isDisabled() {
return true;
}
bool isSelected(int index, int daysToSkip) { bool isSelected(int index, int daysToSkip) {
return DateTime( return DateTime(
date.year, date.year,
date.month, date.month,
index + 1 - daysToSkip, index + 1 - daysToSkip,
).isSameDayAs(dateTimePickerController.selectedDate); ).sameDayAs(dateTimePickerController.selectedDate);
} }
bool shouldMark(int index, int daysToSkip) { bool shouldMark(int index, int daysToSkip) {
@ -146,7 +165,7 @@ class MonthDateTimePicker extends StatelessWidget {
date.year, date.year,
date.month, date.month,
index + 1 - daysToSkip, index + 1 - daysToSkip,
).isSameDayAs( ).sameDayAs(
dateTimePickerController.highlightToday dateTimePickerController.highlightToday
? DateTime.now() ? DateTime.now()
: dateTimePickerController.selectedDate, : dateTimePickerController.selectedDate,
@ -155,7 +174,7 @@ class MonthDateTimePicker extends StatelessWidget {
date.year, date.year,
date.month, date.month,
index + 1 - daysToSkip, index + 1 - daysToSkip,
).isDayPartOf( ).isContainedIn(
dateTimePickerController.markedDates ?? [], dateTimePickerController.markedDates ?? [],
); );
} }

View file

@ -25,7 +25,7 @@ class MonthDateTimePickerSheet extends StatelessWidget {
height: 10, height: 10,
), ),
Text( Text(
DateFormat("MMMM yyyy").format( DateFormat.yMMMM().format(
dateTimePickerController.browsingDate, dateTimePickerController.browsingDate,
), ),
style: Theme.of(context) style: Theme.of(context)
@ -45,7 +45,7 @@ class MonthDateTimePickerSheet extends StatelessWidget {
Duration( Duration(
days: DateTime(dateTimePickerController.browsingDate.year, days: DateTime(dateTimePickerController.browsingDate.year,
dateTimePickerController.browsingDate.month) dateTimePickerController.browsingDate.month)
.getDaysInMonth(), .daysInMonth(),
), ),
), ),
); );
@ -55,7 +55,7 @@ class MonthDateTimePickerSheet extends StatelessWidget {
Duration( Duration(
days: DateTime(dateTimePickerController.browsingDate.year, days: DateTime(dateTimePickerController.browsingDate.year,
dateTimePickerController.browsingDate.month) dateTimePickerController.browsingDate.month)
.getDaysInMonth(), .daysInMonth(),
), ),
), ),
); );

View file

@ -19,10 +19,10 @@ class WeekDateTimePicker extends StatelessWidget {
return Row( return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate( children: List.generate(
date.getDaysOfWeek().length, date.daysOfWeek().length,
(index) => GestureDetector( (index) => GestureDetector(
onTap: () { onTap: () {
dateTimePickerController.onTapDay(date.getDaysOfWeek()[index]); dateTimePickerController.onTapDay(date.daysOfWeek()[index]);
}, },
child: SizedBox( child: SizedBox(
width: 40, width: 40,
@ -31,9 +31,11 @@ class WeekDateTimePicker extends StatelessWidget {
children: [ children: [
const Spacer(), const Spacer(),
Text( Text(
DateFormat('EEEE').format( DateFormat.E()
date.getDaysOfWeek().elementAt(index), .format(
)[0], date.daysOfWeek().elementAt(index),
)
.toUpperCase()[0],
style: Theme.of(context).textTheme.titleSmall, style: Theme.of(context).textTheme.titleSmall,
), ),
const Spacer(), const Spacer(),
@ -52,7 +54,7 @@ class WeekDateTimePicker extends StatelessWidget {
children: [ children: [
Center( Center(
child: Text( child: Text(
date.getDaysOfWeek().elementAt(index).day.toString(), date.daysOfWeek().elementAt(index).day.toString(),
style: style:
Theme.of(context).textTheme.bodyMedium!.copyWith( Theme.of(context).textTheme.bodyMedium!.copyWith(
color: shouldHighlight(index) color: shouldHighlight(index)
@ -87,7 +89,7 @@ class WeekDateTimePicker extends StatelessWidget {
} }
bool shouldHighlight(int index) { bool shouldHighlight(int index) {
return date.getDaysOfWeek().elementAt(index).isSameDayAs( return date.daysOfWeek().elementAt(index).sameDayAs(
dateTimePickerController.highlightToday dateTimePickerController.highlightToday
? DateTime.now() ? DateTime.now()
: dateTimePickerController.selectedDate, : dateTimePickerController.selectedDate,
@ -96,20 +98,20 @@ class WeekDateTimePicker extends StatelessWidget {
bool isSelected(int index) { bool isSelected(int index) {
return date return date
.getDaysOfWeek() .daysOfWeek()
.elementAt(index) .elementAt(index)
.isSameDayAs(dateTimePickerController.selectedDate); .sameDayAs(dateTimePickerController.selectedDate);
} }
bool shouldMark(int index) { bool shouldMark(int index) {
return !date.getDaysOfWeek().elementAt(index).isSameDayAs( return !date.daysOfWeek().elementAt(index).sameDayAs(
dateTimePickerController.highlightToday dateTimePickerController.highlightToday
? DateTime.now() ? DateTime.now()
: dateTimePickerController.selectedDate, : dateTimePickerController.selectedDate,
) && ) &&
date date
.getDaysOfWeek() .daysOfWeek()
.elementAt(index) .elementAt(index)
.isDayPartOf(dateTimePickerController.markedDates ?? []); .isContainedIn(dateTimePickerController.markedDates ?? []);
} }
} }

View file

@ -16,7 +16,7 @@ class WeekDateTimePickerSheet extends StatelessWidget {
String getDateHeader() { String getDateHeader() {
List<DateTime> weekDays = List<DateTime> weekDays =
dateTimePickerController.browsingDate.getDaysOfWeek(); dateTimePickerController.browsingDate.daysOfWeek();
String firstDay = weekDays.first.day.toString(); String firstDay = weekDays.first.day.toString();