Browse Source

compress when pick image from gallery

master
daivph 5 years ago
parent
commit
2f49e5a282
6 changed files with 79 additions and 22 deletions
  1. +29
    -0
      ios/Podfile.lock
  2. +20
    -21
      lib/presentation/custom_widgets/widget_media_picker.dart
  3. +20
    -0
      lib/presentation/screens/actions/util_action.dart
  4. +2
    -1
      lib/utils/const_string.dart
  5. +7
    -0
      pubspec.lock
  6. +1
    -0
      pubspec.yaml

+ 29
- 0
ios/Podfile.lock View File

@@ -85,6 +85,10 @@ PODS:
- Protobuf (>= 3.9.2, ~> 3.9)
- FLAnimatedImage (1.0.12)
- Flutter (1.0.0)
- flutter_image_compress (0.0.1):
- Flutter
- Mantle
- SDWebImageWebPCoder
- FMDB (2.7.5):
- FMDB/standard (= 2.7.5)
- FMDB/standard (2.7.5)
@@ -107,6 +111,18 @@ PODS:
- GoogleUtilities/Logger
- GoogleUtilities/UserDefaults (6.7.2):
- GoogleUtilities/Logger
- libwebp (1.1.0):
- libwebp/demux (= 1.1.0)
- libwebp/mux (= 1.1.0)
- libwebp/webp (= 1.1.0)
- libwebp/demux (1.1.0):
- libwebp/webp
- libwebp/mux (1.1.0):
- libwebp/demux
- libwebp/webp (1.1.0)
- Mantle (2.1.4):
- Mantle/extobjc (= 2.1.4)
- Mantle/extobjc (2.1.4)
- MTBBarcodeScanner (5.0.11)
- nanopb (1.30906.0):
- nanopb/decode (= 1.30906.0)
@@ -125,6 +141,9 @@ PODS:
- SDWebImageFLPlugin (0.4.0):
- FLAnimatedImage (>= 1.0.11)
- SDWebImage/Core (~> 5.6)
- SDWebImageWebPCoder (0.6.1):
- libwebp (~> 1.0)
- SDWebImage/Core (~> 5.7)
- shared_preferences (0.0.1):
- Flutter
- sqflite (0.0.1):
@@ -141,6 +160,7 @@ DEPENDENCIES:
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`)
- Flutter (from `Flutter`)
- flutter_image_compress (from `.symlinks/plugins/flutter_image_compress/ios`)
- package_info (from `.symlinks/plugins/package_info/ios`)
- path_provider (from `.symlinks/plugins/path_provider/ios`)
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
@@ -163,12 +183,15 @@ SPEC REPOS:
- FMDB
- GoogleDataTransport
- GoogleUtilities
- libwebp
- Mantle
- MTBBarcodeScanner
- nanopb
- PromisesObjC
- Protobuf
- SDWebImage
- SDWebImageFLPlugin
- SDWebImageWebPCoder
- SwiftProtobuf

EXTERNAL SOURCES:
@@ -184,6 +207,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/firebase_messaging/ios"
Flutter:
:path: Flutter
flutter_image_compress:
:path: ".symlinks/plugins/flutter_image_compress/ios"
package_info:
:path: ".symlinks/plugins/package_info/ios"
path_provider:
@@ -213,9 +238,12 @@ SPEC CHECKSUMS:
FirebaseMessaging: 29543feb343b09546ab3aa04d008ee8595b43c44
FLAnimatedImage: 4a0b56255d9b05f18b6dd7ee06871be5d3b89e31
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_image_compress: 082f8daaf6c1b0c9fe798251c750ef0ecd98d7ae
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
GoogleDataTransport: e85fb700c9b027079ce182c3d08e12e0f9618bb4
GoogleUtilities: 7f2f5a07f888cdb145101d6042bc4422f57e70b3
libwebp: 946cb3063cea9236285f7e9a8505d806d30e07f3
Mantle: bf2e1b464f82465b9074e9de970739a2615e093a
MTBBarcodeScanner: f453b33c4b7dfe545d8c6484ed744d55671788cb
nanopb: 59317e09cf1f1a0af72f12af412d54edf52603fc
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
@@ -224,6 +252,7 @@ SPEC CHECKSUMS:
Protobuf: 3dac39b34a08151c6d949560efe3f86134a3f748
SDWebImage: a990c053fff71e388a10f3357edb0be17929c9c5
SDWebImageFLPlugin: 6c2295fb1242d44467c6c87dc5db6b0a13228fd8
SDWebImageWebPCoder: d0dac55073088d24b2ac1b191a71a8f8d0adac21
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
sqflite: 4001a31ff81d210346b500c55b17f4d6c7589dd0
SwiftProtobuf: 4ef85479c18ca85b5482b343df9c319c62bda699

+ 20
- 21
lib/presentation/custom_widgets/widget_media_picker.dart View File

@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:farm_tpf/custom_model/Media.dart';
import 'package:farm_tpf/presentation/custom_widgets/camera_helper.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_show_video.dart';
import 'package:farm_tpf/presentation/screens/actions/util_action.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_common.dart';
import 'package:farm_tpf/utils/const_string.dart';
@@ -141,24 +142,24 @@ class _WidgetMediaPickerState extends State<WidgetMediaPicker> {
await FilePicker.platform.pickFiles(type: FileType.image);

if (result != null) {
String filePath = result.files.single.path;
var lengthFileInBytes = result.files.single.size * 1000;
if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large,
"Kích thước: $lengthFileInBytes bytes",
snackPosition: SnackPosition.BOTTOM);
} else {
Media newMedia = Media()
..isVideo = false
..isServerFile = false
..pathFile = filePath;
currentItems.add(newMedia);
files.add(filePath);
BlocProvider.of<MediaHelperBloc>(context)
..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files);
}
print("file size: $lengthFileInBytes");
UtilAction.compressImage(File(result.files.single.path))
.then((compressFile) async {
var lengthFileInBytes = await compressFile.length();
if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large, label_file_size_suggest,
snackPosition: SnackPosition.BOTTOM);
} else {
Media newMedia = Media()
..isVideo = false
..isServerFile = false
..pathFile = compressFile.path;
currentItems.add(newMedia);
files.add(compressFile.path);
BlocProvider.of<MediaHelperBloc>(context)
..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files);
}
});
}
}),
CupertinoDialogAction(
@@ -172,8 +173,7 @@ class _WidgetMediaPickerState extends State<WidgetMediaPicker> {
String filePath = result.files.single.path;
var lengthFileInBytes = result.files.single.size * 1000;
if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large,
"Kích thước: $lengthFileInBytes bytes",
Get.snackbar(label_file_to_large, label_file_size_suggest,
snackPosition: SnackPosition.BOTTOM);
} else {
Media newMedia = Media()
@@ -186,7 +186,6 @@ class _WidgetMediaPickerState extends State<WidgetMediaPicker> {
..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files);
}
print("file size: $lengthFileInBytes");
}
}),
CupertinoDialogAction(

+ 20
- 0
lib/presentation/screens/actions/util_action.dart View File

@@ -1,7 +1,12 @@
import 'dart:io';

import 'package:farm_tpf/custom_model/Media.dart';
import 'package:farm_tpf/utils/const_common.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:mime/mime.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:path/path.dart' as path;

class UtilAction {
static Future<List<Media>> cacheFiles(String existedMedias) async {
@@ -51,4 +56,19 @@ class UtilAction {
}
return result;
}

static Future<File> compressImage(File file) async {
final dir = await path_provider.getTemporaryDirectory();
final fileName = path.basenameWithoutExtension(file.path);
final targetPath = dir.absolute.path + "/$fileName.jpg";
final imgFile = await compressAndGetFile(file, targetPath);
return imgFile;
}

static Future<File> compressAndGetFile(File file, String targetPath) async {
final result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, targetPath,
minWidth: 1024, minHeight: 1024);
return result;
}
}

+ 2
- 1
lib/utils/const_string.dart View File

@@ -36,7 +36,8 @@ const String label_error_get_data = "Lỗi tải dữ liệu";
const String label_update_success = "Cập nhật thành công";
const String label_add_success = "Thêm thành công";

const String label_file_to_large = "Kích thước hình/video quá lớn";
const String label_file_to_large = "Kích thước tập tin quá lớn";
const String label_file_size_suggest = "Vui lòng chọn tập tin nhỏ hơn 1024 Kb";

//Exception
const String exception_common = "Đã có lỗi xảy ra";

+ 7
- 0
pubspec.lock View File

@@ -321,6 +321,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.4.0"
flutter_image_compress:
dependency: "direct main"
description:
name: flutter_image_compress
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:

+ 1
- 0
pubspec.yaml View File

@@ -47,6 +47,7 @@ dependencies:
flutter_cache_manager: ^1.4.2

mime: ^0.9.7
flutter_image_compress: ^0.7.0

dev_dependencies:
flutter_test:

Loading…
Cancel
Save