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

套件詳情v2.0.0

system_date_time_format

一個用於從裝置系統設定中取得日期和時間格式模式的外掛程式。

AndroidiOSLinuxmacOSWebWindows
近 30 天下載
3.2萬
喜歡數
17
Pub 分數
160

安裝套件

flutter pub add system_date_time_format

套件介紹

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

一個 Flutter 外掛,可直接從裝置的系統設定中取得日期與時間格式範本。它透過使用原生作業系統設定,在跨平台環境中確保一致的格式化,非常適合需要地區語系感知日期與時間顯示的應用程式。此套件支援 Android、iOS、Linux、macOS、Web 與 Windows,提供精確的系統層級格式化,無需手動設定。它能自動適應使用者偏好,簡化本地化工作。

適用情境

  • 以使用者偏好的格式顯示日期
  • 建構本地化日曆應用
  • 建立區域特定的時間顯示
  • 確保與系統設定的一致性
  • 支援多語言應用

主要特色

  • 從設備系統設定中提取格式
  • 跨平台相容性
  • 自動地區設定適應
  • 無需手動設定格式
  • 使用原生作業系統格式化 API

適合對象

  • 面向全球受眾的應用程式
  • 本地化密集型應用
  • 日曆或排程工具
  • 面向使用者的日期/時間選擇器
  • 重視系統一致性的開發人員

使用建議

  • 某些系統需要平台特定的權限
  • 限於預定義的系統格式
  • 可能不支援自定格式覆蓋
  • 依賴於裝置的區域設定
  • 不適用於非系統格式需求

原始 README

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

A plugin for getting date & time format from device system settings.

Pub Package Build Status Code Coverage MIT License Android support ✓ iOS support ✓ macOS support ✓ windows support ✓ linux support ✓ web support ✓


Why system_date_time_format?

Flutter does not support retrieving date and time format patterns based on the user's system settings out of the box. However, you can use the system_date_time_format plugin to get date and time format patterns for consistent formatting in your Flutter app with ease:

final datePattern = SystemDateTimeFormat.datePattern;
print(datePattern); // e.g. "M/d/yy"
Examples
iOS (Region: United States 🇺🇸) Result
ios_setup ios_result
Android (Region: United Kingdom 🇬🇧) Result
android_setup android_result
macOS (Region: Poland 🇵🇱) Result
macos_setup macos_result
windows (Region: United States 🇺🇸) Result
windows_setup windows_result
linux (Region: United States 🇺🇸) Result
linux_setup linux_result
web (Region: Poland 🇵🇱)
web_result

Usage

Read a pattern directly from the current system settings. Medium and full date patterns return null on Windows and Linux.

import 'package:system_date_time_format/system_date_time_format.dart';

void main() {
  final date = SystemDateTimeFormat.datePattern;             // e.g. "M/d/yy"
  final mediumDate = SystemDateTimeFormat.mediumDatePattern; // e.g. "MMM d, y"
  final longDate = SystemDateTimeFormat.longDatePattern;     // e.g. "MMMM d, y"
  final fullDate = SystemDateTimeFormat.fullDatePattern;     // e.g. "EEEE, MMMM d, y"
  final time = SystemDateTimeFormat.timePattern;             // e.g. "HH:mm"
}
Migrating from 1.x

See the step-by-step migration guide.

Replace await SystemDateTimeFormat().getDatePattern() with SystemDateTimeFormat.datePattern. The other format methods follow the same naming convention. Replace calls to getAllPatterns() and SystemDateTimeFormat.of(context) with the individual getters you need. Remove the SDTFScope wrapper.

Each getter reads the current settings synchronously. A widget that reads patterns in build() picks up changes on its next rebuild.

Linux

Linux reads short date and time patterns from the active LC_TIME locale. For the long date, it uses the ICU date-only style for that locale. Install the ICU development package on the Linux build machine (for example, sudo apt install libicu-dev on Ubuntu or Debian). The application requires the matching ICU runtime libraries.

Web

Web getters use the browser’s locale and hour cycle. A getter returns null when the browser reports a field that has no matching pattern, such as a named year in the Chinese calendar. Load the JavaScript asset in index.html before Flutter starts.

From version 1.3.1 and above, the preferred way is to use the local asset:

<script src="./assets/packages/system_date_time_format/web/system_date_time_format.min.js"></script>

Alternatively, you can still use the CDN link:

<script src="https://cdn.jsdelivr.net/gh/Nikoro/system_date_time_format@main/web/system_date_time_format.min.js"></script>

index.html

<!DOCTYPE html>
<html>
<head>
    <!--...-->
    <script src="./assets/packages/system_date_time_format/web/system_date_time_format.min.js"></script>
</head>
<body>
<!--...-->
</body>
</html>
Testing

Mocktail stubs instance members. To test code that reads a static format getter, pass the consuming widget a value or a reader owned by your app. In example_with_tests, the production DeviceFormats.time getter calls SystemDateTimeFormat.timePattern. A widget test supplies a mock instance:

final formats = MockDeviceFormats();
when(() => formats.time).thenReturn('HH:mm');
await tester.pumpWidget(App(formats: formats));
expect(find.text('HH:mm'), findsOneWidget);

The example also tests how the app displays unavailable date styles. App adapter · Widget tests

所屬分類

暫無相關資料。

依賴

依賴清單 7 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單