如何关闭日期选取器时,日期是选定的,而无需点击确定在Flutter

tkqqtvp1  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(132)

日期选择器图像:-

我想关闭日期选择器当有人点击日期然后日期选择器将关闭这里是我的代码

_myDateTime = (await showDatePicker(
     context: context,
    initialDate: DateTime.now(),
     firstDate: DateTime.now(),
    lastDate: DateTime(2100)))!;
wgeznvg7

wgeznvg71#

你可以在showDialog上使用CalendarDatePicker小部件。它提供了onDateChanged可以用来关闭对话框并传递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");
  }

相关问题