Skip to content

Instantly share code, notes, and snippets.

@pmatatias
Last active May 28, 2024 07:57
Show Gist options
  • Save pmatatias/44dd7cabb7b26b977331dc5537325805 to your computer and use it in GitHub Desktop.
Save pmatatias/44dd7cabb7b26b977331dc5537325805 to your computer and use it in GitHub Desktop.
Attach file , and open camera from iframe or webview in flutter app
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),
);
}
}
@pmatatias
Copy link
Author

glad if that helps 👍

@CovenantJunior
Copy link

Fabulous, big thanks 🫶🏻

@do-one-thing-to-well
Copy link

big thanks

@maryannemm
Copy link

thank you so much this code works for me

@boris008
Copy link

Hello, for me it freezes on white screen with Flutter logo after run app in emulator. Can you help me please?

@pmatatias
Copy link
Author

sorry for late reply, @boris008
is there any error message in your terminal?

and also make sure you had add the permission to your androitManifest.xml

@boris008
Copy link

Hello, thanks for reply. It was my fault, I didn't know how to run the app properly. Now it works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment