skip to Main Content

I use an app for booking and here is booking date and time picker code.

Problem is when customer selects date and time it picks date from the same day, I want customers to choose date only after 24 hours. Could anyone help modify this flutter dart code?

Thanks.

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: AppConstants.scheduleTime));
  DateTime get selectedData => _selectedDate;


  ///Selected time of day
  // TimeOfDay _selectedTimeOfDay = TimeOfDay(hour: DateTime.now().hour + AppConstants.SCHEDULE_TIME, minute: DateTime.now().minute);
  // TimeOfDay get selectedTimeOfDay => _selectedTimeOfDay;

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

  String? _postId;

  // @override
  // void onInit() {
  //   super.onInit();
  //   // initializeTime();
  //  // _buildSchedule();
  // }



  Future<void> selectDate() async {
    final DateTime? picked = await showDatePicker(
        context: Get.context!,
        initialDate: DateTime.now(),
        firstDate: DateTime.now().subtract(const Duration(days: 0)),
        lastDate: DateTime(2101));
    if (picked != null) {
      _selectedDate = picked;
      update();
      selectTimeOfDay();
    }
  }

  Future<void> selectTimeOfDay() async {
    final TimeOfDay? pickedTime = await showTimePicker(
        context: Get.context!,
        initialTime: TimeOfDay(hour: DateTime.now().hour + AppConstants.scheduleTime, minute: DateTime.now().minute));

    if (pickedTime != null) {
      _selectedDate = DateTime(_selectedDate.year, _selectedDate.month, _selectedDate.day, pickedTime.hour, pickedTime.minute);
      update();

     _buildSchedule();
    }
  }


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

    if(_postId!=null && _schedule.isNotEmpty){
      updatePostInformation(_postId!,_schedule);
    }
    //_schedule = "${DateConverter.dateTimeStringToDateOnly(_selectedDate.toString())} ${_selectedDate.hour.toString().padLeft(2,'0')}:${_selectedDate.minute.toString().padLeft(2,'0')}:00";
    update();
  }

  bool checkScheduleTime(){
    return  _selectedDate.difference(DateTime.now()) > const Duration(hours: AppConstants.scheduleTime, minutes: -15);
  }

  void updateSelectedDate(String? date){
    if(date!=null){
      _selectedDate = DateConverter.dateTimeStringToDate(date);
    }else{
      _selectedDate = DateTime.now().add(const Duration(hours: AppConstants.scheduleTime));
    }
  }
  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;
  }
}

I’m newbie. so looking forward if someone can solve this for me.

2

Answers


  1. Consider adjusting the ‘firstDate’ variable. Since you’ve currently set ‘firstDate’ to begin today, altering it to 24hours later should resolve the issue.

    firstDate: DateTime.now().subtract(const Duration(days: 0)),
    

    ->

    firstDate: DateTime.now().subtract(const Duration(days: 1)),
    
    Login or Signup to reply.
  2. final DateTime minDate = DateTime.now().add(const 
    Duration(days: 1));
    final DateTime? picked = await showDatePicker(
        context: Get.context!,
        initialDate: minDate,
        firstDate: minDate,
        lastDate: DateTime(2101));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search