You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Flutter Dart预约选择器,限制仅选24小时后日期

预约日期时间选择限制实现方案

以下是针对你的需求修改后的代码,核心是限制用户仅能选择当前时间24小时之后的日期和时间,修改点清晰标注:

修改后的完整代码

import 'package:get/get.dart';
import 'package:demandium/components/core_export.dart';

class ScheduleController extends GetxController{

  final ScheduleRepo scheduleRepo;
  ScheduleController({required this.scheduleRepo});

  ///Selected date of day
  DateTime _selectedDate = DateTime.now().add(const Duration(hours: 24)); // 初始值设为当前时间24小时后
  DateTime get selectedData => _selectedDate;

  String _schedule = '';
  String get schedule => _schedule;

  String? _postId;

  Future<void> selectDate() async {
    final DateTime earliestAllowed = DateTime.now().add(const Duration(hours: 24));
    final DateTime? picked = await showDatePicker(
        context: Get.context!,
        initialDate: earliestAllowed, // 初始日期设为最早允许的日期
        firstDate: earliestAllowed, // 禁止选择24小时内的日期
        lastDate: DateTime(2101));
    if (picked != null) {
      _selectedDate = picked;
      update();
      selectTimeOfDay();
    }
  }

  Future<void> selectTimeOfDay() async {
    final DateTime earliestAllowed = DateTime.now().add(const Duration(hours: 24));
    TimeOfDay initialTime;
    
    // 若选中的是最早允许的日期,初始时间设为对应时刻;否则设为当天0点
    if (_selectedDate.year == earliestAllowed.year &&
        _selectedDate.month == earliestAllowed.month &&
        _selectedDate.day == earliestAllowed.day) {
      initialTime = TimeOfDay.fromDateTime(earliestAllowed);
    } else {
      initialTime = const TimeOfDay(hour: 0, minute: 0);
    }

    final TimeOfDay? pickedTime = await showTimePicker(
        context: Get.context!,
        initialTime: initialTime);

    if (pickedTime != null) {
      DateTime selectedDateTime = DateTime(
        _selectedDate.year,
        _selectedDate.month,
        _selectedDate.day,
        pickedTime.hour,
        pickedTime.minute,
      );
      
      // 双重校验:确保选中时间不早于24小时后
      if (selectedDateTime.isBefore(earliestAllowed)) {
        customSnackBar("请选择当前时间24小时之后的时间", isError: true);
        return;
      }
      
      _selectedDate = selectedDateTime;
      update();
      _buildSchedule();
    }
  }

  Future<void> _buildSchedule() async {
    _schedule = DateConverter.dateToDateAndTime(_selectedDate);

    if(_postId!=null && _schedule.isNotEmpty){
      updatePostInformation(_postId!,_schedule);
    }
    update();
  }

  bool checkScheduleTime(){
    // 简化校验逻辑:直接判断选中时间是否在当前时间24小时之后
    return _selectedDate.isAfter(DateTime.now().add(const Duration(hours: 24)));
  }

  void updateSelectedDate(String? date){
    if(date!=null){
      _selectedDate = DateConverter.dateTimeStringToDate(date);
    }else{
      _selectedDate = DateTime.now().add(const Duration(hours: 24));
    }
  }

  Future<void> updatePostInformation(String postId,String scheduleTime) async {
    Response response = await scheduleRepo.changePostScheduleTime(postId,scheduleTime);

    if(response.statusCode==200 && response.body['response_code']=="default_update_200"){
      customSnackBar("service_schedule_updated_successfully".tr,isError: false);
    }
  }

  void setPostId(String postId){
    _postId = postId;
  }
}

关键修改说明

  • 初始值设定:将默认选中的日期直接设为当前时间24小时后,打开页面时就符合要求。
  • 日期选择器限制:通过firstDate参数禁止用户选择24小时内的日期(比如当天),从源头减少错误选择。
  • 时间选择器联动:如果用户选择的是最早允许的日期(比如明天),自动将初始时间设为对应时刻,避免用户选更早的时间;同时添加选中后的双重校验,确保时间合规。
  • 校验逻辑简化:checkScheduleTime方法直接判断选中时间是否在24小时后,逻辑更清晰。

内容的提问来源于stack exchange,提问作者Leafy Gardeners

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 22:31:03