Skip to content

Commit d5a55d7

Browse files
rashi-simformPRBaraiya
authored andcommitted
feat: ✨Add support for custom time provider in live time indicator
1 parent 246669a commit d5a55d7

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ WeekView(
241241
maxLines: 2,
242242
), // To set full day events header text config
243243
keepScrollOffset: true, // To maintain scroll offset when the page changes
244+
liveTimeIndicatorSettings: LiveTimeIndicatorSettings(
245+
color: Colors.red,
246+
showTime: true,
247+
// Support for different timezones - provide custom DateTime function
248+
currentTimeProvider: () {
249+
final utcNow = DateTime.now().toUtc();
250+
return utcNow.subtract(Duration(hours: 4));
251+
},
252+
),
244253
);
245254
```
246255

lib/src/components/_internal_components.dart

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
6464
@override
6565
void initState() {
6666
super.initState();
67-
67+
_currentTime = _updateCurrentTime();
6868
_timer = Timer.periodic(Duration(seconds: 1), _onTick);
6969
}
7070

@@ -74,24 +74,41 @@ class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
7474
super.dispose();
7575
}
7676

77+
/// Returns the current time to display in the live time indicator.
78+
///
79+
/// If [LiveTimeIndicatorSettings.currentTimeProvider] is provided,
80+
/// uses that function to get the current time.
81+
///
82+
/// Otherwise falls back to [DateTime.now] for the device's local time.
83+
DateTime _getCurrentDateTime() {
84+
final settings = widget.liveTimeIndicatorSettings;
85+
return settings.currentTimeProvider?.call() ?? DateTime.now();
86+
}
87+
88+
/// Update the current time based on timezone settings
89+
TimeOfDay _updateCurrentTime() {
90+
final dateTime = _getCurrentDateTime();
91+
return TimeOfDay(hour: dateTime.hour, minute: dateTime.minute);
92+
}
93+
7794
/// Creates an recursive call that runs every 1 seconds.
7895
/// This will rebuild TimeLineIndicator every second. This will allow us
7996
/// to indicate live time in Week and Day view.
8097
void _onTick(Timer? timer) {
81-
final time = TimeOfDay.now();
82-
if (time != _currentTime && mounted) {
83-
_currentTime = time;
84-
setState(() {});
85-
}
98+
final time = _currentTime;
99+
_currentTime = _updateCurrentTime();
100+
if (time == _currentTime || !mounted) return;
101+
setState(() {});
86102
}
87103

88104
@override
89105
Widget build(BuildContext context) {
90106
final currentHour = _currentTime.hourOfPeriod.appendLeadingZero();
91107
final currentMinute = _currentTime.minute.appendLeadingZero();
92108
final currentPeriod = _currentTime.period.name;
109+
final currentDateTime = _getCurrentDateTime();
93110
final timeString = widget.liveTimeIndicatorSettings.timeStringBuilder
94-
?.call(DateTime.now()) ??
111+
?.call(currentDateTime) ??
95112
'$currentHour:$currentMinute $currentPeriod';
96113

97114
/// remove startHour minute from [_currentTime.getTotalMinutes]
@@ -163,8 +180,6 @@ class TimeLine extends StatefulWidget {
163180
/// height of indicator and also allow to show time with custom format.
164181
final LiveTimeIndicatorSettings liveTimeIndicatorSettings;
165182

166-
static DateTime get _date => DateTime.now();
167-
168183
double get _halfHourHeight => hourHeight / 2;
169184

170185
/// This field will be used to set end hour for day and week view
@@ -197,6 +212,7 @@ class _TimeLineState extends State<TimeLine> {
197212
@override
198213
void initState() {
199214
super.initState();
215+
_currentTime = _updateCurrentTime();
200216
_timer = Timer.periodic(Duration(seconds: 1), _onTick);
201217
}
202218

@@ -206,16 +222,32 @@ class _TimeLineState extends State<TimeLine> {
206222
super.dispose();
207223
}
208224

225+
/// Returns the current time for the timeline, respecting timezone settings.
226+
DateTime _getCurrentDateTime() {
227+
final settings = widget.liveTimeIndicatorSettings;
228+
229+
if (settings.currentTimeProvider != null) {
230+
return settings.currentTimeProvider!();
231+
} else {
232+
return DateTime.now();
233+
}
234+
}
235+
236+
/// Update the current time based on timezone settings
237+
TimeOfDay _updateCurrentTime() {
238+
final dateTime = _getCurrentDateTime();
239+
return TimeOfDay(hour: dateTime.hour, minute: dateTime.minute);
240+
}
241+
209242
/// Creates an recursive call that runs every 1 seconds.
210243
/// This will rebuild TimeLine every second. This will allow us
211244
/// to show/hide time line when there is overlap with
212245
/// live time line indicator in Week and Day view.
213246
void _onTick(Timer? timer) {
214-
final time = TimeOfDay.now();
215-
if (time != _currentTime && mounted) {
216-
_currentTime = time;
217-
setState(() {});
218-
}
247+
final time = _currentTime;
248+
_currentTime = _updateCurrentTime();
249+
if (time == _currentTime || !mounted) return;
250+
setState(() {});
219251
}
220252

221253
@override
@@ -291,10 +323,12 @@ class _TimeLineState extends State<TimeLine> {
291323
required int hour,
292324
int minutes = 0,
293325
}) {
326+
final currentDateTime = _getCurrentDateTime();
327+
294328
final dateTime = DateTime(
295-
TimeLine._date.year,
296-
TimeLine._date.month,
297-
TimeLine._date.day,
329+
currentDateTime.year,
330+
currentDateTime.month,
331+
currentDateTime.day,
298332
hour,
299333
minutes,
300334
);

lib/src/modals.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ class LiveTimeIndicatorSettings {
6262
/// Width of time backgroud view.
6363
final double timeBackgroundViewWidth;
6464

65+
/// Function that provides the DateTime to be used for the live time indicator.
66+
///
67+
/// If not provided, [DateTime.now] will be used as the default behavior.
68+
///
69+
/// Example usage:
70+
/// ```dart
71+
/// // Show time for New York timezone (UTC-4)
72+
/// currentTimeProvider: () {
73+
/// final utcNow = DateTime.now().toUtc();
74+
/// return utcNow.subtract(Duration(hours: 4));
75+
/// }
76+
/// ```
77+
final DateTime Function()? currentTimeProvider;
78+
6579
/// Settings for live time line
6680
const LiveTimeIndicatorSettings({
6781
this.height = 1.0,
@@ -73,6 +87,7 @@ class LiveTimeIndicatorSettings {
7387
this.showTimeBackgroundView = false,
7488
this.bulletRadius = 5.0,
7589
this.timeBackgroundViewWidth = 60.0,
90+
this.currentTimeProvider,
7691
}) : assert(height >= 0, "Height must be greater than or equal to 0.");
7792

7893
factory LiveTimeIndicatorSettings.none() => LiveTimeIndicatorSettings(

0 commit comments

Comments
 (0)