Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-检查当前时间是否在给定的小时范围内_Flutter_Dart_Time - Fatal编程技术网

Flutter 颤振-检查当前时间是否在给定的小时范围内

Flutter 颤振-检查当前时间是否在给定的小时范围内,flutter,dart,time,Flutter,Dart,Time,我想检查当前时间是否介于我的营业时间和营业时间之间,因为我知道营业时间有时是凌晨2点,营业时间是凌晨3点,例如,我已经试着用逻辑处理这个问题两周了,但我不能完全理解它,这是我迄今为止最好的尝试: open = new DateTime(now.year, now.month, now.day, open.hour, open.minute); close = new DateTime(now.year, now.month, now.day, close.hour, close.minu

我想检查当前时间是否介于我的营业时间和营业时间之间,因为我知道营业时间有时是凌晨2点,营业时间是凌晨3点,例如,我已经试着用逻辑处理这个问题两周了,但我不能完全理解它,这是我迄今为止最好的尝试:

  open = new DateTime(now.year, now.month, now.day, open.hour, open.minute);
  close = new DateTime(now.year, now.month, now.day, close.hour, close.minute);
  midnight = new DateTime(now.year, now.month, now.day, midnight.hour, midnight.minute);


  if(close.hour > midnight.hour && close.hour < open.hour){

   
    if(now.hour < midnight.hour){
      DateTime theClose = new DateTime(now.year, now.month, now.day + 1, close.hour, close.minute);

    

      if(now.isBefore(theClose) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
    
        _showToast("this branch is closed right now");
      }

    }else{
      open = new DateTime(now.year, now.month, now.day - 1, open.hour, open.minute);

      if(now.isBefore(close) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
  
        _showToast("this branch is closed right now");
      }

    }


  }else{


    if(now.isBefore(close) && now.isAfter(open)){
      sendIt(context, notes);

    }else{
 
      _showToast("this branch is closed right now");
    }

  }
open=newdatetime(now.year,now.month,now.day,open.hour,open.minute);
close=新日期时间(now.year,now.month,now.day,close.hour,close.minute);
午夜=新日期时间(now.year,now.month,now.day,午夜.hour,午夜.min);
如果(close.hour>午夜时间&close.hour
正如您所注意到的,在我们的例子中使用
日期时间并不是最好的解决方案,因为它依赖于月/年/日。
相反,我们可以使用
TimeOfDay
类,该类不依赖于特定的一天,而只依赖于时间:

List<TimeOfDay> openingTimeRange = [TimeOfDay(hour: 2, minute: 30), TimeOfDay(hour: 15, minute: 45)]; // as an example
bool isOpen(List<TimeOfDay> openingTimeRange) {
   TimeOfDay now = TimeOfDay.now();
   return now.hour >= openingTimeRange[0].hour
      && now.minute >= openingTimeRange[0].minute
      && now.hour <= openingTimeRange[1].hour
      && now.minute <= openingTimeRange[1].minute;
}
List openingTimeRange=[TimeOfDay(小时:2,分钟:30),TimeOfDay(小时:15,分钟:45)];//例如
布尔等参线(列表打开时间范围){
TimeOfDay now=TimeOfDay.now();
立即返回。小时>=打开时间范围[0]。小时
&&now.minute>=打开时间范围[0]。分钟
&&现在,一小时