diff --git a/lib/ui/settings/import_export.dart b/lib/ui/settings/import_export.dart index 7e7d2ca22..7848922c6 100644 --- a/lib/ui/settings/import_export.dart +++ b/lib/ui/settings/import_export.dart @@ -1,5 +1,9 @@ +import 'dart:typed_data'; + +import 'package:file_picker/file_picker.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:image_picker/image_picker.dart'; import 'package:invoiceninja_flutter/constants.dart'; import 'package:invoiceninja_flutter/data/models/entities.dart'; import 'package:invoiceninja_flutter/ui/app/form_card.dart'; @@ -8,6 +12,9 @@ import 'package:invoiceninja_flutter/ui/app/forms/decorated_form_field.dart'; import 'package:invoiceninja_flutter/ui/settings/import_export_vm.dart'; import 'package:invoiceninja_flutter/utils/localization.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart'; +import 'package:invoiceninja_flutter/utils/web_stub.dart' + if (dart.library.html) 'package:invoiceninja_flutter/utils/web.dart'; +import 'package:permission_handler/permission_handler.dart'; class ImportExport extends StatefulWidget { const ImportExport({ @@ -27,6 +34,8 @@ class _ImportExportState extends State { FocusScopeNode _focusNode; bool autoValidate = false; String _filePath; + String _fileName; + Uint8List _fileBytes; @override void initState() { @@ -82,9 +91,10 @@ class _ImportExportState extends State { ), ), DecoratedFormField( + key: ValueKey(_fileName), enabled: false, label: localization.csvFile, - initialValue: localization.noFileSelected, + initialValue: _fileName ?? localization.noFileSelected, ), SizedBox(height: 20), Row( @@ -94,7 +104,20 @@ class _ImportExportState extends State { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5)), child: Text(localization.selectFile), - onPressed: () {}, + onPressed: () async { + final result = await FilePicker.platform.pickFiles( + type: FileType.custom, + allowedExtensions: ['csv'], + ); + if (result != null) { + setState(() { + final file = result.files.single; + _filePath = file.path; + _fileName = file.name; + _fileBytes = file.bytes; + }); + } + }, ), ), SizedBox(width: kTableColumnGap), @@ -103,9 +126,11 @@ class _ImportExportState extends State { shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5)), child: Text(localization.uploadFile), - onPressed: _filePath == null ? null : () { - // - }, + onPressed: _fileName == null + ? null + : () { + // + }, ), ), ], @@ -117,4 +142,4 @@ class _ImportExportState extends State { ), ); } -} \ No newline at end of file +} diff --git a/pubspec.lock b/pubspec.lock index ffbe25723..16383dbf3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -281,6 +281,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "6.0.0-nullsafety.4" + file_picker: + dependency: "direct main" + description: + name: file_picker + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" firebase: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c60353c60..be1ba8379 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -63,6 +63,7 @@ dependencies: permission_handler: ^5.0.1+1 contacts_service: ^0.4.6 extended_image: 1.3.1-dev #TODO remove + file_picker: ^2.1.1 dependency_overrides: # https://github.com/flutter/flutter/issues/70433#issuecomment-727154345