showModalPopupDatePicker function

Future<DateTime?> showModalPopupDatePicker(
  1. BuildContext context, {
  2. double? height,
  3. CupertinoDatePickerMode mode = CupertinoDatePickerMode.dateAndTime,
  4. DateTime? initialDateTime,
  5. DateTime? minimumDate,
  6. DateTime? maximumDate,
  7. int? minimumYear,
  8. int? maximumYear,
})

Implementation

Future<DateTime?> showModalPopupDatePicker(BuildContext context,
    {double? height,
    CupertinoDatePickerMode mode = CupertinoDatePickerMode.dateAndTime,
    DateTime? initialDateTime,
    DateTime? minimumDate,
    DateTime? maximumDate,
    int? minimumYear,
    int? maximumYear}) {
  return showCupertinoModalPopup(
    context: context,
    builder: (context) {
      DateTime dateTime = initialDateTime ?? DateTime.now();
      return Container(
        height: height ?? 260,
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  BaseButton(
                    onPressed: () {
                      Navigator.of(context).pop(null);
                    },
                    child: Text(
                      '取消',
                    ),
                  ),
                  BaseButton(
                    onPressed: () {
                      Navigator.of(context).pop(dateTime);
                    },
                    child: Text(
                      '确认',
                    ),
                  ),
                ],
              ),
            ),
            Container(
              height: (height ?? 260) - 60,
              child: CupertinoDatePicker(
                mode: mode,
                onDateTimeChanged: (changed) {
                  dateTime = changed;
                },
                initialDateTime: initialDateTime ?? DateTime.now(),
                minimumDate: minimumDate,
                maximumDate: maximumDate,
                minimumYear: minimumYear ?? 1,
                maximumYear: maximumYear,
              ),
            ),
          ],
        ),
      );
    },
  );
}