Rework file upload
This commit is contained in:
parent
ca37d4094e
commit
757a1f1c46
|
|
@ -6,7 +6,7 @@ buildscript {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:3.5.0'
|
classpath 'com.android.tools.build:gradle:3.6.2'
|
||||||
classpath 'com.google.gms:google-services:4.3.2'
|
classpath 'com.google.gms:google-services:4.3.2'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
|
||||||
|
|
|
||||||
|
|
@ -116,8 +116,7 @@ class _ExpenseViewState extends State<ExpenseView>
|
||||||
.getImage(source: ImageSource.camera);
|
.getImage(source: ImageSource.camera);
|
||||||
if (image != null && image.path != null) {
|
if (image != null && image.path != null) {
|
||||||
final bytes = await image.readAsBytes();
|
final bytes = await image.readAsBytes();
|
||||||
multipartFile = MultipartFile.fromBytes(
|
multipartFile = MultipartFile.fromBytes('file', bytes,
|
||||||
'file', bytes,
|
|
||||||
filename: image.path.split('/').last);
|
filename: image.path.split('/').last);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import 'package:invoiceninja_flutter/utils/files.dart';
|
||||||
import 'package:invoiceninja_flutter/utils/formatting.dart';
|
import 'package:invoiceninja_flutter/utils/formatting.dart';
|
||||||
import 'package:invoiceninja_flutter/utils/localization.dart';
|
import 'package:invoiceninja_flutter/utils/localization.dart';
|
||||||
import 'package:invoiceninja_flutter/utils/platforms.dart';
|
import 'package:invoiceninja_flutter/utils/platforms.dart';
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
|
||||||
class CompanyDetails extends StatefulWidget {
|
class CompanyDetails extends StatefulWidget {
|
||||||
const CompanyDetails({
|
const CompanyDetails({
|
||||||
|
|
@ -426,7 +427,10 @@ class _CompanyDetailsState extends State<CompanyDetails>
|
||||||
label: localization.uploadLogo,
|
label: localization.uploadLogo,
|
||||||
iconData: Icons.cloud_upload,
|
iconData: Icons.cloud_upload,
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final multipartFile = await pickFile(fileIndex: 'company_logo');
|
final multipartFile = await pickFile(
|
||||||
|
fileIndex: 'company_logo',
|
||||||
|
fileType: FileType.image,
|
||||||
|
);
|
||||||
if (multipartFile != null) {
|
if (multipartFile != null) {
|
||||||
viewModel.onUploadLogo(context, multipartFile);
|
viewModel.onUploadLogo(context, multipartFile);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,33 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
Future<MultipartFile> pickFile({String fileIndex = 'file'}) async {
|
Future<MultipartFile> pickFile(
|
||||||
|
{String fileIndex,
|
||||||
|
FileType fileType,
|
||||||
|
List<String> allowedExtensions}) async {
|
||||||
if (kIsWeb) {
|
if (kIsWeb) {
|
||||||
return _pickFile(fileIndex: fileIndex);
|
return _pickFile(
|
||||||
|
fileIndex: fileIndex,
|
||||||
|
fileType: fileType,
|
||||||
|
allowedExtensions: allowedExtensions,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
final permissionStatus = await [Permission.photos].request();
|
final permissionType = fileType == FileType.image && Platform.isIOS
|
||||||
|
? Permission.photos
|
||||||
|
: Permission.storage;
|
||||||
|
final permissionStatus = await [permissionType].request();
|
||||||
final permission =
|
final permission =
|
||||||
permissionStatus[Permission.photos] ?? PermissionStatus.undetermined;
|
permissionStatus[permissionType] ?? PermissionStatus.undetermined;
|
||||||
if (permission == PermissionStatus.granted) {
|
if (permission == PermissionStatus.granted) {
|
||||||
return _pickFile(fileIndex: fileIndex);
|
return _pickFile(
|
||||||
|
fileIndex: fileIndex,
|
||||||
|
fileType: fileType,
|
||||||
|
allowedExtensions: allowedExtensions,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
openAppSettings();
|
openAppSettings();
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -19,8 +35,16 @@ Future<MultipartFile> pickFile({String fileIndex = 'file'}) async {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<MultipartFile> _pickFile({String fileIndex}) async {
|
Future<MultipartFile> _pickFile(
|
||||||
final result = await FilePicker.platform.pickFiles();
|
{String fileIndex,
|
||||||
|
FileType fileType,
|
||||||
|
List<String> allowedExtensions}) async {
|
||||||
|
final result = await FilePicker.platform.pickFiles(
|
||||||
|
withData: true,
|
||||||
|
type: fileType ?? FileType.any,
|
||||||
|
allowedExtensions: allowedExtensions ?? [],
|
||||||
|
allowCompression: true,
|
||||||
|
);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
final file = result.files.single;
|
final file = result.files.single;
|
||||||
return MultipartFile.fromBytes(fileIndex ?? 'file', file.bytes,
|
return MultipartFile.fromBytes(fileIndex ?? 'file', file.bytes,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue