Flutter工具集插件starlight_utils的使用
Starlight Utils 的使用
Starlight Utils 是一个方便的 Flutter 工具集插件,它简化了导航、对话框、底部菜单等常见功能的实现。此外,它还提供了对字符串、数字、列表、地图、日期、颜色等实用函数的支持。
特性
以下是一些支持的功能及其状态:
名称 | 状态 |
---|---|
无上下文导航服务 | ✅ |
无上下文对话框 | ✅ |
无上下文底部菜单 | ✅ |
无上下文 Snackbar | ✅ |
无上下文日期选择器 | ✅ |
无上下文时间选择器 | ✅ |
简易菜单 | ✅ |
字符串实用函数 | ✅ |
数字实用函数 | ✅ |
列表实用函数 | ✅ |
地图实用函数 | ✅ |
日期实用函数 | ✅ |
颜色实用函数 | ✅ |
响应式实用函数 | ✅ |
MediaQuery 实用函数 | ✅ |
Theme 实用函数 | ✅ |
截图
安装
在 pubspec.yaml
文件中添加 starlight_utils
作为依赖项:
dependencies:
starlight_utils:
git:
url: https://github.com/YeMyoAung/starlight_utils.git
设置
无需额外集成步骤即可在 Android 和 iOS 上使用。
使用
首先,导入我们的包:
import 'package:starlight_utils/starlight_utils.dart';
然后就可以轻松使用了。
无上下文导航
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
StarlightUtils.push(MyHome()); // `Navigation.push()` 快捷方式。
StarlightUtils.pushNamed('home'); // `Navigation.pushNamed()` 快捷方式。
StarlightUtils.pushNamedAndRemoveUntil('home'); // `Navigation.pushNamedAndRemoveUntil()` 快捷方式。
StarlightUtils.pushAndRemoveUntil(MyHome()); // `Navigation.pushAndRemoveUntil()` 快捷方式。
StarlightUtils.pushReplacement(MyHome()); // `Navigation.pushReplacement()` 快捷方式。
StarlightUtils.pushReplacementNamed('home'); // `Navigation.pushReplacement()` 快捷方式。
StarlightUtils.popAndPushNamed('home'); // `Navigation.popAndPushNamed()` 快捷方式。
StarlightUtils.pop(); // `Navigation.pop()` 快捷方式。
StarlightUtils.conPop(); // `Navigation.conPop()` 快捷方式。
return MaterialApp(
navigatorKey: StarlightUtils.navigatorKey, // 重要
);
}
}
关于对话框
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: StarlightUtils.aboutDialog,
child: const Text("aboutDialog"),
),
);
}
}
对话框
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.dialog(AlertDialog(
title: Text("hello"),
));
},
child: const Text("dialog"),
),
);
}
}
底部菜单
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.bottomSheet(Container(
width: context.width,
height: 100,
child: Text("bottom sheet"),
));
},
child: const Text("bottomSheet"),
),
);
}
}
Snackbar
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.snackbar(SnackBar(content: Text('hello')));
},
child: const Text("snackbar"),
),
);
}
}
日期选择器
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
StarlightUtils.of(context); // 重要
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.datePicker(
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2100),
);
},
child: const Text("datePicker"),
),
);
}
}
时间选择器
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
StarlightUtils.of(context); // 重要
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.timePicker(
initialTime: TimeOfDay.fromDateTime(
DateTime.now(),
),
);
},
child: const Text("timePicker"),
),
);
}
}
日期范围选择器
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
StarlightUtils.of(context); // 重要
return SizedBox(
width: context.width - 20,
height: 45,
child: ElevatedButton(
onPressed: () {
StarlightUtils.dateRangePicker(
firstDate: DateTime(2000),
lastDate: DateTime(2100),
);
},
child: const Text("dateRangePicker"),
),
);
}
}
菜单
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
StarlightUtils.of(context); // 重要
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
onPressed: () {
StarlightUtils.menu(
position: RelativeRect.fromLTRB(10, 0, 0, 0),
items: [
PopupMenuItem(child: Text('item 1')),
PopupMenuItem(child: Text('item 2')),
PopupMenuItem(child: Text('item 3')),
],
);
},
icon: Icon(Icons.more_vert),
)
],
),
);
}
}
简易菜单
class TestSocketScreen extends StatelessWidget {
const TestSocketScreen({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return StarlightEasyMenu(
childBuilder: (context, controller) => StreamBuilder<bool>(
initialData: false,
stream: stream,
builder: (_, snapshot) {
return AnimatedIcon(
icon: snapshot.data == true
? AnimatedIcons.menu_close
: AnimatedIcons.arrow_menu,
progress: kAlwaysCompleteAnimation,
);
},
),
menuBuilder: (context, controller) => Container(
width: 200,
height: 120,
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
onTap: controller.hideMenu,
title: Row(
children: const [
Icon(Icons.settings),
SizedBox(width: 10),
Text("Setting"),
],
),
),
ListTile(
onTap: controller.hideMenu,
title: Row(
children: const [
Icon(Icons.help),
SizedBox(width: 10),
Text("Help"),
],
),
),
],
),
),
);
}
}
字符串
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
print("Hello World".toValidate); // helloworld
print("a b c".withoutWhiteSpace); // abc
print("a@".isEmail); // false
print("password".isStrongPassword(
checkUpperCase: true,
checkLowerCase: true,
checkDigit: true,
checkSpecailChar: true,
minLength: 6
)); // null or requirement
final File file = "file_path".file; // File Instance
final Uri uri = "http://url.com".uri; // Uri Instance
return Container();
}
}
数字
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
print(1.ordinal); // 1st (String)
print(2.ordinal); // 2nd (String)
print(100000.currencyFormat); // 100,000 (String)
print(1000.humanReadAble(useLowerCase: true)); // 1k (String)
return Container();
}
}
列表
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
print(["Starlight Studio", {"name": "Starlight Studio"}].equal(["Starlight Studio", {"name": "Starlight Studio"}])); // true
print([1, 2, 3, 4, 5, 6].chunk(2)); // [[1, 2], [3, 4], [5, 6]]
return Container();
}
}
地图
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
print({
"name": "Starlight Studio",
"in_list": ["Starlight Studio"]
}.equal({
"name": "Starlight Studio",
"in_list": ["Starlight Studio"]
})); // true
return Container();
}
}
响应式
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
10.w(context); // 返回基于设备计算的宽度
10.h(context); // 返回基于设备计算的高度
3.sp(context); // 返回基于设备计算的 sp
return Container();
}
}
MediaQuery
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
context.with; // 设备宽度
context.height; // 设备高度
context.devicePixelRatio; // 设备像素比
context.textScaleFactor; // 文本缩放因子
context.topSafe; // Appbar 高度
context.bottomSafe; // 底部导航栏高度
context.orientation; // 设备方向
context.invertColors; // 是否反转颜色
context.highContrast; // 是否高对比度
context.gestureSettings; // 手势设置
context.boldText; // 是否粗体文本
context.alwaysUse24HourFormat; // 是否始终使用 24 小时制
context.accessibleNavigation; // 是否可访问导航
context.viewInsets; // 处理键盘显示或隐藏
return Container();
}
}
日期
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
print(DateTime.now().monthformat(MonthFormat.long)); // February
print(DateTime.now().monthformat(MonthFormat.short)); // Feb
print(DateTime.now().monthformat(MonthFormat.ordinal)); // 2nd
print(DateTime.now().monthformat(MonthFormat.none)); // 2
print(DateTime.now().isLeapYear); // false
print(DateTime.now().maxDay); // 28
print(DateTime(2022, 2, 2).differenceTimeInString(DateTime(2022, 2, 1))); // 24h
return Container();
}
}
Theme
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
context.theme; // ThemeData
return Container();
}
}
更多关于Flutter工具集插件starlight_utils的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter工具集插件starlight_utils的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
starlight_utils
是一个 Flutter 工具集插件,旨在为开发者提供一系列常用的工具和功能,以简化开发流程并提高代码的可维护性。这个插件可能包含各种实用工具,如字符串处理、日期时间操作、网络请求、状态管理等。
以下是如何在 Flutter 项目中使用 starlight_utils
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 starlight_utils
插件的依赖。
dependencies:
flutter:
sdk: flutter
starlight_utils: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 导入包
在你的 Dart 文件中导入 starlight_utils
包:
import 'package:starlight_utils/starlight_utils.dart';
3. 使用工具集
starlight_utils
提供了多种工具和功能,以下是一些常见的使用示例:
字符串处理
String text = "Hello, World!";
print(text.capitalize()); // 输出: Hello, world!
日期时间操作
DateTime now = DateTime.now();
print(now.format("yyyy-MM-dd")); // 输出: 2023-10-05
网络请求
var response = await StarlightHttp.get("https://jsonplaceholder.typicode.com/posts");
print(response.body);
状态管理
class MyController extends StarlightController {
final count = 0.obs;
void increment() {
count.value++;
}
}
// 在 Widget 中使用
class MyWidget extends StatelessWidget {
final controller = MyController();
[@override](/user/override)
Widget build(BuildContext context) {
return Obx(() => Text("Count: ${controller.count.value}"));
}
}