搜尋套件、儲存庫與 APP 分類

套件詳情v1.6.0

datetime_loop

一個 Flutter 套件,提供一個用於監聽系統日期時間並根據指定的時間單位觸發重建的 widget。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
274
喜歡數
11
Pub 分數
160

安裝套件

flutter pub add datetime_loop

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

datetime_loop 包是 Flutter 的一個實用工具,提供了一個用於監控系統目前日期和時間的 widget,可根據可配置的時間間隔(如秒、分鐘或小時)觸發重建。它簡化了需要動態時間內容的 UI 中的即時更新,例如時鐘、倒計時或即時資料顯示。該 widget 效率地監聽系統時間變更,避免不必要的開銷,確保跨平台的順暢性能。適用於需要與時間進展相關的週期性刷新之應用程式。

適用情境

  • 實時鐘顯示
  • 倒數計時
  • 即時資料更新
  • 基於時間的UI刷新
  • 動態內容排程

主要特色

  • 於指定的時間間隔內重新建構
  • 高效系統時間監聽
  • 跨平台支援
  • 可設定的時間單位
  • 最小性能影響

適合對象

  • 具有時間依賴性使用者介面的應用程式
  • 需要輕量級時間監聽器的開發人員
  • 需要頻繁更新UI的項目
  • 時鐘或計時器應用程式
  • 即時儀表板

使用建議

  • 如果間隔太短,可能導致過度重建
  • 需要仔細處理複雜組件中的狀態
  • 不適合長時間運行的背景任務
  • 最佳搭配不可變或輕量級 UI 元件使用
  • 僅限UI層級的時間監控

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Clock Example

Buy Me A Coffee
Pub BSD 3-Clause License Pub
Pub likes Download Pub points

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):

DateTimeLoopBuilder(
  timeUnit: TimeUnit.seconds,
  builder: (context, dateTime, child) {
    return Column(
      children: [
        Container(
          width: 200,
          height: 200,
          color: Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0),
        ),
        Text('$dateTime'),
      ],
    );
  }
)
Using DateTimeLoopController

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 DateTimeLoopBuilder
DateTimeLoopBuilder(
  controller: controller,
  builder: (context, dateTime, child) {
  return Text('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:

// Example: Testing with a mocked time
DateTime customTime = DateTime(2025, 10, 31, 12, 0, 0);

DateTimeLoopBuilder(
  timeUnit: TimeUnit.seconds,
  getNow: () => customTime,
  builder: (context, dateTime, child) {
    return Text('Mocked time: $dateTime');
  },
)

// Example: Using getNow with DateTimeLoopController
final controller = DateTimeLoopController(
  timeUnit: TimeUnit.minutes,
  getNow: () => customTime,
);

This feature enables:

  • Unit Testing: Write deterministic tests by controlling the datetime value
  • Time Simulation: Simulate different time scenarios without waiting for real time to pass
  • Debugging: Test edge cases and time-dependent behavior

You can check more examples of using this widget here

Issues and Feedback

Please file any issues or feedback here.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。