Flutter应用设置插件 - 轻松打开iOS和Android系统设置

app_settings

一个用于从Flutter应用中打开iOS和Android手机设置的插件。

功能特性

  • 🚀 跨平台支持 - 完美支持iOS和Android两大移动平台
  • ⚙️ 多种设置类型 - 支持打开位置、Wi-Fi、蓝牙、通知、声音等多种系统设置
  • 🔧 Android Q面板支持 - 支持Android Q及以上版本的设置面板功能
  • 📱 Swift包管理器 - 支持SPM方式集成到iOS项目
  • 🎯 灵活配置 - 可指定特定设置类型,平台不支持时自动回退到通用设置
  • 🔄 新任务模式 - Android平台支持在新Activity中打开设置页面

安装指南

添加依赖

pubspec.yaml文件中添加依赖:

yaml 复制代码
dependencies:
  app_settings: ^6.1.1

或使用命令行:

bash 复制代码
flutter pub add app_settings

导入包

在Dart代码中导入:

dart 复制代码
import 'package:app_settings/app_settings.dart';

iOS配置

如果项目使用Objective-C,需要在Podfile中添加:

ruby 复制代码
target 'Runner' do
  use_frameworks!

如需使用Swift Package Manager,启用Swift支持:

bash 复制代码
flutter config --enable-swift-package-manager

使用说明

基础用法

打开应用的设置页面:

dart 复制代码
Widget build(BuildContext context) {
  return ElevatedButton(
    onPressed: () => AppSettings.openAppSettings(),
    child: const Text('打开应用设置'),
  );
}

指定设置类型

打开特定类型的设置页面:

dart 复制代码
Widget build(BuildContext context) {
  return ElevatedButton(
    onPressed: () => AppSettings.openAppSettings(type: AppSettingsType.location),
    child: const Text('打开位置设置'),
  );
}

Android Q设置面板

在Android Q及以上版本打开设置面板:

dart 复制代码
Widget build(BuildContext context) {
  return ElevatedButton(
    onPressed: () => AppSettings.openAppSettingsPanel(AppSettingsPanelType.volume),
    child: const Text('打开音量设置面板'),
  );
}

支持的所有设置类型

插件支持打开以下设置类型:

  • 应用设置 (AppSettingsType.settings)
  • 位置设置 (AppSettingsType.location)
  • Wi-Fi设置 (AppSettingsType.wifi)
  • 蓝牙设置 (AppSettingsType.bluetooth)
  • 通知设置 (AppSettingsType.notification)
  • 声音设置 (AppSettingsType.sound)
  • 显示设置 (AppSettingsType.display)
  • 日期设置 (AppSettingsType.date)
  • 应用语言设置 (AppSettingsType.appLocale) - Android 13+
  • 开发者设置 (AppSettingsType.developer)
  • 以及其他系统设置

核心代码

主要API调用

dart 复制代码
/// 打开应用设置的主方法
/// [type]: 设置类型,默认为通用设置
/// [asAnotherTask]: Android平台是否在新Activity中打开
Future<void> openAppSettings({
  AppSettingsType type = AppSettingsType.settings,
  bool asAnotherTask = false,
}) async {
  // 实现平台特定的设置打开逻辑
  // iOS使用UIApplication.openSettingsURLString
  // Android使用Intent跳转到对应设置页面
}

Android平台实现

dart 复制代码
/// 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平台实现

dart 复制代码
/// 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);
  }
}

设置面板支持

dart 复制代码
/// 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应用中集成系统设置跳转功能,提升用户体验。

相关推荐
蜜桃味女焊匠人1 小时前
焊接生产线优化思路:解决手工焊、机器人焊气体浪费问题
人工智能·经验分享·其他·机器人
Georgeviewer5 小时前
商业落地评测|实体门店GEO优化性价比与服务体系深度复盘
大数据·人工智能
GuWenyue6 小时前
分不清AI Workflow与Agent?3个实战案例彻底讲透,做AI应用不再踩选型坑
人工智能
彩讯股份3006347 小时前
彩讯股份与心洲科技签署战略合作协议,共建企业级模型后训练能力
人工智能·科技
迅易科技7 小时前
从场景验证到Agent上线:迅易 × WorkBuddy如何帮助企业建设AI能力?
人工智能·ai·腾讯云
PNP Robotics7 小时前
多伦多大学机器人峰会|物理AI与具身智能落地新趋势
人工智能·深度学习·机器学习·机器人
GIR1237 小时前
官方出品 | 多通道土壤呼吸测量系统市场现状与十五五规划深度报告:行业分析+趋势预测全收录
大数据·人工智能·机器学习
绿算技术7 小时前
绿算技术亮相第十八届HPC AI中国年会,擘画AI基础设施全栈协同新图景
人工智能
Litluecat8 小时前
2026年7月22日科技热点新闻
人工智能·科技·新闻·每日·速览
To_OC8 小时前
别再傻傻分不清:Workflow 和 Agent 到底不是一回事
人工智能·agent·workflow