Flutter实用工具集合插件flutter_easy_use_tools的使用
flutter_easy_use_tools 使用指南 #
flutter_easy_use_tools
是一个用于收集 Flutter 常用工具类和工具方法的插件。它提供了多种实用功能,帮助开发者快速实现常见需求。
Widgets
1. 内阴影 widget
CustomInnerShadow
是一个可以为控件添加内阴影效果的自定义 Widget
。
CustomInnerShadow(
shadows: [
BoxShadow(
color: Colors.white.withOpacity(0.5), // 白色半透明阴影
offset: Offset(-1, -1), // 阴影偏移
blurRadius: 1, // 模糊半径
spreadRadius: 0, // 扩散半径
),
BoxShadow(
color: Colors.grey[400]!, // 灰色阴影
offset: Offset(0, 1), // 阴影偏移
blurRadius: 3, // 模糊半径
spreadRadius: 0, // 扩散半径
),
],
child: Container(
decoration: BoxDecoration(
color: Colors.white, // 背景颜色
borderRadius: BorderRadius.all(Radius.circular(10)), // 圆角
),
margin: EdgeInsets.all(20),
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('内阴影'), // 显示文本
],
),
),
)
运行效果如下:
Utils
1. 数值格式化 NumUtil
NumUtil
提供了数值格式化的工具方法,例如保留小数位数。
print(NumUtil.formatNum(12.12345678, 2)); // 输出: 12.12
2. 字符串处理 StringUtil
StringUtil
提供了一些字符串处理方法,例如判断字符串是否为空。
bool isEmpty = StringUtil.isEmpty(''); // true
extension
1. 字符串扩展 StringExtension
StringExtension
提供了字符串的扩展方法,例如去除首尾空格。
String result = ' hello world '.trim(); // 输出: 'hello world'
manager
1. 任务队列管理 TaskQueueManager
TaskQueueManager
提供了任务队列管理的功能,支持批量添加任务并按顺序执行。
/// 添加多个下载任务
void testDownloadTask() async {
if (TaskQueueManager.instance('download').taskAllFinishedCallback == null) {
TaskQueueManager.instance('download').taskAllFinishedCallback = () {
print('任务全部完成 - queueName:download');
};
}
TaskQueueManager.instance('download').addTask(() {
return _download('download', 1);
});
TaskQueueManager.instance('download').addTask(() {
return _download('download', 2);
});
TaskQueueManager.instance('download').addTask(() {
return _download('download', 3);
});
}
/// 取消任务队列
void cancelDownloadTask() async {
TaskQueueManager.instance('download').cancelTask();
print('任务取消 - queueName:download');
}
/// 单个下载任务
Future _download(String queueName, int taskNo) async {
try {
String downloadUrl =
'https://img-dev.xinxigu.com.cn/s1/2020/7/23/ca226be9814208db75cf000eb43cf12f.mp4';
String downloadPath = await getDownloadPath(downloadUrl, taskNo);
Response<dynamic> response = await dio.download(downloadUrl, downloadPath);
if (response.statusCode == 200) {
print('任务完成 - 下载成功 queueName:$queueName, taskNo: $taskNo');
} else {
print('任务完成 - 下载失败 queueName:$queueName, taskNo: $taskNo');
}
return response;
} catch (e) {
print('任务完成 - 下载失败 queueName:$queueName, taskNo: $taskNo Error: $e');
return FlutterError('下载异常');
}
}
/// 获取下载路径
Future<String> getDownloadPath(String url, int taskNo) async {
String savedDirPath = '';
final tempDic = await getTemporaryDirectory();
Directory downloadDir = Directory('${tempDic.path}/download/');
bool isFold = await downloadDir.exists();
if (!isFold) {
await downloadDir.create();
}
savedDirPath = downloadDir.path;
String fileName = url.split('/').last.split('.').first;
String extension = url.split('.').last;
String videoPath = '$savedDirPath/$fileName-$taskNo.$extension';
return videoPath;
}
更多关于Flutter实用工具集合插件flutter_easy_use_tools的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter实用工具集合插件flutter_easy_use_tools的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_easy_use_tools
是一个为 Flutter 开发者提供的实用工具集合插件,旨在简化开发过程中的常见任务。它提供了一系列便捷的功能,如网络请求、数据存储、UI工具、设备信息获取等。以下是该插件的基本使用方法和一些常见功能的示例。
1. 安装插件
首先,在 pubspec.yaml
文件中添加依赖:
dependencies:
flutter_easy_use_tools: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
2. 导入插件
在需要使用插件的 Dart 文件中导入:
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
3. 使用插件功能
3.1 网络请求
flutter_easy_use_tools
提供了简化的网络请求功能,支持 GET、POST 等请求方式。
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
void fetchData() async {
var response = await EasyHttp.get('https://jsonplaceholder.typicode.com/posts/1');
if (response.statusCode == 200) {
print('Data: ${response.body}');
} else {
print('Failed to load data');
}
}
3.2 数据存储
插件提供了简单的本地数据存储功能,支持键值对存储。
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
void saveData() async {
await EasyStorage.setString('key', 'value');
String? value = await EasyStorage.getString('key');
print('Stored value: $value');
}
3.3 UI工具
插件提供了一些常用的 UI 工具,如显示 Toast 消息。
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
void showToast() {
EasyToast.show('Hello, Flutter!');
}
3.4 设备信息
插件可以获取设备的基本信息,如设备型号、操作系统版本等。
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
void getDeviceInfo() async {
String deviceModel = await EasyDeviceInfo.getDeviceModel();
String osVersion = await EasyDeviceInfo.getOsVersion();
print('Device Model: $deviceModel');
print('OS Version: $osVersion');
}
3.5 其他工具
插件还提供了其他一些实用工具,如日期格式化、字符串处理等。
import 'package:flutter_easy_use_tools/flutter_easy_use_tools.dart';
void formatDate() {
DateTime now = DateTime.now();
String formattedDate = EasyDate.format(now, 'yyyy-MM-dd HH:mm:ss');
print('Formatted Date: $formattedDate');
}
void stringUtils() {
String str = 'Hello, Flutter!';
bool isEmpty = EasyString.isEmpty(str);
print('Is string empty? $isEmpty');
}