Merge pull request #36 from totem-technologies/master

Allow caller to pick default entry mode
This commit is contained in:
Gaspard Merten 2023-04-12 09:51:16 +02:00 committed by GitHub
commit 6fa939b8b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -25,6 +25,7 @@ typedef DateTimeFieldCreator = DateTimeField Function({
DateTime? lastDate, DateTime? lastDate,
DateTimeFieldPickerMode mode, DateTimeFieldPickerMode mode,
bool use24hFormat, bool use24hFormat,
TimePickerEntryMode initialTimePickerEntryMode,
}); });
/// [DateTimeField] /// [DateTimeField]
@ -48,6 +49,7 @@ class DateTimeField extends StatelessWidget {
DateTime? firstDate, DateTime? firstDate,
DateTime? lastDate, DateTime? lastDate,
DateFormat? dateFormat, DateFormat? dateFormat,
this.initialTimePickerEntryMode = TimePickerEntryMode.dial,
}) : dateFormat = dateFormat ?? getDateFormatFromDateFieldPickerMode(mode), }) : dateFormat = dateFormat ?? getDateFormatFromDateFieldPickerMode(mode),
firstDate = firstDate ?? _kDefaultFirstSelectableDate, firstDate = firstDate ?? _kDefaultFirstSelectableDate,
lastDate = lastDate ?? _kDefaultLastSelectableDate, lastDate = lastDate ?? _kDefaultLastSelectableDate,
@ -63,6 +65,7 @@ class DateTimeField extends StatelessWidget {
this.dateTextStyle, this.dateTextStyle,
this.use24hFormat = false, this.use24hFormat = false,
this.initialEntryMode = DatePickerEntryMode.calendar, this.initialEntryMode = DatePickerEntryMode.calendar,
this.initialTimePickerEntryMode = TimePickerEntryMode.dial,
DateTime? firstDate, DateTime? firstDate,
DateTime? lastDate, DateTime? lastDate,
}) : initialDatePickerMode = null, }) : initialDatePickerMode = null,
@ -111,6 +114,9 @@ class DateTimeField extends StatelessWidget {
/// The initial entry mode for the material date picker dialog /// The initial entry mode for the material date picker dialog
final DatePickerEntryMode initialEntryMode; final DatePickerEntryMode initialEntryMode;
// the initial entry mode for the material time picker dialog
final TimePickerEntryMode initialTimePickerEntryMode;
/// Shows a dialog asking the user to pick a date ! /// Shows a dialog asking the user to pick a date !
Future<void> _selectDate(BuildContext context) async { Future<void> _selectDate(BuildContext context) async {
final DateTime initialDateTime; final DateTime initialDateTime;
@ -159,8 +165,9 @@ class DateTimeField extends StatelessWidget {
]; ];
if (modesWithTime.contains(mode)) { if (modesWithTime.contains(mode)) {
final TimeOfDay? _selectedTime = final TimeOfDay? _selectedTime = await showMaterialTimePicker(
await showMaterialTimePicker(context, initialDateTime); context, initialDateTime,
initialEntryMode: initialTimePickerEntryMode);
if (_selectedTime != null) { if (_selectedTime != null) {
_selectedDateTime = DateTime( _selectedDateTime = DateTime(
@ -180,12 +187,12 @@ class DateTimeField extends StatelessWidget {
/// Launches the Material time picker by invoking [showTimePicker]. /// Launches the Material time picker by invoking [showTimePicker].
/// Can be @[override]n to allow further customization of the picker options /// Can be @[override]n to allow further customization of the picker options
Future<TimeOfDay?> showMaterialTimePicker( Future<TimeOfDay?> showMaterialTimePicker(
BuildContext context, BuildContext context, DateTime initialDateTime,
DateTime initialDateTime, {TimePickerEntryMode initialEntryMode = TimePickerEntryMode.dial}) async {
) async {
return showTimePicker( return showTimePicker(
initialTime: TimeOfDay.fromDateTime(initialDateTime), initialTime: TimeOfDay.fromDateTime(initialDateTime),
context: context, context: context,
initialEntryMode: initialEntryMode,
builder: (BuildContext context, Widget? child) { builder: (BuildContext context, Widget? child) {
return MediaQuery( return MediaQuery(
data: MediaQuery.of(context).copyWith( data: MediaQuery.of(context).copyWith(

View file

@ -30,6 +30,7 @@ class DateTimeFormField extends FormField<DateTime> {
DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar, DatePickerEntryMode initialEntryMode = DatePickerEntryMode.calendar,
DatePickerMode initialDatePickerMode = DatePickerMode.day, DatePickerMode initialDatePickerMode = DatePickerMode.day,
DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime, DateTimeFieldPickerMode mode = DateTimeFieldPickerMode.dateAndTime,
TimePickerEntryMode initialTimePickerEntryMode = TimePickerEntryMode.dial,
DateTimeFieldCreator fieldCreator = DateTimeField.new, DateTimeFieldCreator fieldCreator = DateTimeField.new,
}) : super( }) : super(
key: key, key: key,
@ -68,6 +69,7 @@ class DateTimeFormField extends FormField<DateTime> {
mode: mode, mode: mode,
initialEntryMode: initialEntryMode, initialEntryMode: initialEntryMode,
dateTextStyle: dateTextStyle, dateTextStyle: dateTextStyle,
initialTimePickerEntryMode: initialTimePickerEntryMode,
); );
}, },
); );