Flutter实用工具插件essential_utils的使用

Flutter实用工具插件essential_utils的使用

概述

Essential Utils 是一套旨在提升 Flutter 项目开发体验的强大工具集。通过 DateUtilsStringUtilsColorUtils,该包为开发者提供了处理日期、字符串和颜色所需的工具,使这些任务更加高效和简便。通过减少样板代码、确保 UI 一致性并提高可访问性,您可以轻松实现目标。

特点

  • DateUtils:用于格式化日期和计算相对时间的功能。
  • StringUtils:包括首字母大写和回文检查在内的增强字符串操作能力。
  • ColorUtils:生成 Material 颜色样本并根据背景亮度确定最佳文本颜色的工具。

开始使用

安装

要在您的 Flutter 项目中添加 FlutterToolkit,请将其包含在 pubspec.yaml 文件中:

dependencies:
  flutter_toolkit: ^1.0.0

然后运行以下命令以安装包:

flutter pub get

导入

要使用项目中的工具,请简单导入包:

import 'package:flutter_toolkit/flutter_toolkit.dart';

使用

DateUtils

// 获取当前日期时间
DateTime now = DateTime.now();

// 格式化日期为美国格式
String formattedDate = DateUtils.formatAsUsDate(now);

// 计算自昨天以来的时间
String timeAgo = DateUtils.timeAgoSinceDate(now.subtract(Duration(days: 1)));

StringUtils

// 将字符串中的每个单词首字母大写
String capitalized = StringUtils.capitalizeWords("hello world");

// 检查字符串是否为回文
bool isPalindrome = StringUtils.isPalindrome("racecar");

ColorUtils

// 定义一个主要颜色
Color primaryColor = Color(0xFF6A1B9A);

// 创建 Material 颜色样本
MaterialColor materialColor = ColorUtils.createMaterialColor(primaryColor);

// 根据背景颜色获取最佳字体颜色
Color fontColor = ColorUtils.getBestFontColor(primaryColor);

更多关于Flutter实用工具插件essential_utils的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter实用工具插件essential_utils的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


essential_utils 是一个 Flutter 实用工具插件,提供了许多常用的工具和扩展方法,可以帮助开发者更高效地编写代码。它包含了许多功能,如字符串处理、日期时间操作、文件操作、网络请求、加密解密、正则表达式等。

安装

首先,你需要在 pubspec.yaml 文件中添加 essential_utils 依赖:

dependencies:
  essential_utils: ^1.0.0

然后运行 flutter pub get 来安装依赖。

使用示例

以下是一些 essential_utils 插件的常用功能示例:

1. 字符串处理

import 'package:essential_utils/essential_utils.dart';

void main() {
  String text = "Hello, World!";

  // 判断字符串是否为空
  bool isEmpty = StringUtils.isEmpty(text);
  print('Is empty: $isEmpty'); // false

  // 反转字符串
  String reversed = StringUtils.reverse(text);
  print('Reversed: $reversed'); // !dlroW ,olleH

  // 将字符串转换为驼峰命名
  String camelCase = StringUtils.camelCase("hello_world");
  print('Camel case: $camelCase'); // helloWorld
}

2. 日期时间操作

import 'package:essential_utils/essential_utils.dart';

void main() {
  DateTime now = DateTime.now();

  // 格式化日期时间
  String formatted = DateUtils.formatDate(now, 'yyyy-MM-dd HH:mm:ss');
  print('Formatted date: $formatted'); // 2023-10-05 14:30:45

  // 添加天数
  DateTime futureDate = DateUtils.addDays(now, 7);
  print('Future date: $futureDate'); // 2023-10-12 14:30:45

  // 判断是否为闰年
  bool isLeapYear = DateUtils.isLeapYear(2024);
  print('Is leap year: $isLeapYear'); // true
}

3. 文件操作

import 'package:essential_utils/essential_utils.dart';

void main() async {
  String path = "example.txt";

  // 写入文件
  await FileUtils.writeFile(path, "Hello, World!");

  // 读取文件
  String content = await FileUtils.readFile(path);
  print('File content: $content'); // Hello, World!

  // 删除文件
  await FileUtils.deleteFile(path);
}

4. 网络请求

import 'package:essential_utils/essential_utils.dart';

void main() async {
  String url = "https://jsonplaceholder.typicode.com/posts";

  // 发起 GET 请求
  var response = await HttpUtils.get(url);
  print('Response: $response');

  // 发起 POST 请求
  var postResponse = await HttpUtils.post(url, body: {
    "title": "foo",
    "body": "bar",
    "userId": 1,
  });
  print('Post response: $postResponse');
}

5. 加密解密

import 'package:essential_utils/essential_utils.dart';

void main() {
  String text = "Hello, World!";

  // MD5 加密
  String md5 = CryptoUtils.md5(text);
  print('MD5: $md5');

  // SHA256 加密
  String sha256 = CryptoUtils.sha256(text);
  print('SHA256: $sha256');

  // Base64 编码
  String base64 = CryptoUtils.base64Encode(text);
  print('Base64: $base64');

  // Base64 解码
  String decoded = CryptoUtils.base64Decode(base64);
  print('Decoded: $decoded');
}

6. 正则表达式

import 'package:essential_utils/essential_utils.dart';

void main() {
  String email = "test@example.com";

  // 验证电子邮件格式
  bool isValidEmail = RegexUtils.isEmail(email);
  print('Is valid email: $isValidEmail'); // true

  // 验证手机号码格式
  bool isValidPhone = RegexUtils.isPhoneNumber("1234567890");
  print('Is valid phone: $isValidPhone'); // false (假设格式不正确)
}
回到顶部