Flutter中URL_Launcher无法启动URL问题求助
问题:点击Container无法打开指定URL
我要实现点击Container时打开指定URL的功能,这个功能在其他项目里跑通好几次了,但当前项目点击容器完全没反应。pubspec.yaml、AndroidManifest.xml和Info.plist的配置都做了,main.dart里也导入了需要的依赖,是不是漏了什么步骤?
相关代码
InkWell( onTap: () {}, splashColor: Colors.transparent, highlightColor: Colors.transparent, child: Padding( padding: const EdgeInsets.all(5.0), child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(10.0), color: Colors.yellow.shade800, ), // color: Colors.yellow.shade800, height: 60.0, child: Center( child: Padding( padding: const EdgeInsets.only(right: 100.0), child: GestureDetector( onTap: () => launchUrl(Uri.parse( 'https://www.youtube.com/watch?v=sRc-uB6IgS4&list=PLNJd3WyRswnfgMo0sf8&index=1')), child: const Center( child: Text( 'Help', style: TextStyle(color: Colors.white, fontSize: 30), ), ), ), ),
AndroidManifest.xml配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Provide required visibility configuration for API level 30 and above --> <queries> <!-- If your app checks for SMS support --> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="sms" /> </intent> <!-- If your app checks for call support --> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="tel" /> </intent> <!-- If your application checks for inAppBrowserView launch mode support --> <intent> <action android:name="android.support.customtabs.action.CustomTabsService" /> </intent> </queries> <application android:label="PocketAlgs" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
Info.plist配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>LSApplicationQueriesSchemes</key> <array> <string>sms</string> <string>tel</string> </array> <key>CADisableMinimumFrameDurationOnPhone</key> <true/> <key>CFBundleDevelopmentRegion</key> <string>$(DEVELOPMENT_LANGUAGE)</string> <key>CFBundleDisplayName</key> <string>PocketAlgs</string> <key>CFBundleExecutable</key> <string>$(EXECUTABLE_NAME)</string> <key>CFBundleIdentifier</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundleName</key> <string>testing_stuff_ignore</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> <string>$(FLUTTER_BUILD_NAME)</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>$(FLUTTER_BUILD_NUMBER)</string> <key>LSRequiresIPhoneOS</key> <true/> <key>UIApplicationSupportsIndirectInputEvents</key> <true/> <key>UILaunchStoryboardName</key> <string>LaunchScreen</string> <key>UIMainStoryboardFile</key> <string>Main</string> <key>UIStatusBarStyle</key> <string></string> <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> </array> <key>UIStatusBarHidden</key> <false/> </dict> </plist>
解决方案
移除外层InkWell的空onTap拦截
最外层的InkWell设置了空的onTap: () {},会优先消费点击事件,导致内部GestureDetector无法响应。要么删掉这个空的onTap,要么将其设为null,让点击事件透传下去:InkWell( // 删掉onTap: () {}, 或者改为onTap: null, splashColor: Colors.transparent, highlightColor: Colors.transparent, child: ... // 原有子组件 )补充iOS的http/https scheme权限
Info.plist的LSApplicationQueriesSchemes只添加了sms和tel,要打开HTTPS链接必须补充http和https:<key>LSApplicationQueriesSchemes</key> <array> <string>sms</string> <string>tel</string> <string>http</string> <string>https</string> </array>优化launchUrl调用逻辑
launchUrl是异步方法,建议添加async/await,同时显式指定启动模式确保行为符合预期:onTap: () async { await launchUrl( Uri.parse('https://www.youtube.com/watch?v=sRc-uB6IgS4&list=PLNJd3WyRswnfgMo0sf8&index=1'), mode: LaunchMode.externalApplication, ); }检查依赖版本
确认url_launcher依赖为最新稳定版,运行flutter pub upgrade url_launcher更新,避免版本兼容性问题。
内容的提问来源于stack exchange,提问作者Spicy Pumpkin
相关产品推荐
相关产品推荐

