| | |
| | | //图片工具 |
| | | import 'package:flutter/material.dart'; |
| | | import '../../utils/ui_constant.dart'; |
| | | import 'package:image_cropper/image_cropper.dart'; |
| | | import 'dart:io'; |
| | | |
| | | import 'package:dio/dio.dart'; |
| | | import 'package:flutter/material.dart'; |
| | | import 'package:image_cropper/image_cropper.dart'; |
| | | import 'package:image_gallery_saver/image_gallery_saver.dart'; |
| | | import 'package:image_picker/image_picker.dart'; |
| | | import 'package:path_provider/path_provider.dart'; |
| | | import 'package:permission_handler/permission_handler.dart'; |
| | | |
| | | class ImageUtil{ |
| | | import '../../utils/ui_constant.dart'; |
| | | import 'config_util.dart'; |
| | | import 'encrypt_util.dart'; |
| | | import 'permission_util.dart'; |
| | | import 'share_utils.dart'; |
| | | import 'ui_utils.dart'; |
| | | |
| | | class ImageUtil { |
| | | //选择并裁剪图片 |
| | | static Future<File?> selectAndCropImage() async{ |
| | | static Future<File?> selectAndCropImage() async { |
| | | final ImagePicker _picker = ImagePicker(); |
| | | final XFile? image = await _picker.pickImage(source: ImageSource.gallery); |
| | | ImageCropper cropper=ImageCropper(); |
| | | ImageCropper cropper = ImageCropper(); |
| | | File? croppedFile = await cropper.cropImage( |
| | | cropStyle:CropStyle.circle, |
| | | cropStyle: CropStyle.circle, |
| | | sourcePath: image!.path, |
| | | aspectRatioPresets: [ |
| | | CropAspectRatioPreset.square |
| | | ], |
| | | aspectRatioPresets: [CropAspectRatioPreset.square], |
| | | androidUiSettings: const AndroidUiSettings( |
| | | toolbarTitle: 'Cropper', |
| | | toolbarColor: ColorConstant.theme, |
| | |
| | | lockAspectRatio: true), |
| | | iosUiSettings: const IOSUiSettings( |
| | | minimumAspectRatio: 1.0, |
| | | ) |
| | | ); |
| | | )); |
| | | return croppedFile; |
| | | } |
| | | |
| | | //保存图片 |
| | | static saveImg(String url, BuildContext context) { |
| | | Future<Directory?> dir; |
| | | if (Platform.isAndroid) { |
| | | dir = getExternalStorageDirectory(); |
| | | } else { |
| | | dir = getApplicationDocumentsDirectory(); |
| | | } |
| | | dir.then((value) { |
| | | if (value == null) { |
| | | ToastUtil.toast("获取缓存目录失败", context); |
| | | return; |
| | | } |
| | | |
| | | String doc = value.path; |
| | | String path = doc + "/" + EncryptUtil.MD5(url) + ".png"; |
| | | _dowloadImg(url, path, context).then((value) { |
| | | if (value) { |
| | | ToastUtil.toast("保存成功", context); |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | } |
| | | ///分享图片 |
| | | ///type: 1-微信 2-qq 3-新浪 |
| | | static shareImg(String url, int type, BuildContext context) { |
| | | getTemporaryDirectory().then((value) { |
| | | String path = value.path + "/" + EncryptUtil.MD5(url) + ".png"; |
| | | _dowloadImg(url, path, context).then((value) { |
| | | if (value) { |
| | | //开始分享 |
| | | switch (type) { |
| | | case 0: |
| | | ShareUtil.shareImg(context, File(path), SharePlatform.all); |
| | | break; |
| | | case 1: |
| | | ShareUtil.shareImg(context, File(path), SharePlatform.wx); |
| | | break; |
| | | case 2: |
| | | ShareUtil.shareImg(context, File(path), SharePlatform.qq); |
| | | break; |
| | | case 3: |
| | | ShareUtil.shareImg(context, File(path), SharePlatform.sina); |
| | | break; |
| | | } |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | static Future<bool> _dowloadImg( |
| | | String url, String path, BuildContext context) async { |
| | | PermissionStatus status = |
| | | await PermissionUtil.openPermission(Permission.storage, force: true); |
| | | if (status != PermissionStatus.granted) { |
| | | return false; |
| | | } |
| | | |
| | | //下载图片 |
| | | Dio dio = Dio(); |
| | | //设置连接超时时间 |
| | | dio.options.connectTimeout = 20000; |
| | | //设置数据接收超时时间 |
| | | dio.options.receiveTimeout = 20000; |
| | | Response response; |
| | | try { |
| | | response = await dio.download(url, path); |
| | | if (response.statusCode == 200) { |
| | | await ImageGallerySaver.saveFile(path); |
| | | return true; |
| | | } else { |
| | | return false; |
| | | } |
| | | } catch (e) { |
| | | ToastUtil.toast("网络连接失败", context); |
| | | return false; |
| | | } |
| | | } |
| | | } |