From 71085788cda71c428fc0e3651c880cc29c0e85ec Mon Sep 17 00:00:00 2001 From: sindhu Date: Mon, 15 Jan 2024 08:24:14 +0700 Subject: [PATCH] step 6 : add file picker not implemented --- .../app/src/main/res/xml/filepaths.xml | 4 + app_petty_cash/lib/app/route.dart | 15 +++ app_petty_cash/lib/main.dart | 3 +- .../test_file_picker/test_file_picker.dart | 93 +++++++++++++++++++ app_petty_cash/pubspec.lock | 8 ++ app_petty_cash/pubspec.yaml | 1 + 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app_petty_cash/android/app/src/main/res/xml/filepaths.xml create mode 100644 app_petty_cash/lib/screen/test_file_picker/test_file_picker.dart diff --git a/app_petty_cash/android/app/src/main/res/xml/filepaths.xml b/app_petty_cash/android/app/src/main/res/xml/filepaths.xml new file mode 100644 index 0000000..56f25df --- /dev/null +++ b/app_petty_cash/android/app/src/main/res/xml/filepaths.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app_petty_cash/lib/app/route.dart b/app_petty_cash/lib/app/route.dart index 4e18eba..374826e 100644 --- a/app_petty_cash/lib/app/route.dart +++ b/app_petty_cash/lib/app/route.dart @@ -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 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( diff --git a/app_petty_cash/lib/main.dart b/app_petty_cash/lib/main.dart index 6a68a03..6d696b1 100644 --- a/app_petty_cash/lib/main.dart +++ b/app_petty_cash/lib/main.dart @@ -31,7 +31,8 @@ class MyApp extends StatelessWidget { debugShowCheckedModeBanner: false, // initialRoute: loginRoute, // initialRoute: splashScreen, - initialRoute: transaksiRoute, + // initialRoute: transaksiRoute, + initialRoute: testFilePickerRoute, onGenerateRoute: AppRoute.generateRoute, ); } diff --git a/app_petty_cash/lib/screen/test_file_picker/test_file_picker.dart b/app_petty_cash/lib/screen/test_file_picker/test_file_picker.dart new file mode 100644 index 0000000..81f58f3 --- /dev/null +++ b/app_petty_cash/lib/screen/test_file_picker/test_file_picker.dart @@ -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 createState() => _TestFilePickerState(); +} + +class _TestFilePickerState extends State { + List? files = []; + + Future 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 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 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), + ), + ], + ), + ], + ), + ), + ); + } +} diff --git a/app_petty_cash/pubspec.lock b/app_petty_cash/pubspec.lock index bdfc21c..f4dbc69 100644 --- a/app_petty_cash/pubspec.lock +++ b/app_petty_cash/pubspec.lock @@ -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: diff --git a/app_petty_cash/pubspec.yaml b/app_petty_cash/pubspec.yaml index 8c96efa..39dc7b2 100644 --- a/app_petty_cash/pubspec.yaml +++ b/app_petty_cash/pubspec.yaml @@ -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: