-
-
Save rohankandwal/c7dd80627fdade779d1e6154657cecf1 to your computer and use it in GitHub Desktop.
/// Read details on https://theprogrammingway.com/flutter-file-upload-using-webview-on-android/ | |
import 'dart:io'; | |
import 'package:file_picker/file_picker.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:webview_flutter/webview_flutter.dart'; | |
import 'package:webview_flutter_android/webview_flutter_android.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
late final WebViewController controller; | |
@override | |
void initState() { | |
controller = WebViewController(); | |
controller.loadRequest( | |
Uri.parse("https://theprogrammingway.com/html-file-upload/")); | |
addFileSelectionListener(); | |
super.initState(); | |
} | |
void addFileSelectionListener() async { | |
if (Platform.isAndroid) { | |
final androidController = controller.platform as AndroidWebViewController; | |
await androidController.setOnShowFileSelector(_androidFilePicker); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return WebViewWidget(controller: controller); | |
} | |
/// Function for file selection from gallery | |
Future<List<String>> _androidFilePicker(FileSelectorParams params) async { | |
final result = await FilePicker.platform.pickFiles(); | |
if (result != null && result.files.single.path != null) { | |
final file = File(result.files.single.path!); | |
return [file.uri.toString()]; | |
} | |
return []; | |
} | |
} |
it shows image selection but then closes. it doesnt get uploaded to the backend web app. mine is django. /// Read details on https://theprogrammingway.com/flutter-file-upload-using-webview-on-android/
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@OverRide
State createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
late final WebViewController controller;
@OverRide
void initState() {
controller = WebViewController();
controller.loadRequest(
Uri.parse("https://pythonanywhere.com/"));
controller.setJavaScriptMode(JavaScriptMode.unrestricted);
addFileSelectionListener();
super.initState();
}
void addFileSelectionListener() async {
if (Platform.isAndroid) {
final androidController = controller.platform as AndroidWebViewController;
await androidController.setOnShowFileSelector(_androidFilePicker);
}
}
@OverRide
Widget build(BuildContext context) {
return WebViewWidget(controller: controller,
);
}
/// Function for file selection from gallery
Future<List> _androidFilePicker(FileSelectorParams params) async {
final result = await FilePicker.platform.pickFiles();
if (result != null && result.files.single.path != null) {
final file = File(result.files.single.path!);
return [file.uri.toString()];
}
return [];
}
}
Is there a way to do the same thing for ios?
Thank you for your code. It works well.