Flutter跨应用组共享偏好设置插件shared_preference_app_group的使用
Flutter跨应用组共享偏好设置插件shared_preference_app_group的使用
Shared preferences with App Group
shared_preference_app_group
插件支持 iOS 应用组(App Group)功能,通过使用 -[NSUserDefaults initWithSuiteName:]
实现了偏好设置在应用组中的共享。
注意: 仅支持iOS平台
使用方法 Usage
要使用此插件,您需要将 shared_preference_app_group
添加为 依赖项到您的 pubspec.yaml 文件中。
示例代码 Example
以下是一个完整的示例应用程序,展示了如何使用 shared_preference_app_group
插件来设置、获取和移除偏好设置。请确保首先在Xcode项目中为Runner Target启用AppGroup能力,并通过setAppGroup
函数设置应用组ID。
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preference_app_group/shared_preference_app_group.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// 填入在Xcode项目中设置的应用组ID
String appGroupID = 'group.com.example.your_awesome_project';
Map<String, dynamic> myParams = {
'MY_BOOL_KEY': false,
'MY_STRING_KEY': 'null',
'MY_INT_KEY': 0,
'MY_DOUBLE_KEY': 0.1
};
String displayParamsText = '';
@override
void initState() {
super.initState();
// 必须首先设置应用组
SharedPreferenceAppGroup.setAppGroup(appGroupID);
}
void setMyParams() async {
await SharedPreferenceAppGroup.setBool('MY_BOOL_KEY', true);
await SharedPreferenceAppGroup.setString('MY_STRING_KEY', 'STRING_VALUE');
await SharedPreferenceAppGroup.setInt('MY_INT_KEY', 42);
await SharedPreferenceAppGroup.setDouble('MY_DOUBLE_KEY', 9.9);
await SharedPreferenceAppGroup.setStringList('MY_STRING_ARRAY', ["element1", "element2", "element3"]);
}
Future<void> getMyParams() async {
bool boolValue = await SharedPreferenceAppGroup.getBool('MY_BOOL_KEY') ?? false;
String stringValue = await SharedPreferenceAppGroup.getString('MY_STRING_KEY') ?? 'null';
int intValue = await SharedPreferenceAppGroup.getInt('MY_INT_KEY') ?? 0;
double doubleValue = await SharedPreferenceAppGroup.getDouble('MY_DOUBLE_KEY') ?? 0.0;
List<String> stringArrayValue = await SharedPreferenceAppGroup.getStringList('MY_STRING_ARRAY') ?? [];
this.myParams = {
'MY_BOOL_KEY': boolValue,
'MY_STRING_KEY': stringValue,
'MY_INT_KEY': intValue,
'MY_DOUBLE_KEY': doubleValue,
'MY_STRING_ARRAY': stringArrayValue
};
String text = '';
for (String key in this.myParams.keys) {
text += '$key = ${this.myParams[key]}\n';
}
setState(() {
this.displayParamsText = text;
});
}
Future<void> getAllParams() async {
Map<String, dynamic> allPreferences = await SharedPreferenceAppGroup.getAll();
String text = '';
for (String key in allPreferences.keys) {
text += '$key = ${allPreferences[key]}\n'; // 更正为 allPreferences
}
setState(() {
this.displayParamsText = text;
});
}
Future<void> removeMyParams() async {
await SharedPreferenceAppGroup.remove('MY_BOOL_KEY');
await SharedPreferenceAppGroup.remove('MY_STRING_KEY');
await SharedPreferenceAppGroup.remove('MY_INT_KEY');
await SharedPreferenceAppGroup.remove('MY_DOUBLE_KEY');
}
Future<void> removeAll() async {
await SharedPreferenceAppGroup.removeAll();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: ListView(
children: [
Padding(padding: const EdgeInsets.only(top: 50.0)),
Text('Only support iOS'),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Text('You should first enable the AppGroup capability for the Runner Target of your Xcode project, and then set the app group id to this plugin through `setAppGroup` function',
style: TextStyle(
fontSize: 11.0,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Container(
padding: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Color(0xff0e88eb),
),
width: 240.0,
height: 50.0,
child: CupertinoButton(
child: Text('SetParams',
style: TextStyle(
color: Colors.white
),
),
onPressed: setMyParams,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Container(
padding: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Color(0xff0e88eb),
),
width: 240.0,
height: 50.0,
child: CupertinoButton(
child: Text('RemoveParams',
style: TextStyle(
color: Colors.white,
),
),
onPressed: removeMyParams,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Container(
padding: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Color(0xff0e88eb),
),
width: 240.0,
height: 50.0,
child: CupertinoButton(
child: Text('RemoveAll',
style: TextStyle(
color: Colors.white
),
),
onPressed: removeAll,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Container(
padding: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Color(0xff0b5eda),
),
width: 240.0,
height: 50.0,
child: CupertinoButton(
child: Text('GetAllParams',
style: TextStyle(
color: Colors.white
),
),
onPressed: getAllParams,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Container(
padding: const EdgeInsets.all(0.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12.0),
color: Color(0xff0b5eda),
),
width: 240.0,
height: 50.0,
child: CupertinoButton(
child: Text('GetMyParams',
style: TextStyle(
color: Colors.white
),
),
onPressed: getMyParams,
),
),
Padding(padding: const EdgeInsets.only(top: 10.0)),
Text(displayParamsText,
style: TextStyle(
fontSize: 11.0,
),
),
],
)
)
),
),
);
}
}
相关项目 Related projects
如果您需要实现屏幕录制功能,我开发了一些有用的插件:
- iOS: ReplayKit Launcher: 一个用于打开iOS上的
RPSystemBroadcastPickerView
的Flutter插件。 - Android: MediaProjection Creator: 一个用于创建Android上的
MediaProjection
实例(包括请求权限)的Flutter插件。
另一个实用的演示:
- Screen Capture Flutter Demo: 使用ZEGO Express Audio and Video Flutter SDK在iOS/Android上实现屏幕直播。
贡献 Contributing
欢迎所有人通过提交Pull Request贡献代码、帮助回答问题、添加文档或以其他方式提供帮助。
更多关于Flutter跨应用组共享偏好设置插件shared_preference_app_group的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复