Flutter 图片选取及裁剪

在开发项目里修改用户头像的功能,涉及到图片选取及裁剪,基本实现步骤如下:

1、pubspec.yaml 添加 image_picker: ^1.0.1 image_cropper: ^4.0.1:

复制代码
dependencies:
  image_picker: ^1.0.1
  image_cropper: ^4.0.1
  flutter:
    sdk: flutter

2、AndroidManifest.xml 添加:

XML 复制代码
<activity
            android:name="com.yalantis.ucrop.UCropActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

3、封装工具类 image_picker_helper.dart ,代码如下:

Dart 复制代码
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

/// @description: 从相册或照相机里选取图片的工具,带裁剪功能
class ImagePickerHelper {
  BuildContext buildContext;

  ImagePickerHelper(this.buildContext);

  void pickImage(ImageSource source, ImagePickerCallback? callback) {
    ImagePicker().pickImage(source: source).then((image) {
      if (image == null) return;
      if (callback == null) return;
      callback.call(image);
    }).onError((error, stackTrace) {
      debugPrint("获取图片失败:$error");
    });
  }

  void cropImage(File originalImage, ImageCropCallback? callback) {
    Future<CroppedFile?> future = ImageCropper().cropImage(
      sourcePath: originalImage.path,
      maxWidth: 100,
      maxHeight: 100,
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: '',
            toolbarColor: Colors.white,
            statusBarColor: Colors.white,
            toolbarWidgetColor: Colors.green,
            initAspectRatio: CropAspectRatioPreset.square,
            lockAspectRatio: false),
        IOSUiSettings(
          title: '',
        ),
        WebUiSettings(
          context: buildContext,
        ),
      ],
    );
    future.then((value) {
      debugPrint("_cropImage path:${value == null ? "" : value.path}");
      if (value == null) return;
      if (callback == null) return;
      callback.call(value);
    }).onError((error, stackTrace) {
      debugPrint("裁剪图片失败:$error");
    });
  }

  void pickWithCropImage(ImageSource source, ImageCropCallback? callback) {
    pickImage(source, (xFile) {
      cropImage(File(xFile.path), callback);
    });
  }
}

typedef ImagePickerCallback = void Function(XFile xFile);
typedef ImageCropCallback = void Function(CroppedFile croppedFile);

4、使用示例:

Dart 复制代码
//ImageSource.camera 照相机 或 ImageSource.gallery 相册
    ImagePickerHelper(context).pickWithCropImage(ImageSource.gallery,
        (croppedFile) {
      //获取到剪切的文件路径,进行相关的操作
      debugPrint("croppedFile:${croppedFile.path}");
    });
相关推荐
fouryears_234175 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
LinXunFeng11 小时前
Flutter - 详情页 TabBar 与模块联动?秒了!
前端·flutter·开源
阅文作家助手开发团队_山神14 小时前
第三章: 解决Android iPad蓝牙键盘联想词UI不跟随光标问题
flutter
阅文作家助手开发团队_山神14 小时前
第四章:Flutter自定义Engine本地依赖与打包流程
前端·flutter
程序员老刘15 小时前
Flutter 3.35 更新要点解析
flutter·ai编程·客户端
阅文作家助手开发团队_山神17 小时前
第一章: Mac Flutter Engine开发准备工作
前端·flutter
EmmaGuo201518 小时前
flutter3.7.12版本设置TextField的contextMenuBuilder的文字颜色
前端·flutter
鹏多多.20 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
来来走走1 天前
Flutter开发 网络请求
android·flutter
SoaringHeart2 天前
Flutter进阶:高内存任务的动态并发执行完美实现
前端·flutter