2

Date Picker Image : -

I Want To Close The Date Picker When Someone Tap On Date Then Date Picker Will Be Close Here Is My Code

 _myDateTime = (await showDatePicker(
     context: context,
    initialDate: DateTime.now(),
     firstDate: DateTime.now(),
    lastDate: DateTime(2100)))!;
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • check this link https://stackoverflow.com/questions/52727535/what-is-the-correct-way-to-add-date-picker-in-flutter-app – Ijaz Ahmed Apr 03 '23 at 10:05

1 Answers1

2

You can use CalendarDatePicker widget on showDialog. It provides onDateChanged can be used to close the dialog and pass the selectedDate.

 _datePicker() async {
    _myDateTime = await showDialog(
      context: context,
      builder: (context) {
        return LayoutBuilder(builder: (_, constraints) {
          final width = constraints.maxWidth;
          final height = constraints.maxHeight;

          return Center(
            child: Material(
              // ok or cancel buttons arent needed for this case, else you can use AlertDialog for general purpose
              child: SizedBox(
                width: width * 0.8, // cant managed being expanded
                child: CalendarDatePicker(
                  initialDate: DateTime.now(),
                  firstDate: DateTime.now(),
                  lastDate: DateTime(2100),
                  onDateChanged: (DateTime value) {
                    Navigator.of(context).pop(value);
                  },
                ),
              ),
            ),
          );
        });
      },
    );

    debugPrint("selected date is $_myDateTime");
  }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56