Flutter本地功能集合插件local_function_collections的使用
Flutter本地功能集合插件local_function_collections的使用
Local Function Collections 是一个简单的包,提供了导航、对话框和安全存储的功能。
特性
该包提供了以下功能:
- 导航功能,如跳转到、替换为和返回上一页。
- 对话框功能,如确认对话框、选项对话框和加载对话框。
- 安全存储功能,如写入、读取、移除和清除。
开始使用
确保你的 Flutter 版本是最新的。
使用方法
任何进一步的说明都已添加到 example.dart
文件中。
class FirstPage extends StatefulWidget {
const FirstPage({super.key});
[@override](/user/override)
State<FirstPage> createState() => _FirstPageState();
}
class _FirstPageState extends State<FirstPage> {
[@override](/user/override)
void initState() {
super.initState();
}
onOKDialogPressed() => LocalDialogFunction.okDialog(
context: context,
contentText: "Hello Users!!!",
contentAlign: TextAlign.justify,
);
onOptionDialogPressed() => LocalDialogFunction.optionDialog(
context: context,
contentText: "Do you like this package?",
onAccept: () => LocalDialogFunction.okDialog(
context: context,
title: "Thank You!!!",
contentText: "Thank you for appreciating our package!, soon there will be another new package launched.",
contentAlign: TextAlign.justify,
),
onDecline: () => LocalDialogFunction.okDialog(
context: context,
title: "We're Sorry",
contentText: "We're sorry if our package isn't enough for you :(, for any critiques you can DM me on Instagram: @raznovrnf or my LinkedIn: Razy Firdana.",
contentAlign: TextAlign.justify,
),
contentAlign: TextAlign.justify,
);
onLoadingDialogPressed() {
LocalDialogFunction.loadingDialog(
context: context,
contentText: "This loading will disappear in 3 second",
contentAlign: TextAlign.justify,
);
Future.delayed(const Duration(seconds: 3), () {
if(mounted) {
LocalRouteNavigator.closeBack(context: context);
}
});
}
onMoveTo2ndPage() => LocalRouteNavigator.moveTo(
context: context,
target: const SecondPage(),
);
onMoveTo3rdPage() => LocalRouteNavigator.moveTo(
context: context,
target: const ThirdPage(
isJumped: true,
),
);
onUpdateSecureStorage() async => await LocalSecureStorage.writeKey(
key: "example",
data: "This example was created at ${DateTime.now()}",
);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text(
"LCF Demo App 1st Page",
),
),
body: ListView(
padding: const EdgeInsets.symmetric(
horizontal: 10.0,
),
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 4,
),
Column(
children: <Widget>[
const Text(
"欢迎来到第一页,\n让我们试用本地功能集合",
textAlign: TextAlign.center,
),
const SizedBox(
height: 10.0,
),
const Text(
"对话框集合",
),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () => onOKDialogPressed(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"确定",
),
),
),
),
const SizedBox(
width: 10.0,
),
Expanded(
child: ElevatedButton(
onPressed: () => onOptionDialogPressed(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"选项",
),
),
),
),
const SizedBox(
width: 10.0,
),
Expanded(
child: ElevatedButton(
onPressed: () => onLoadingDialogPressed(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"加载",
),
),
),
),
],
),
const SizedBox(
height: 5.0,
),
const Text(
"路由集合",
),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: () => onMoveTo2ndPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"跳转到第2页",
),
),
),
),
const SizedBox(
width: 10.0,
),
Expanded(
child: ElevatedButton(
onPressed: () => onMoveTo3rdPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"跳转到第3页",
),
),
),
),
],
),
const SizedBox(
height: 5.0,
),
const Text(
"安全存储集合",
),
ElevatedButton(
onPressed: () => onUpdateSecureStorage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"更新安全存储时间戳",
),
),
),
],
),
],
),
);
}
}
class SecondPage extends StatefulWidget {
const SecondPage({super.key});
[@override](/user/override)
State<SecondPage> createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
String? storageResult;
[@override](/user/override)
void initState() {
super.initState();
onCheckSecureStorage();
}
onCheckSecureStorage() async {
await LocalSecureStorage.readKey(key: "example").then((result) {
setState(() {
storageResult = result;
});
});
}
onCloseThisPage() => LocalRouteNavigator.closeBack(context: context);
onReplaceTo3rdPage() => LocalRouteNavigator.replaceWith(
context: context,
target: const ThirdPage(
isJumped: true,
),
);
onMoveTo3rdPage() => LocalRouteNavigator.moveTo(
context: context,
target: const ThirdPage(
isJumped: false,
),
callbackFunction: (_) => onCheckSecureStorage(),
);
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text(
"LCF Demo App 第2页",
),
),
body: ListView(
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 4,
),
Column(
children: <Widget>[
const Text(
"欢迎来到第2页...",
textAlign: TextAlign.center,
),
const SizedBox(
height: 10.0,
),
Text(
"保存在本地安全存储中的文本: ${storageResult ?? "-"}",
textAlign: TextAlign.center,
),
const SizedBox(
height: 10.0,
),
ElevatedButton(
onPressed: () => onCloseThisPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"回到第1页",
),
),
),
const SizedBox(
height: 5.0,
),
ElevatedButton(
onPressed: () => onMoveTo3rdPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"跳转到第3页",
),
),
),
const SizedBox(
height: 5.0,
),
ElevatedButton(
onPressed: () => onReplaceTo3rdPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"用第3页替换当前页面",
),
),
),
],
),
],
),
);
}
}
class ThirdPage extends StatefulWidget {
final bool isJumped;
const ThirdPage({
super.key,
required this.isJumped,
});
[@override](/user/override)
State<ThirdPage> createState() => _ThirdPageState();
}
class _ThirdPageState extends State<ThirdPage> {
String? storageResult;
[@override](/user/override)
void initState() {
super.initState();
onCheckSecureStorage();
}
onCheckSecureStorage() async {
await LocalSecureStorage.readKey(key: "example").then((result) {
setState(() {
storageResult = result;
});
});
}
onCloseThisPage() => LocalRouteNavigator.closeBack(context: context);
onRedirectThisPage() => LocalRouteNavigator.redirectTo(
context: context,
target: const FirstPage(),
);
onClearSecureStorage() async => await LocalSecureStorage.clearKey().then((_) => onCheckSecureStorage());
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text(
"LCF Demo App 第3页",
),
),
body: ListView(
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 4,
),
Column(
children: <Widget>[
const Text(
"欢迎来到第3页...",
textAlign: TextAlign.center,
),
const SizedBox(
height: 10.0,
),
Text(
"保存在本地安全存储中的文本: ${storageResult ?? "-"}",
textAlign: TextAlign.center,
),
const SizedBox(
height: 10.0,
),
ElevatedButton(
onPressed: () => onClearSecureStorage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"清除安全存储",
),
),
),
const SizedBox(
height: 5.0,
),
ElevatedButton(
onPressed: () => onCloseThisPage(),
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
widget.isJumped == true
? "回到第1页"
: "回到第2页",
),
),
),
const SizedBox(
height: 5.0,
),
widget.isJumped == true ?
const Material() :
ElevatedButton(
onPressed: () => onRedirectThisPage(),
child: const Padding(
padding: EdgeInsets.all(5.0),
child: Text(
"重定向到第1页",
),
),
),
],
),
],
),
);
}
}
更多关于Flutter本地功能集合插件local_function_collections的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复