欧拉数理化
80.64MB · 2025-10-22
一个用于从Flutter应用中打开iOS和Android手机设置的插件。
在pubspec.yaml
文件中添加依赖:
dependencies:
app_settings: ^6.1.1
或使用命令行:
flutter pub add app_settings
在Dart代码中导入:
import 'package:app_settings/app_settings.dart';
如果项目使用Objective-C,需要在Podfile
中添加:
target 'Runner' do
use_frameworks!
如需使用Swift Package Manager,启用Swift支持:
flutter config --enable-swift-package-manager
打开应用的设置页面:
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => AppSettings.openAppSettings(),
child: const Text('打开应用设置'),
);
}
打开特定类型的设置页面:
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => AppSettings.openAppSettings(type: AppSettingsType.location),
child: const Text('打开位置设置'),
);
}
在Android Q及以上版本打开设置面板:
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => AppSettings.openAppSettingsPanel(AppSettingsPanelType.volume),
child: const Text('打开音量设置面板'),
);
}
插件支持打开以下设置类型:
AppSettingsType.settings
)AppSettingsType.location
)AppSettingsType.wifi
)AppSettingsType.bluetooth
)AppSettingsType.notification
)AppSettingsType.sound
)AppSettingsType.display
)AppSettingsType.date
)AppSettingsType.appLocale
) - Android 13+AppSettingsType.developer
)/// 打开应用设置的主方法
/// [type]: 设置类型,默认为通用设置
/// [asAnotherTask]: Android平台是否在新Activity中打开
Future<void> openAppSettings({
AppSettingsType type = AppSettingsType.settings,
bool asAnotherTask = false,
}) async {
// 实现平台特定的设置打开逻辑
// iOS使用UIApplication.openSettingsURLString
// Android使用Intent跳转到对应设置页面
}
/// Android平台的设置打开实现
Future<void> _openAppSettingsAndroid(
AppSettingsType type, bool asAnotherTask) async {
// 根据设置类型构建对应的Intent
final Intent intent = _getSettingsIntent(type);
// 设置Intent标志位
if (asAnotherTask) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
// 启动设置Activity
await _channel.invokeMethod('openAppSettings', {
'intent': intent.toUri(0),
'asAnotherTask': asAnotherTask,
});
}
/// iOS平台的设置打开实现
Future<void> _openAppSettingsIOS(AppSettingsType type) async {
final String urlString;
// 根据设置类型构建对应的URL Scheme
switch (type) {
case AppSettingsType.location:
urlString = 'App-Prefs:root=Privacy&path=LOCATION';
break;
case AppSettingsType.wifi:
urlString = 'App-Prefs:root=WIFI';
break;
// 其他设置类型的URL Scheme处理
default:
urlString = UIApplication.openSettingsURLString;
}
// 通过URL打开系统设置
if (await canLaunch(urlString)) {
await launch(urlString);
}
}
/// Android Q设置面板打开方法
Future<void> openAppSettingsPanel(AppSettingsPanelType type) async {
// 检查Android版本,仅Q及以上支持
if (defaultTargetPlatform == TargetPlatform.android) {
final String panelType = _getPanelTypeString(type);
await _channel.invokeMethod('openAppSettingsPanel', {
'panelType': panelType,
});
} else {
// 其他平台回退到通用设置
await openAppSettings();
}
}
该插件提供了简洁易用的API,让开发者能够轻松地在Flutter应用中集成系统设置跳转功能,提升用户体验。