How we can use time and date picker together in flutter?
Asked Answered
L

4

17

I want to show date and time picker together but I think no any widget or lib provides this feature in flutter. Any suggestion or solution ?

Luca answered 15/3, 2022 at 9:46 Comment(0)
N
3

Try flutter_datetime_picker here hope its help to you.

In this package you want to pick date and time both.

Try date_time_picker package also.

Updated Answer:

You used flutter_cupertino_datetime_picker and set the date format on your need

    ElevatedButton(
          onPressed: () {
            dateTimePickerWidget(context);
          },
          child: Text('Pick Date-Time'),
        ),

method for dateTimePicker:

  dateTimePickerWidget(BuildContext context) {
    return DatePicker.showDatePicker(
      context,
      dateFormat: 'dd MMMM yyyy HH:mm',
      initialDateTime: DateTime.now(),
      minDateTime: DateTime(2000),
      maxDateTime: DateTime(3000),
      onMonthChangeStartWithFirstDate: true,
      onConfirm: (dateTime, List<int> index) {
        DateTime selectdate = dateTime;
        final selIOS = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate);
        print(selIOS);
      },
    );
  }

Your Output:

16-Mar-2022 - 12:28

Result Screen for Widget -> image1

Result Screen for DateTimePicker -> enter image description here

Newspaperwoman answered 15/3, 2022 at 9:51 Comment(1)
@MuhammadUmairSaqib check my updated answer, I tried other packageNewspaperwoman
A
34

If you don't want to add extra packages, combine showDatePicker and showTimePicker.

import 'package:flutter/material.dart';

Future<DateTime?> showDateTimePicker({
  required BuildContext context,
  DateTime? initialDate,
  DateTime? firstDate,
  DateTime? lastDate,
}) async {
  initialDate ??= DateTime.now();
  firstDate ??= initialDate.subtract(const Duration(days: 365 * 100));
  lastDate ??= firstDate.add(const Duration(days: 365 * 200));

  final DateTime? selectedDate = await showDatePicker(
    context: context,
    initialDate: initialDate,
    firstDate: firstDate,
    lastDate: lastDate,
  );

  if (selectedDate == null) return null;

  if (!context.mounted) return selectedDate;

  final TimeOfDay? selectedTime = await showTimePicker(
    context: context,
    initialTime: TimeOfDay.fromDateTime(initialDate),
  );

  return selectedTime == null
      ? selectedDate
      : DateTime(
          selectedDate.year,
          selectedDate.month,
          selectedDate.day,
          selectedTime.hour,
          selectedTime.minute,
        );
}

Aundrea answered 25/4, 2023 at 5:22 Comment(1)
Thanks for the solution! I believe you wanted to write initialTime: TimeOfDay.fromDateTime(initialDate), instead of selectedDate? I made an edit on that.Haigh
N
3

Try flutter_datetime_picker here hope its help to you.

In this package you want to pick date and time both.

Try date_time_picker package also.

Updated Answer:

You used flutter_cupertino_datetime_picker and set the date format on your need

    ElevatedButton(
          onPressed: () {
            dateTimePickerWidget(context);
          },
          child: Text('Pick Date-Time'),
        ),

method for dateTimePicker:

  dateTimePickerWidget(BuildContext context) {
    return DatePicker.showDatePicker(
      context,
      dateFormat: 'dd MMMM yyyy HH:mm',
      initialDateTime: DateTime.now(),
      minDateTime: DateTime(2000),
      maxDateTime: DateTime(3000),
      onMonthChangeStartWithFirstDate: true,
      onConfirm: (dateTime, List<int> index) {
        DateTime selectdate = dateTime;
        final selIOS = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate);
        print(selIOS);
      },
    );
  }

Your Output:

16-Mar-2022 - 12:28

Result Screen for Widget -> image1

Result Screen for DateTimePicker -> enter image description here

Newspaperwoman answered 15/3, 2022 at 9:51 Comment(1)
@MuhammadUmairSaqib check my updated answer, I tried other packageNewspaperwoman
F
2

You can use this code

showDatePicker(
  context: context,
  initialDate: DateTime.now(),
  firstDate: DateTime(2000),
  lastDate: DateTime(2101),
).then((selectedDate) {
  // After selecting the date, display the time picker.
  if (selectedDate != null) {
    showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    ).then((selectedTime) {
      // Handle the selected date and time here.
      if (selectedTime != null) {
        DateTime selectedDateTime = DateTime(
          selectedDate.year,
          selectedDate.month,
          selectedDate.day,
          selectedTime.hour,
          selectedTime.minute,
        );
        print(selectedDateTime); // You can use the selectedDateTime as needed.
      }
    });
  }
});
Formfitting answered 7/11, 2023 at 21:17 Comment(1)
DateTime selectedDateTime = DateTime( sDate.year, sDate.month, sDate.day, selectedTime.hour, selectedTime.minute, ); you have not used the time varibale so time not coming use my codeFanestil
O
1

There is a package and it's also null safety.

TextButton(
onPressed: () {
    DatePicker.showDatePicker(context,
                          showTitleActions: true,
                          minTime: DateTime(2019, 3, 5),
                          maxTime: DateTime(2022, 1, 1), onChanged: (date) {
                      }, onConfirm: (date) {
                        print('Confirmed $date');
                      }, currentTime: DateTime.now(), locale: LocaleType.it);
},
child: Text(
    'Show Datetime picker (italian)',
    style: TextStyle(color: Colors.blue),
));
Overcoat answered 15/3, 2022 at 9:50 Comment(1)
this only shows date. I have tried it earlierLuca

© 2022 - 2024 — McMap. All rights reserved.