搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。

所在榜单