-
-
Save maryannemm/fdc12b80415c1c2007d02e49c9deafd5 to your computer and use it in GitHub Desktop.
Attach file , and open camera from iframe or webview in flutter app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:file_picker/file_picker.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:image_picker/image_picker.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
// ignore: depend_on_referenced_packages | |
import 'package:webview_flutter_android/webview_flutter_android.dart' | |
as webview_flutter_android; | |
import 'package:image_picker/image_picker.dart' as image_picker; | |
class PreviewWebpage extends StatefulWidget { | |
const PreviewWebpage({super.key}); | |
@override | |
State<PreviewWebpage> createState() => _PreviewWebpageState(); | |
} | |
class _PreviewWebpageState extends State<PreviewWebpage> { | |
late final WebViewController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = WebViewController() | |
..setJavaScriptMode(JavaScriptMode.unrestricted) | |
..setBackgroundColor(const Color(0x00000000)) | |
..setNavigationDelegate(NavigationDelegate( | |
onProgress: (int progress) {}, | |
onPageStarted: (String url) {}, | |
onPageFinished: (String url) {}, | |
onWebResourceError: (WebResourceError error) {}, | |
)) | |
..loadRequest(Uri.parse( | |
"https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture")); | |
initFilePicker(); | |
} | |
initFilePicker() async { | |
if (Platform.isAndroid) { | |
final androidController = (_controller.platform | |
as webview_flutter_android.AndroidWebViewController); | |
await androidController.setOnShowFileSelector(_androidFilePicker); | |
} | |
} | |
/// This method is called when the user tries to upload a file from the webview. | |
/// It will open the file picker and return the selected files. | |
/// If the user cancels the file picker, it will return an empty list. | |
/// | |
/// Returns uri's of the selected files. | |
Future<List<String>> _androidFilePicker( | |
webview_flutter_android.FileSelectorParams params) async { | |
if (params.acceptTypes.any((type) => type == 'image/*')) { | |
final picker = image_picker.ImagePicker(); | |
final photo = | |
await picker.pickImage(source: image_picker.ImageSource.camera); | |
if (photo == null) { | |
return []; | |
} | |
return [Uri.file(photo.path).toString()]; | |
} else if (params.acceptTypes.any((type) => type == 'video/*')) { | |
final picker = image_picker.ImagePicker(); | |
final vidFile = await picker.pickVideo( | |
source: ImageSource.camera, maxDuration: const Duration(seconds: 10)); | |
if (vidFile == null) { | |
return []; | |
} | |
return [Uri.file(vidFile.path).toString()]; | |
} else { | |
try { | |
if (params.mode == | |
webview_flutter_android.FileSelectorMode.openMultiple) { | |
final attachments = | |
await FilePicker.platform.pickFiles(allowMultiple: true); | |
if (attachments == null) return []; | |
return attachments.files | |
.where((element) => element.path != null) | |
.map((e) => File(e.path!).uri.toString()) | |
.toList(); | |
} else { | |
final attachment = await FilePicker.platform.pickFiles(); | |
if (attachment == null) return []; | |
File file = File(attachment.files.single.path!); | |
return [file.uri.toString()]; | |
} | |
} catch (e) { | |
return []; | |
} | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Attach files Iframe [Flutter]"), | |
), | |
body: WebViewWidget(controller: _controller), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment