feat: 添加 Android 文件选择器支持,更新 H5ShellPage 以集成文件选择功能

This commit is contained in:
Booker
2026-05-27 15:34:16 +07:00
parent 3f71ba0e23
commit 76897c6e0f
2 changed files with 172 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'config/app_config.dart';
@@ -14,6 +15,8 @@ const _shellAccent = Color(0xFF0089FF);
const _shellSubText = Color(0xFF8E9AB0);
const _shellBrandingChannel =
MethodChannel('io.openim.flutter.im_webview_app/shell_branding');
const _androidFilePickerChannel =
MethodChannel('io.openim.flutter.openim/file_picker');
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
@@ -120,7 +123,7 @@ class _H5ShellPageState extends State<H5ShellPage> {
}
WebViewController _buildController(int lineIndex) {
return WebViewController(
final controller = WebViewController(
onPermissionRequest: (request) {
unawaited(_handleWebViewPermissionRequest(request));
},
@@ -176,6 +179,39 @@ class _H5ShellPageState extends State<H5ShellPage> {
onNavigationRequest: _handleNavigationRequest,
),
);
_configurePlatformController(controller);
return controller;
}
void _configurePlatformController(WebViewController controller) {
final platformController = controller.platform;
if (platformController is AndroidWebViewController) {
unawaited(
platformController.setOnShowFileSelector(_handleAndroidFileSelection),
);
}
}
Future<List<String>> _handleAndroidFileSelection(
FileSelectorParams params,
) async {
if (params.mode == FileSelectorMode.save) {
return <String>[];
}
try {
final result = await _androidFilePickerChannel.invokeListMethod<String>(
'pickFiles',
{
'acceptTypes': params.acceptTypes,
'allowMultiple': params.mode == FileSelectorMode.openMultiple,
},
);
return result ?? <String>[];
} catch (_) {
return <String>[];
}
}
@override