Home > Blockchain >  I want add CupertinoDatePicker and minimum day should be date before 2 years from today and maximum
I want add CupertinoDatePicker and minimum day should be date before 2 years from today and maximum

Time:09-06

I tried to add minimum day should be date before 2 years from today and maximum date should be date before 1 year from today. But show this error enter image description here

code

 DateTime min = new DateTime.now();
  DateTime max = new DateTime.now();

  DateTime mindate = new DateTime(min.year, min.month - 2, 0);
  DateTime maxdate = new DateTime(max.year, max.month - 1, 0);

  DateTime? selectedDate;

  void showDatePicker() {
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext builder) {
          return Container(
            height: MediaQuery.of(context).copyWith().size.height * 0.25,
            color: Colors.white,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.date,
              initialDateTime: mindate,
              onDateTimeChanged: (value) {
                if (value != null && value != selectedDate)
                  setState(() {
                    selectedDate = value;
                  });
              },
              maximumDate: maxdate,
              minimumDate: mindate,
            ),
          );
        });
  }

CodePudding user response:

Try this:

DateTime? selectedDate;
DateTime now = new DateTime.now();

  void showDatePicker() {
    DateTime mindate = new DateTime(now.year - 2, now.month, now.day);
    DateTime maxdate = new DateTime(now.year - 1, now.month, now.day);

    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext builder) {
          return Container(
            height: MediaQuery.of(context).copyWith().size.height * 0.25,
            color: Colors.white,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.date,
              initialDateTime: mindate,
              onDateTimeChanged: (value) {
                if (value != null && value != selectedDate)
                  setState(() {
                    selectedDate = value;
                  });
              },
              maximumDate: maxdate,
              minimumDate: mindate,
            ),
          );
        });
  }
  • Related