Flutter基础功能扩展插件bsmi_base的使用
Flutter基础功能扩展插件bsmi_base的使用
这是bsmi 团队打造的实用工具类库。
时间相关
参考: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
格式化时间,获取时间等。
开发者备注
如果你加入了修改代码,请确保能够正确地格式化源代码:
dart format .
示例代码
以下是一个完整的示例,展示了如何使用bsmi_base
插件中的各种功能。
import 'package:bsmi_base/bsmi_base.dart' as bb;
import 'package:json_annotation/json_annotation.dart';
// 这一行特别重要,它定义了包含 pub 自动生成的 Dart 代码的锚点。
// 文件名与这个文件名 bsmi_base_example.dart 对应。
// 构建的命令为:flutter pub run build_runner build
part 'bsmi_base_example.g.dart';
[@JsonSerializable](/user/JsonSerializable)(
anyMap: true,
checked: true,
disallowUnrecognizedKeys: true,
)
class DemoYml {
@JsonKey(required: true)
final String ym;
DemoYml({required this.ym}) {
if (ym.isEmpty) {
throw ArgumentError.value(ym, 'name', 'Cannot be empty.');
}
}
factory DemoYml.fromJson(Map json) => _$DemoYmlFromJson(json);
Map<String, dynamic> toJson() => _$DemoYmlToJson(this);
@override
String toString() => 'DemoYml: ${toJson()}';
}
void main() {
// 使用 Awesome 类
var awesome = bb.Awesome();
print('awesome: ${awesome.isAwesome}');
// 使用 TimeUtils 类
var tu = bb.TimeUtils();
print('TimeUnits version: ${tu.timeUtilsVersion}');
print('current time stamp: ${tu.getCurrrentTimestamp()}');
print('current time in millseconds: ${tu.getCurrentMillseconds()}');
// 使用 YamlUtils 类
var yt = bb.YamlUtils();
print('yaml map: ${yt.loadYamlMapFromFile("example/demo.yml")}');
// 使用泛型化的 YamlUtils 类
var yt2 = bb.YamlUtils<DemoYml>();
print('yaml: ${yt2.parseYamlFileToClass("example/demo.yml", (p0) => DemoYml.fromJson(p0!))}');
}
代码解释
-
导入必要的包:
import 'package:bsmi_base/bsmi_base.dart' as bb; import 'package:json_annotation/json_annotation.dart';
-
定义一个使用
[@JsonSerializable](/user/JsonSerializable)
注解的类:[@JsonSerializable](/user/JsonSerializable)( anyMap: true, checked: true, disallowUnrecognizedKeys: true, ) class DemoYml { @JsonKey(required: true) final String ym; DemoYml({required this.ym}) { if (ym.isEmpty) { throw ArgumentError.value(ym, 'name', 'Cannot be empty.'); } } factory DemoYml.fromJson(Map json) => _$DemoYmlFromJson(json); Map<String, dynamic> toJson() => _$DemoYmlToJson(this); @override String toString() => 'DemoYml: ${toJson()}'; }
-
在
main
函数中使用插件提供的功能:void main() { // 使用 Awesome 类 var awesome = bb.Awesome(); print('awesome: ${awesome.isAwesome}'); // 使用 TimeUtils 类 var tu = bb.TimeUtils(); print('TimeUnits version: ${tu.timeUtilsVersion}'); print('current time stamp: ${tu.getCurrrentTimestamp()}'); print('current time in millseconds: ${tu.getCurrentMillseconds()}'); // 使用 YamlUtils 类 var yt = bb.YamlUtils(); print('yaml map: ${yt.loadYamlMapFromFile("example/demo.yml")}'); // 使用泛型化的 YamlUtils 类 var yt2 = bb.YamlUtils<DemoYml>(); print('yaml: ${yt2.parseYamlFileToClass("example/demo.yml", (p0) => DemoYml.fromJson(p0!))}'); }
更多关于Flutter基础功能扩展插件bsmi_base的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter基础功能扩展插件bsmi_base的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
bsmi_base
是一个为 Flutter 应用提供基础功能扩展的插件。它包含了一些常用的工具和方法,可以帮助开发者更高效地构建应用。以下是对 bsmi_base
插件的功能和使用方法的简要介绍。
主要功能
-
基础工具类:
- 日期时间工具: 提供日期和时间的格式化、解析等操作。
- 字符串工具: 提供字符串的常用操作,如空值检查、截取、格式化等。
- 数字工具: 提供数字的格式化、转换等操作。
- 文件操作工具: 提供文件读写、删除等操作。
-
网络请求:
- 封装了网络请求的常用操作,提供简单的 API 进行 GET、POST 等请求。
- 支持请求拦截器、响应拦截器,方便处理请求前后逻辑。
-
本地存储:
- 封装了 SharedPreferences 的操作,方便进行本地数据的存储和读取。
-
UI组件:
- 提供了一些常用的 UI 组件,如按钮、弹窗、加载框等,简化 UI 开发。
-
路由管理:
- 提供了简单的路由管理工具,方便进行页面跳转和参数传递。
-
日志打印:
- 提供了日志打印工具,支持不同级别的日志输出。
-
主题管理:
- 提供了主题管理工具,方便应用主题的切换和管理。
使用方法
1. 安装插件
在 pubspec.yaml
文件中添加 bsmi_base
依赖:
dependencies:
flutter:
sdk: flutter
bsmi_base: ^版本号
然后运行 flutter pub get
安装插件。
2. 基本使用
import 'package:bsmi_base/bsmi_base.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'BsmiBase Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BsmiBase Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Current Date: ${BsmiDateUtil.formatDateTime(DateTime.now())}'),
ElevatedButton(
onPressed: () {
BsmiNavigator.pushNamed(context, '/nextPage');
},
child: Text('Go to Next Page'),
),
],
),
),
);
}
}
3. 网络请求示例
import 'package:bsmi_base/bsmi_base.dart';
void fetchData() async {
var response = await BsmiHttp.get('https://jsonplaceholder.typicode.com/posts/1');
if (response.isSuccess) {
print('Response Data: ${response.data}');
} else {
print('Error: ${response.error}');
}
}
4. 本地存储示例
import 'package:bsmi_base/bsmi_base.dart';
void saveData() async {
await BsmiStorage.saveString('key', 'value');
String value = await BsmiStorage.getString('key');
print('Stored Value: $value');
}
5. 日志打印示例
import 'package:bsmi_base/bsmi_base.dart';
void logExample() {
BsmiLog.d('This is a debug log');
BsmiLog.i('This is an info log');
BsmiLog.w('This is a warning log');
BsmiLog.e('This is an error log');
}