Flutter核心功能扩展插件vvs_core的使用
Flutter核心功能扩展插件vvs_core的使用
安装
1. 依赖
在您的包的pubspec.yaml
文件中添加以下内容:
dependencies:
vvs_core: ^version
2. 安装
您可以从命令行安装包:
$ flutter pub get
3. 导入
现在可以在您的 Dart 代码中使用:
import 'package:vvs_core/vvs_core.dart';
使用的依赖项
此项目使用的依赖项包括:
- shared_preferences: 在设备的本地存储中存储数据。
- dio: 执行 HTTP 请求。
- uuid: 生成 GUID。
- equatable: 类之间的相等性检查。
- animate_do: 简化动画。
- google_fonts: 谷歌字体。
- intl: 国际化。
- flutter_hooks: 管理小部件的生命周期。
- package_info_plus: 获取应用程序的原生信息。
- connectivity_plus: 检查互联网连接。
包的实用工具
包中所有存在的功能如下:
Utils:
- copyToClipboard: 用于复制文本的实用工具。
- ExcludeGlowScrollBehavior: 用于移除列表滚动时的发光效果的小部件。
- generateGuid: 生成 GUID 的实用工具。
- getClipboardData: 获取剪贴板中的文本。
- hideKeyboard: 用于关闭键盘的实用工具。
- keyboardIsOpened: 用于检查键盘是否打开的实用工具。
- Resulter: 用于将成功和错误情况分离到一个类中的类。
- formatCurrency: 用于将数字格式化为货币格式(例如 “R$ 9.999,99”)的实用工具。
- formatDate: 用于格式化日期的实用工具。
- formatDecimal: 用于将数字格式化为千位分隔符(例如 “1.000.000,00”)的实用工具。
- formatPercentage: 用于将数字格式化为百分比(例如 “10%”)的实用工具。
- formatTelefone: 用于将字符串格式化为电话号码格式(例如 “(xx) xxxxx-xxxx”)的实用工具。
- CepHelper: 用于验证、格式化和删除 CEP 字符的助手。
- CnpjHelper: 用于验证、格式化、生成和删除 CNPJ 字符的助手。
- CpfCnpjHelper: 用于验证、格式化和删除 CPF 或 CNPJ 字符的助手。
- TelefoneHelper: 用于格式化和删除电话字符的助手。
- CentavosInputFormatter: 用于在文本字段中格式化带有分位数的货币输入格式器。
- CepInputFormatter: 用于在文本字段中格式化 CEP 输入格式器。
- CnpjInputFormatter: 用于在文本字段中格式化 CNPJ 输入格式器。
- CpfInputFormatter: 用于在文本字段中格式化 CPF 输入格式器。
- CpfOuCnpjFormatter: 用于在文本字段中格式化 CPF 或 CNPJ 输入格式器。
- DataInputFormatter: 用于在文本字段中格式化日期输入格式器。
- PlacaVeiculoInputFormatter: 用于在文本字段中格式化车辆牌照输入格式器。
- RealInputFormatter: 用于在文本字段中格式化货币输入格式器。
- TelefoneInputFormatter: 用于在文本字段中格式化电话输入格式器。
Shared:
- BaseHookView: 使用
flutter_hooks
包创建视图(页面)和视图模型的类。 - BaseView: 创建视图和视图模型的类。
- BaseViewModel: 创建视图模型的类。
Services:
- FlutterDialogService: 用于显示对话框的服务。
- 此服务不使用任何外部包来显示对话框,要使用它,请参阅
flutter_dialog_service.dart
文件中的步骤。
- 此服务不使用任何外部包来显示对话框,要使用它,请参阅
- VvsSistemasDioAdapter: 用于对符合 VVS Systems API 响应标准的 API 进行 HTTP 客户端请求的适配器。
- InternetConnectivityPlusService: 用于检查互联网连接的服务。
- 此服务使用
connectivity_plus
包。
- 此服务使用
- SharedPreferencesLocalStorageService: 用于读取、保存和删除设备本地存储的数据的服务。
扩展:
-
double_extension:
- arredondar: 将数字四舍五入到指定的小数位。
- casoForZero: 如果当前值为零,则返回一个给定值 X。
- getValueOrDefault: 如果当前值为空,则返回一个给定值 X。
-
list_extensions:
- sumBy: 对列表中的某个属性求和。
- separarComVirgulas: 将值用逗号分隔(例如,[‘VVS Sistemas’, ‘Google’] = “VVS Sistemas, Google”)。
-
string_extensions:
- whereNullOrEmpty: 返回一个值,如果当前值为空或 null。
- toDateTime: 从字符串返回 DateTime(字符串格式必须为 “日/月/年”)。
- capitalize: 将字符串首字母大写(例如,“texto exemplo PARA CAPITALIZAÇÃO” = “Texto exemplo para capitalização”)。
- removeWhitespace: 删除字符串中的所有空格。
- contemNumero: 检查字符串是否包含数字。
- contemCaracterEspecial: 检查字符串是否包含特殊字符。
- isInstagramUser: 检查字符串是否以 “@” 开头。
组件:
- CircularLoader
- TooltipSpan: 具有提示的 Text.rich。
- VvsButton: 应用程序的标准按钮。
- VvsIconButton: 带有图标的应用程序标准按钮。
示例代码
为了更好地理解如何使用 vvs_core
插件,下面是一个简单的示例代码,展示了如何使用一些主要的功能。
import 'package:flutter/material.dart';
import 'package:vvs_core/vvs_core.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _textEditingController = TextEditingController();
void _showDialog() {
// 使用 FlutterDialogService 显示对话框
FlutterDialogService.showDialog(
context: context,
title: 'Hello',
content: 'This is a simple dialog.',
);
}
void _copyToClipboard() {
// 使用 copyToClipboard 实用工具复制文本
VvsCore.copyToClipboard('Copied text');
}
void _formatCurrency() {
// 使用 formatCurrency 实用工具格式化货币
String formattedCurrency = VvsCore.formatCurrency(1234567.89);
print(formattedCurrency); // 输出: R$ 1.234.567,89
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('VVS Core Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
ElevatedButton(
onPressed: _showDialog,
child: Text('Show Dialog'),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _copyToClipboard,
child: Text('Copy to Clipboard'),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: _formatCurrency,
child: Text('Format Currency'),
),
TextField(
controller: _textEditingController,
inputFormatters: [
// 使用 RealInputFormatter 格式化货币输入
VvsCore.RealInputFormatter(),
],
decoration: InputDecoration(
labelText: 'Enter currency',
),
),
],
),
),
);
}
}
更多关于Flutter核心功能扩展插件vvs_core的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter核心功能扩展插件vvs_core的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
vvs_core
是一个 Flutter 插件,通常用于扩展 Flutter 的核心功能,提供一些常用的工具类和功能模块,方便开发者快速构建应用。由于 vvs_core
是一个特定于某些项目或团队的插件,这里我将提供一个通用的使用指南,假设它包含了一些常见的功能模块,如网络请求、本地存储、状态管理、工具类等。
1. 安装插件
首先,你需要在 pubspec.yaml
文件中添加 vvs_core
插件的依赖:
dependencies:
flutter:
sdk: flutter
vvs_core: ^1.0.0 # 请根据实际版本号填写
然后运行 flutter pub get
来安装插件。
2. 基本使用
2.1 网络请求
假设 vvs_core
提供了一个网络请求的工具类 HttpUtil
,你可以这样使用它:
import 'package:vvs_core/vvs_core.dart';
void fetchData() async {
var response = await HttpUtil.get('https://jsonplaceholder.typicode.com/posts');
if (response.statusCode == 200) {
print('Data fetched successfully: ${response.body}');
} else {
print('Failed to fetch data');
}
}
2.2 本地存储
如果 vvs_core
提供了本地存储的工具类 LocalStorage
,你可以这样使用:
import 'package:vvs_core/vvs_core.dart';
void saveData() async {
await LocalStorage.setString('key', 'value');
}
void getData() async {
String value = await LocalStorage.getString('key');
print('Value: $value');
}
2.3 状态管理
如果 vvs_core
提供了自己的状态管理工具,比如 BaseViewModel
,你可以这样使用:
import 'package:vvs_core/vvs_core.dart';
class MyViewModel extends BaseViewModel {
String _data = '';
String get data => _data;
Future<void> loadData() async {
setBusy(true);
_data = await someAsyncFunction();
setBusy(false);
notifyListeners();
}
}
2.4 工具类
如果 vvs_core
提供了一些常用的工具类,比如 DateUtil
,你可以这样使用:
import 'package:vvs_core/vvs_core.dart';
void formatDate() {
String formattedDate = DateUtil.formatDate(DateTime.now(), 'yyyy-MM-dd');
print('Formatted Date: $formattedDate');
}
3. 进阶使用
3.1 配置
如果 vvs_core
需要一些初始配置,比如设置 API 的基础 URL,你可以在应用的启动时进行配置:
import 'package:vvs_core/vvs_core.dart';
void main() {
VvsCore.configure(
apiBaseUrl: 'https://api.example.com',
enableLogging: true,
);
runApp(MyApp());
}
3.2 自定义扩展
如果 vvs_core
允许你扩展或自定义某些功能,你可以根据需要编写自己的工具类或扩展方法:
extension StringExtension on String {
String capitalize() {
if (this.length <= 1) return this.toUpperCase();
return this[0].toUpperCase() + this.substring(1);
}
}