Flutter工作工具插件flutter_work_utils的使用

Flutter工作工具插件flutter_work_utils的使用

Flutter Work Utils 是一个包含多个实用工具的包,可以帮助解决一些常见问题,避免重复造轮子。

Flutter Work Utils 包含这些工具包

color_parse

color_parse 提供了一种将十六进制颜色字符串解析为 Color 对象的方法。

示例代码:

String stringColor = "ff000000";
Color? res = colorFromString(stringColor);

dates

dates 扩展了 DateTime 类,提供了多种格式化的日期字符串方法。

示例代码:

DateTime date = DateTime.now();
print(date.dateString); // 将返回当前日期,如 26-09-2022
print(date.hourString); // 将返回当前小时,如 22:42
print(date.monthAbbr); // 将返回月份缩写,如 SEPT

它还提供了一个方法来验证并解析毫秒时间戳到 DateTime? 类。

digester

digester 扩展了 String 类,通过 crypto 包将字符串转换为 SHA256 摘要。

示例代码:

String password = "hola12";
String digest = password.digest; // 将返回 "hola12" 的 SHA256 摘要

font_color_switcher

font_color_switcher 包含一个方法,可以返回与背景色对比度高的文本颜色。

示例代码:

Color background = Colors.black;
Color textColor = getColorContrast(background); // 将返回白色

iterable

iterable 扩展了 Iterable 类,提供了多个有用的方法。

示例代码:

List<String> names = ["John", "Charles", "Lisa", "Dog"];
List<String> namesWithO = names.containsLambda((name) => name.contains("o")); // 将返回包含 "o" 的名字列表

List<String> allNamesContainO = names.allLambda((name) => name.contains("o")); // 将返回所有名字是否都包含 "o"

bool containsAllNames = names.containsAll(["John", "Miguel"]); // 将返回 false,因为 "Miguel" 不在列表中

bool containsAnyNames = names.containsAny(["John", "Miguel"]); // 将返回 true,因为 "John" 在列表中

logger

logger 包提供了一种结构化日志记录的方式。

示例代码:

printWarning("This is a warning message");
printError("This is an error message");
printSuccess("This is a success message");
printLog("This is a log message");

注意:Xcode 不支持彩色文本。

map

map 扩展了 Map 类,提供了一些有用的实用方法。

示例代码:

Map<String, dynamic> myMap = {"Hello": "There", "General": "Kenobi"};
print(myMap.pprint); // 将打印出格式化的键值对
/*
    Hello: There
    General: Kenobi
*/

nav_route

nav_route 包含一个方法,可以根据字符串路由和查询参数生成 URI 路由。

示例代码:

String route = generateNavRoute("/home", queryParams: {"foo": "bar"});
print(route); // 将打印 "/home?foo=bar"

network_image

network_image 包含一个方法,可以从 URL 加载并获取图像的字节数组。

示例代码:

Uint8List? bytes = getBytesFromNetworkImageUrl("https://image.com/image.png");

query_parameters

query_parameters 扩展了 String 类,可以根据导航路由生成 RoutingData 对象。

示例代码:

String route = "/home?foo=bar"; // 参见 nav_route 包
RoutingData data = route.getRoutingData;
print(data.contains("foo")); // 将打印 true

router

router 包含一个类,用于管理带有 Web 策略(Beta 版)的路由。

示例代码:

// 使用 get_it 包
locator<NavigationService>().navigateTo("/home", queryParams: {"foo": "bar"});
locator<NavigationService>().goBack();

String? fooValue = getQueryParam(context, "foo"); // 将保存 "bar"

scroll_configuration

scroll_configuration 包含一个滚动行为,非常适用于 Web 开发,因为它移除了默认滚动效果。

示例代码:

return MaterialApp(
  title: 'Test',
  debugShowCheckedModeBanner: runtime == "Test",
  scaffoldMessengerKey: _scaffoldKey,
  theme: lightTheme,
  darkTheme: darkTheme,
  themeMode: ui.themeMode,
  initialRoute: SplashScreen.routeName,
  navigatorKey: locator<NavigationService>().navigatorKey, // 使用 router 包
  builder: (ctx, child) {
    return ScrollConfiguration(
      behavior: ScrollClean().copyWith(scrollbars: false), 
      child: child ?? Container()
    );
  },
  localizationsDelegates: const [
    S.delegate,
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate
  ],
  supportedLocales: S.delegate.supportedLocales,
  locale: Locale(ui.locale),
  onGenerateRoute: (settings) => RutaPercepthor.generateRoute(settings, context),
);

string_utils

string_utils 扩展了 String 类,包含了一些有用的字符串格式化方法。

示例代码:

String name = "John";
print("${name.pertenence} house"); // 将打印 "John's house"

String formattedName = "María Juanita Lisandra".formattedSearchText; // 将打印 "mariajuanitalisandra"

unfocus

unfocus 包含一个方法,可以在任何上下文中取消键盘焦点。

示例代码:

ElevatedButton(
  child: Text("Unfocus"),
  onPressed: () => unfocus(context), // 取消键盘焦点
),

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

1 回复

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


flutter_work_utils 是一个 Flutter 插件,旨在提供一些实用的工具和功能,帮助开发者更高效地进行 Flutter 应用开发。这个插件通常包含了一些常用的工具方法,例如日期处理、字符串处理、文件操作、网络请求等。

由于 flutter_work_utils 并不是 Flutter 官方提供的插件,因此它的具体功能和用法可能会根据插件的作者和版本有所不同。以下是一个假设的 flutter_work_utils 插件的使用示例,假设它包含了一些常见的工具方法。

安装插件

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

dependencies:
  flutter:
    sdk: flutter
  flutter_work_utils: ^1.0.0  # 请根据实际情况填写版本号

然后运行 flutter pub get 来安装插件。

使用插件

假设 flutter_work_utils 插件提供了以下功能:

  1. 日期格式化
  2. 字符串处理
  3. 文件操作
  4. 网络请求

以下是一些使用示例:

1. 日期格式化

import 'package:flutter_work_utils/flutter_work_utils.dart';

void main() {
  DateTime now = DateTime.now();
  String formattedDate = WorkUtils.formatDate(now, 'yyyy-MM-dd HH:mm:ss');
  print(formattedDate);  // 输出: 2023-10-05 14:30:45
}

2. 字符串处理

import 'package:flutter_work_utils/flutter_work_utils.dart';

void main() {
  String text = "Hello, World!";
  bool contains = WorkUtils.containsIgnoreCase(text, 'world');
  print(contains);  // 输出: true
}

3. 文件操作

import 'package:flutter_work_utils/flutter_work_utils.dart';

void main() async {
  String filePath = 'example.txt';
  await WorkUtils.writeFile(filePath, 'Hello, Flutter!');
  String content = await WorkUtils.readFile(filePath);
  print(content);  // 输出: Hello, Flutter!
}

4. 网络请求

import 'package:flutter_work_utils/flutter_work_utils.dart';

void main() async {
  String url = 'https://jsonplaceholder.typicode.com/posts/1';
  var response = await WorkUtils.getRequest(url);
  print(response);  // 输出: 获取到的JSON数据
}
回到顶部