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}");
    });
相关推荐
山屿落星辰20 分钟前
Flutter 高级特性实战:动画、自定义绘制、平台通道与 Web 优化
前端·flutter
程序软件分享1 小时前
2026旗舰版 Java+Flutter 期货微交易系统源码全开源多语言平台
flutter·交易所源码·微盘源码·微交易源码
飞龙14775657467502 小时前
Flutter 安全存储插件全面解析:从入门到进阶
flutter
带带弟弟学爬虫__2 小时前
dyAPP数据采集-个人主页、发布、搜索、评论
服务器·python·算法·flutter·java-ee·django
icc_tips3 小时前
Flutter runAppAsync() 详解:干净的异步应用启动
前端·flutter
恋猫de小郭4 小时前
Android 发布全新性能分析器,实用性和性能大升级
android·前端·flutter
恋猫de小郭5 小时前
Flutter 3.44 发布啦,超级大版本更新!!!
android·flutter·ios
张3蜂5 小时前
Flutter macOS 安装文档
flutter·macos
Swuagg5 小时前
Flutter 架构实践:从 0 到 1 构建智能眼镜应用
flutter·架构
天天开发5 小时前
Flutter开发者该掌握的iOS隐私审核政策
flutter·ios·cocoa