A. Time Comparison
As a human, we always think it will be more straight-forward
to compare hour value of 2 time windows.
But, actually, for dart
, it is faster to compare two time windows along with its minutes and seconds.
To get this privilege, we need to convert all timestamp format, which usually comes with String
type.
We will convert it to DateTime by using this syntax
:
var someTimestamp = "2020-06-19 09:00:00";
var someDatetimeObject = DateTime.parse(someTimestamp); // 9 a.m.
or, if you are new to dart
, you can make it more intuitive by pairing their Type
.
In this example below, we will replace var
which shows dynamic variable, to more static type :
String someTimestamp = "2020-06-19 09:00:00";
DateTime someDatetimeObject = DateTime.parse(someTimestamp); // 9 a.m.
In short, I think you will have this part of code :
A.1 Final Code
bool checkIfIsOpen() {
var horaireOfTodayOpen = DateTime.parse("2020-06-19 09:00:00"); // 9 a.m.
var horaireOfTodayClose = DateTime.parse("2020-06-19 17:00:00"); // 5 p.m.
var now = new DateTime.now();
if (now.isBefore(horaireOfTodayClose) && now.isAfter(horaireOfTodayOpen)) {
return true;
} else {
return false;
}
}
A.2 Detail Explanation Steps
1. First you need to have two time window
For example, some organization opens at 9 a.m. and closes at 5 p.m
var horaireOfTodayOpen = DateTime.parse("2020-06-19 09:00:00"); // 9 a.m.
var horaireOfTodayClose = DateTime.parse("2020-06-19 17:00:00"); // 5 p.m.
2. Then you need to have current time
Using standard syntax :
var now = new DateTime.now();
3. Lastly, compare it using instance method
You can read it in this official docs page
if (now.isBefore(horaireOfTodayClose) && now.isAfter(horaireOfTodayOpen)) {
return true;
} else {
return false;
}
}
B. Periodically Refresh Page / Widget
We need to trigger setState, let say every 5 seconds.
1. Make Timer Property and init the with it
import 'dart:async'; // add this
class MapMarkerPage extends State<MyHomePage> {
Timer timer; // add this
@override
void initState() {
super.initState();
const oneSec = const Duration(seconds: 5); // add this
timer = new Timer.periodic(oneSec, (Timer t) => refreshMarkers()); // add this
}
...
}
2. Loop Through Possible Markers
Please note, in this section, I will post PSEUDUCODE
related to markers, because I don't know specifically you solve this.
After we have refreshMarkers
, in first section, we now know, that it will be executed every 5 seconds.
Now, we need to define its business-logic :
import 'dart:async';
class MapMarkerPage extends State<MyHomePage> {
List<dynamic> markers = []; // add this
bool checkIfIsOpen(horaire) {
var horaireOfTodayOpen = DateTime.parse("2020-06-19 09:00:00"); // 9 a.m.
var horaireOfTodayClose = DateTime.parse("2020-06-19 17:00:00"); // 5 p.m.
var now = new DateTime.now();
if (now.isBefore(horaireOfTodayClose) && now.isAfter(horaireOfTodayOpen)) {
return true;
} else {
return false;
}
}
void refreshMarkers() {
List<Widget> tempMarkers = [];
// Loop through possible organization
// Pseudocode
for (horaire in horaires){
if checkIsOpen(horaire){
tempMarkers.add(Marker(
markerId: MarkerId(
p.geoPoint.longitude.toString()),
position: point,
onTap: ,
icon: status == Status.open
? BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen)
: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueRed)));
}
}
markers = tempMarkers; // Renew markers
setState(() {}); // Force Widget to Update
}
}
I hope you will understand the main idea, the final code may not 100%
applicable to your app, but I hope it will be clearer now