Flutter:具有默认材料日历设计的 DateRangePicker

时间:2020-12-31 08:12:11

标签: flutter dart

如何在不使用任何包的情况下使用默认材料日历设计实现 DateRangePicker

1 个答案:

答案 0 :(得分:1)

通过使用 showDateRangePicker()

Future selectDateRange(BuildContext context) async {
DateTimeRange pickedRange = await showDateRangePicker(
    context: context,
    initialDateRange: DateTimeRange(
      start: DateTime.now(),
      end: DateTime.now(),
    ),
    firstDate: DateTime.now(),
    lastDate: DateTime(DateTime.now().year + 2),
    helpText: 'Select Date Range',
    cancelText: 'CANCEL',
    confirmText: 'OK',
    saveText: 'SAVE',
    errorFormatText: 'Invalid format.',
    errorInvalidText: 'Out of range.',
    errorInvalidRangeText: 'Invalid range.',
    fieldStartHintText: 'Start Date',
    fieldEndLabelText: 'End Date');

if (pickedRange != null) {
  print(
      'picked range ${pickedRange.start} ${pickedRange.end} ${pickedRange.duration.inDays}');
}

}

然后在 onPressed 调用它

    RaisedButton(
                  onPressed: () {
                    selectDateRange(context);
                  },
                  child: Text('select range'),
                )

output