diff --git a/.gitignore b/.gitignore index 96486fd..79b1eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ migrate_working_dir/ # The .vscode folder contains launch configuration and tasks you configure in # VS Code which you may wish to be included in version control, so this line # is commented out by default. -#.vscode/ +.vscode/ # Flutter/Dart/Pub related # Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. @@ -28,3 +28,11 @@ migrate_working_dir/ .dart_tool/ .packages build/ + + +example/web +example/android +example/ios +example/macos +example/windows +example/linux \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index dc70be9..f0e9917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 2.1.0 +* Fixed internalization of the package +* Added dragcallback to the draggablesheet + +## 2.0.0 +* Added overlay variant of datetimepicker + ## 1.2.0 * Made date time picker height customizable diff --git a/example/.metadata b/example/.metadata index 31b763f..aa9aff4 100644 --- a/example/.metadata +++ b/example/.metadata @@ -4,7 +4,7 @@ # This file should be version controlled. version: - revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 + revision: b8f7f1f9869bb2d116aa6a70dbeac61000b52849 channel: stable project_type: app @@ -13,26 +13,11 @@ project_type: app migration: platforms: - platform: root - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - - platform: android - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - - platform: ios - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - - platform: linux - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - - platform: macos - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 + create_revision: b8f7f1f9869bb2d116aa6a70dbeac61000b52849 + base_revision: b8f7f1f9869bb2d116aa6a70dbeac61000b52849 - platform: web - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - - platform: windows - create_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 - base_revision: ffccd96b62ee8cec7740dab303538c5fc26ac543 + create_revision: b8f7f1f9869bb2d116aa6a70dbeac61000b52849 + base_revision: b8f7f1f9869bb2d116aa6a70dbeac61000b52849 # User provided section diff --git a/example/lib/main.dart b/example/lib/main.dart index 0fb86d4..500a1b5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -5,6 +5,7 @@ import 'package:datetime_picker_example/shaped_border.dart'; import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/flutter_date_time_picker.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; void main() { runApp(const MyApp()); @@ -17,6 +18,17 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( + // set locale to dutch + localizationsDelegates: const [ + GlobalWidgetsLocalizations.delegate, + GlobalMaterialLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + locale: const Locale('nl', 'NL'), + supportedLocales: const [ + Locale('nl', 'NL'), + Locale('en', 'US'), + ], title: 'Demo drag down date time picker', theme: ThemeData( primarySwatch: Colors.blue, @@ -28,8 +40,10 @@ class MyApp extends StatelessWidget { class DatePickerDemo extends StatelessWidget { const DatePickerDemo({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { + // set locale to Dutch const dateTimePickerTheme = DateTimePickerTheme( dateBoxShape: DateBoxShape.roundedRectangle, backgroundColor: Colors.white, @@ -120,6 +134,8 @@ class DatePickerDemo extends StatelessWidget { ), ), DragDownDateTimePicker( + onTimerPickerSheetChange: (value) {}, + alwaysUse24HourFormat: true, dateTimePickerTheme: const DateTimePickerTheme( backgroundColor: Colors.white, markedIndicatorColor: Colors.red, @@ -145,7 +161,7 @@ class DatePickerDemo extends StatelessWidget { ), ), markedDates: [DateTime(2022, 9, 6)], - ) + ), ], ), ); diff --git a/example/pubspec.lock b/example/pubspec.lock index 9427b51..83eeb68 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -61,7 +61,7 @@ packages: path: ".." relative: true source: path - version: "2.0.0" + version: "2.1.0" flutter_lints: dependency: "direct dev" description: @@ -69,13 +69,18 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" intl: - dependency: transitive + dependency: "direct main" description: name: intl url: "https://pub.dartlang.org" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 443ea47..59f1d24 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,7 +1,7 @@ name: datetime_picker_example description: DateTimePicker Widget -publish_to: 'none' +publish_to: "none" version: 1.0.0+1 @@ -12,13 +12,15 @@ dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 - flutter_date_time_picker: + flutter_date_time_picker: path: ../ + flutter_localizations: + sdk: flutter + intl: ^0.17.0 # Add this line dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: - uses-material-design: true diff --git a/lib/src/drag_down_date_time_picker.dart b/lib/src/drag_down_date_time_picker.dart index 12acfcc..4926f57 100644 --- a/lib/src/drag_down_date_time_picker.dart +++ b/lib/src/drag_down_date_time_picker.dart @@ -2,8 +2,6 @@ // // SPDX-License-Identifier: BSD-3-Clause -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:flutter_date_time_picker/src/models/date_time_picker_theme.dart'; import 'package:flutter_date_time_picker/src/utils/date_time_picker_controller.dart'; @@ -15,110 +13,15 @@ class DragDownDateTimePicker extends StatefulWidget { /// A widget that displays a date picker from a sheet form the top of the screen. /// This sheet displays initially displays a week but can be dragged down to show a full month. /// Both views can be dragged sideways to show the next or previous week/month. - /// - /// Example: - /// ```dart - /// DatePicker( - /// dateTimePickerTheme: const DateTimePickerTheme() - /// initialDate: selectedDate, - /// highlightToday: true, - /// onTapDay: (date) { - /// setState(() { - /// selectedDate = date; - /// }); - /// }, - /// markedDates: [ - /// DateTime(2022, 3, 14), - /// ], - /// wrongTimeDialog: - /// AlertDialog( - /// title: const Text('Invalid Time'), - /// content: SingleChildScrollView( - /// child: ListBody( - /// children: const [ - /// Text( - /// 'The time you try to choose is disabled, try to pick another time.'), - /// ], - /// ), - /// ), - /// actions: [ - /// TextButton( - /// child: const Text('OK'), - /// onPressed: () { - /// Navigator.pop(context); - /// }, - /// ), - /// ], - /// ), - /// header: Container( - /// height: 100, - /// width: MediaQuery.of(context).size.width, - /// padding: const EdgeInsets.only(bottom: 10), - /// child: Row( - /// crossAxisAlignment: CrossAxisAlignment.end, - /// mainAxisAlignment: MainAxisAlignment.center, - /// children: [ - /// const SizedBox( - /// width: 160, - /// height: 34, - /// child: Center( - /// child: Text( - /// 'Personal calendar', - /// style: TextStyle( - /// fontSize: 16, - /// fontWeight: FontWeight.w900, - /// ), - /// ), - /// ), - /// ), - /// const SizedBox( - /// width: 4, - /// ), - /// Container( - /// width: 160, - /// height: 34, - /// decoration: BoxDecoration( - /// color: const Color(0xFF00273D), - /// borderRadius: const BorderRadius.all( - /// Radius.circular(10), - /// ), - /// boxShadow: [ - /// BoxShadow( - /// color: const Color(0xFF000000).withOpacity(0.50), - /// offset: const Offset(0, 6), - /// blurRadius: 9, - /// ), - /// ], - /// ), - /// child: const Center( - /// child: Text( - /// 'Work calendar', - /// style: TextStyle( - /// color: Colors.white, - /// fontSize: 16, - /// fontWeight: FontWeight.w900, - /// ), - /// ), - /// ), - /// ), - /// ], - /// ), - /// ), - /// child: Container( - /// margin: const EdgeInsets.only( - /// top: 195, - /// ), - /// child: HolidayRoster(), - /// ), - /// ), - ///``` - DragDownDateTimePicker({ + + const DragDownDateTimePicker({ this.dateTimePickerTheme = const DateTimePickerTheme(), this.header, + this.onTimerPickerSheetChange, this.onTapDay, this.highlightToday = true, this.wrongTimeDialog, - bool? use24HourFormat, + this.alwaysUse24HourFormat, this.pickTime = false, this.initialDate, this.markedDates, @@ -126,9 +29,7 @@ class DragDownDateTimePicker extends StatefulWidget { this.disabledTimes, this.child, super.key, - }) { - alwaysUse24HourFormat = use24HourFormat ?? _useTimeFormatBasedOnLocale(); - } + }); /// The child contained by the DatePicker. final Widget? child; @@ -149,8 +50,8 @@ class DragDownDateTimePicker extends StatefulWidget { final bool highlightToday; /// a [bool] to set de clock on [TimePickerDialog] to a fixed 24 or 12-hour format. - /// By default this gets determined by the [Locale] on the device. - late final bool alwaysUse24HourFormat; + /// By default this gets determined by the settings on the user device. + final bool? alwaysUse24HourFormat; /// [pickTime] is a [bool] that determines if the user is able to pick a time after picking a date using the [TimePickerDialog]. final bool pickTime; @@ -167,6 +68,10 @@ class DragDownDateTimePicker extends StatefulWidget { /// a [List] of [TimeOfDay] objects that cannot be picked in the [TimePickerDialog]. final List? disabledTimes; + /// Function that gets called when the view changes from week to month or vice versa. + /// The value is the amount of scrolledpixels. + final Function(double)? onTimerPickerSheetChange; + @override State createState() => _DragDownDateTimePickerState(); } @@ -199,6 +104,9 @@ class _DragDownDateTimePickerState extends State { _dateTimePickerController.addListener(() { setState(() {}); }); + _dragController.addListener(() { + widget.onTimerPickerSheetChange?.call(_dragController.pixels); + }); } @override @@ -279,24 +187,3 @@ class _DragDownDateTimePickerState extends State { ); } } - -bool _useTimeFormatBasedOnLocale() { - // Get LocaleName of current platform and split language- and countryCode in 2 List values. - List deviceLocale = Platform.localeName.split('_'); - - // Make LocaleName of current platform in a Locale Object - Locale defaultLocale = Locale.fromSubtags( - languageCode: deviceLocale[0], - countryCode: deviceLocale[1], - ); - - // Determine Country. - switch (defaultLocale.countryCode) { - case 'NL': - return true; - case 'US': - return false; - default: - return true; - } -} diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index b151a90..e1b40e8 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -31,31 +31,9 @@ extension DateTimeExtension on DateTime { bool get isLeapYear => (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0); + /// Returns the amount of days in the current month of the [DateTime] object int daysInMonth() { - 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; - } - - return amountOfDays; + return DateTime(year, month + 1, 0).day; } } diff --git a/lib/src/utils/date_time_picker_controller.dart b/lib/src/utils/date_time_picker_controller.dart index d789c7e..7b72994 100644 --- a/lib/src/utils/date_time_picker_controller.dart +++ b/lib/src/utils/date_time_picker_controller.dart @@ -9,10 +9,10 @@ class DateTimePickerController extends ChangeNotifier { DateTimePickerController({ required this.theme, required this.highlightToday, - required this.alwaysUse24HourFormat, required this.pickTime, required this.browsingDate, required this.selectedDate, + this.alwaysUse24HourFormat, this.header, this.wrongTimeDialog, this.markedDates, diff --git a/lib/src/widgets/month_date_time_picker/month_date_time_picker_sheet.dart b/lib/src/widgets/month_date_time_picker/month_date_time_picker_sheet.dart index 56ebc49..7cffaf9 100644 --- a/lib/src/widgets/month_date_time_picker/month_date_time_picker_sheet.dart +++ b/lib/src/widgets/month_date_time_picker/month_date_time_picker_sheet.dart @@ -33,11 +33,11 @@ class MonthDateTimePickerSheet extends StatelessWidget { height: 10, ), Text( - DateFormat.yMMMM().format( + // use localization to get the month name + DateFormat.yMMMM(Localizations.localeOf(context).toString()).format( dateTimePickerController.browsingDate, ), - style: theme.baseTheme.textStyle! - .copyWith(fontSize: 25), + style: theme.baseTheme.textStyle!.copyWith(fontSize: 25), ), SizedBox( width: MediaQuery.of(context).size.width, diff --git a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart index e59df9d..a5b9101 100644 --- a/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart +++ b/lib/src/widgets/week_date_time_picker/week_date_time_picker.dart @@ -43,7 +43,7 @@ class WeekDateTimePicker extends StatelessWidget { : () async { TimeOfDay? timeOfDay; - DateTime selectedDate = date.daysOfWeek()[index]; + var selectedDate = date.daysOfWeek()[index]; timeOfDay = const TimeOfDay(hour: 0, minute: 0); @@ -65,7 +65,7 @@ class WeekDateTimePicker extends StatelessWidget { } } - DateTime selectedDateTime = DateTime( + var selectedDateTime = DateTime( selectedDate.year, selectedDate.month, selectedDate.day, @@ -82,7 +82,7 @@ class WeekDateTimePicker extends StatelessWidget { children: [ const Spacer(), Text( - DateFormat.E() + DateFormat.E(Localizations.localeOf(context).toString()) .format( date.daysOfWeek().elementAt(index), ) diff --git a/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart b/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart index 44c1b95..d39f93f 100644 --- a/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart +++ b/lib/src/widgets/week_date_time_picker/week_date_time_picker_sheet.dart @@ -20,16 +20,17 @@ class WeekDateTimePickerSheet extends StatelessWidget { final bool showHeader; final double weekDateBoxSize; - String getDateHeader() { - List weekDays = + String _getDateHeader(BuildContext context) { + var weekDays = dateTimePickerController.browsingDate.daysOfWeek(); - String firstDay = weekDays.first.day.toString(); + var firstDay = weekDays.first.day.toString(); - String lastDay = weekDays.last.day.toString(); + var lastDay = weekDays.last.day.toString(); - String monthYear = - DateFormat("MMMM, yyyy").format(dateTimePickerController.browsingDate); + var monthYear = + DateFormat("MMMM, yyyy", Localizations.localeOf(context).toString()) + .format(dateTimePickerController.browsingDate); return '$firstDay - $lastDay $monthYear'; } @@ -50,7 +51,7 @@ class WeekDateTimePickerSheet extends StatelessWidget { ), if (showHeader) ...[ Text( - getDateHeader(), + _getDateHeader(context), style: theme.baseTheme.textStyle!.copyWith(fontSize: 9), ), const SizedBox( diff --git a/pubspec.yaml b/pubspec.yaml index 9a93c86..cc46548 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_date_time_picker -description: A new Flutter package project. -version: 2.0.0 +description: A Flutter package for date and time picker. +version: 2.1.0 homepage: https://iconica.nl/ environment: