如何在flutter中实现日历功能,以便在DateTimePickerFormField中显示当前日期之后的日期,直到2天之后

问题描述 投票:2回答:1

我希望我的日历能够开始显示从当前日期后一天到后两天的日期。

final dateFormat = DateFormat.yMMMMd("en_US");

new DateTimePickerFormField(
                  dateOnly: true,
                  format: dateFormat,
                  validator: (val) {
                    if (val != null) {
                      return null;
                    } else {
                      return 'Date field is empty';
                    }
                  },
                  decoration: InputDecoration(labelText: 'Departure Date'),
                  initialValue: DateTime.now(),
                  //difference: finalValue.difference(initialValue).inDays,
                  onSaved: (value) {
                    debugPrint(value.toString());
                  },
                ),
flutter
1个回答
0
投票

使用add类的DateTime函数:

   final dateFormat = DateFormat.yMMMMd("en_US");

   new DateTimePickerFormField(
              inputType: InputType.date,
              format: dateFormat,
              validator: (val) {
                if (val != null) {
                  return null;
                } else {
                  return 'Date field is empty';
                }
              },
              decoration: InputDecoration(labelText: 'Departure Date'),
              initialDate: DateTime.now().add(Duration(days: 1)),
              firstDate: DateTime.now(),
              lastDate: DateTime.now().add(Duration(days: 3)),
              onSaved: (value) {
                debugPrint(value.toString());
              },
            ),

顺便说一句,我将代码从使用dateOnly参数更改为inputType,因为前者现已弃用并使用它将导致您将来出现兼容性问题。

© www.soinside.com 2019 - 2024. All rights reserved.