A Flutter package that provides a widget to listen to the system's datetime and trigger a rebuild based on the specified time unit
Support the project by giving the repo a ⭐ and showing some ❤️!
Usage
Import the package in your Dart code:
import'package:datetime_loop/datetime_loop.dart';
Using DateTimeLoopBuilder
Use the DateTimeLoopBuilder widget to rebuild UI elements based on system time updates. You can provide a timeUnit for simple setups or a custom DateTimeLoopController for advanced control (e.g., pause/resume):
The DateTimeLoopController provides a stream of datetime updates and supports advanced features like pausing and resuming updates, useful for optimizing resource usage (e.g., when the app is backgrounded):
final controller = DateTimeLoopController(timeUnit: TimeUnit.minutes);
controller.dateTimeStream.listen((dateTime) {
print('Current time: $dateTime');
});
// Pause updates to save resources
controller.pause();
// Resume updates with an immediate trigger
controller.resume(triggerImmediate: true);
// Use the controller with DateTimeLoopBuilderDateTimeLoopBuilder(
controller: controller,
builder: (context, dateTime, child) {
returnText('Time: ${dateTime.toString().split('.').first}');
},
);
// Dispose of the controller when done
controller.dispose();
Testing with getNow
Both DateTimeLoopBuilder and DateTimeLoopController support a getNow parameter that allows you to mock the system time. This is particularly useful for testing and simulating different time scenarios: