step 6 : add file picker not implemented

This commit is contained in:
sindhu
2024-01-15 08:24:14 +07:00
parent 81c9fc6034
commit 71085788cd
6 changed files with 123 additions and 1 deletions

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<external-path name="external_storage_directory" path="." />
</resources>

View File

@@ -4,12 +4,14 @@ import 'package:flutter/material.dart';
import '../screen/login/login_screen.dart';
import '../screen/splash/splash_screen.dart';
import '../screen/test_file_picker/test_file_picker.dart';
const loginRoute = "/loginRoute";
const menuRoute = "/menuRoute";
const splashScreen = "/splashScreen";
const homeRoute = "/homeRoute";
const transaksiRoute = "/transaksiRoute";
const testFilePickerRoute = "/testFilePickerRoute";
class AppRoute {
static Route<dynamic> generateRoute(RouteSettings settings) {
@@ -52,6 +54,19 @@ class AppRoute {
});
}
// test file picker screen
if (settings.name == testFilePickerRoute) {
return MaterialPageRoute(builder: (context) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0,
padding: EdgeInsets.all(0),
),
child: TestFilePicker(),
);
});
}
// default
return MaterialPageRoute(builder: (context) {
return MediaQuery(

View File

@@ -31,7 +31,8 @@ class MyApp extends StatelessWidget {
debugShowCheckedModeBanner: false,
// initialRoute: loginRoute,
// initialRoute: splashScreen,
initialRoute: transaksiRoute,
// initialRoute: transaksiRoute,
initialRoute: testFilePickerRoute,
onGenerateRoute: AppRoute.generateRoute,
);
}

View File

@@ -0,0 +1,93 @@
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
class TestFilePicker extends StatefulWidget {
const TestFilePicker({super.key});
@override
State<TestFilePicker> createState() => _TestFilePickerState();
}
class _TestFilePickerState extends State<TestFilePicker> {
List<PlatformFile>? files = [];
Future<void> openFilePicker() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowMultiple: true,
allowedExtensions: ['pdf', 'doc', 'docx', 'txt'],
);
if (result != null) {
// Handle the result and extract the list of files
setState(() {
files = result.files;
});
} else {
// User canceled the file picking
// Handle accordingly
}
}
Future<void> deleteFile(String filePath) async {
try {
File file = File(filePath);
await file.delete();
setState(() {
files?.removeWhere((PlatformFile element) => element.path == filePath);
});
} catch (e) {
print('Error deleting file: $e');
}
}
Future<void> openSelectedFile(String filePath) async {
try {
await OpenFile.open(filePath);
} catch (e) {
print('Error opening file: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('File Picker Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: openFilePicker,
child: Text('Open File Picker'),
),
SizedBox(height: 20),
if (files != null && files!.isNotEmpty)
Text('Selected Files:')
else
SizedBox(),
for (var file in files ?? [])
Row(
children: [
Text(file.name),
IconButton(
icon: Icon(Icons.delete),
onPressed: () => deleteFile(file.path),
),
IconButton(
icon: Icon(Icons.open_in_browser),
onPressed: () => openSelectedFile(file.path),
),
],
),
],
),
),
);
}
}

View File

@@ -224,6 +224,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.1"
open_file:
dependency: "direct main"
description:
name: open_file
sha256: a5a32d44acb7c899987d0999e1e3cbb0a0f1adebbf41ac813ec6d2d8faa0af20
url: "https://pub.dev"
source: hosted
version: "3.3.2"
path:
dependency: transitive
description:

View File

@@ -46,6 +46,7 @@ dependencies:
permission_handler: ^10.2.0
dropdown_button2: ^2.1.3
file_picker: ^6.1.1
open_file: ^3.3.2
dev_dependencies:
flutter_test: