mytooyo / board_datetime_picker

Picker to select date and time for Flutter. It is both a calendar and a picker, offering a variety of options as a package.
BSD 3-Clause "New" or "Revised" License
14 stars 12 forks source link

Selected date with custom minutes bug #49

Closed matteoberla closed 2 months ago

matteoberla commented 2 months ago

Hi,

when custom minutes are set, if i confirm the date without making any change to the date, initial date is returned so it's not rounded to the nearest minute found.

ex. -initial date = 2024-09-12 14:05:00 -confirm selection -returns same date instead of rounded 2024-09-12 14:00:00

here's the code

ElevatedButton(
            child: const Text("test"),
            onPressed: () async {
              DateTime? selected = await showBoardDateTimePicker(
                context: context,
                pickerType: DateTimePickerType.datetime,
                initialDate: DateTime.now(),
                minimumDate: null,
                maximumDate: null,
                breakpoint: 950,
                options: BoardDateTimeOptions(
                  languages: const BoardPickerLanguages.it(),
                  startDayOfWeek: DateTime.monday,
                  pickerFormat: PickerFormat.dmy,
                  customOptions: BoardPickerCustomOptions(
                    minutes: [0, 15, 30, 45],
                  ),
                  boardTitle: "title",
                  showDateButton: false,
                  pickerSubTitles: const BoardDateTimeItemTitles(
                      year: "Anno",
                      month: "Mese",
                      day: "Giorno",
                      hour: "Ora",
                      minute: "Minuto"),
                ),
              );
              print(selected);
            },
          ),

i'll try to fix the error looking into the code

mytooyo commented 2 months ago

No change is considered a cancellation, so the corrected value is not returned

matteoberla commented 2 months ago

Isn't it possibile to adjust the initial data value once showBoardDateTimePicker is called, so if it isn't rounded correctly it is forces to a valid value of the picker?

Even if it will be rounded to the first valid valu, it would be perfect

matteoberla commented 2 months ago

i've decided to handle it by rounding the input before opening the picker.

If someone will need to do this type of operation here's a function to do it.

static DateTime nearestQuarter(DateTime val) {
    return DateTime(val.year, val.month, val.day, val.hour,
        [0, 15, 30, 45][(val.minute / 15).floor()]);
  }