Flutter图片裁剪插件simple_image_crop_plus的使用
Flutter 图片裁剪插件 simple_image_crop_plus 的使用
simple_image_crop_plus
是一个简单易用的 Flutter 插件,可以在 iOS 和 Android 上进行图片裁剪。以下是如何在 Flutter 应用程序中使用该插件的详细步骤。
安装
首先,在 pubspec.yaml
文件中添加 simple_image_crop_plus
依赖:
dependencies:
simple_image_crop_plus: ^版本号
然后运行 flutter pub get
来获取依赖。
使用
创建一个加载和编辑图像的小部件
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:simple_image_crop_plus/simple_image_crop_plus.dart';
class MyHomeRoute extends StatefulWidget {
[@override](/user/override)
_MyHomeRouteState createState() => new _MyHomeRouteState();
}
class _MyHomeRouteState extends State<MyHomeRoute> {
final cropKey = GlobalKey<ImgCropState>();
Future<void> getImage(_sheetType type) async {
var image = await ImagePicker().pickImage(
source: type == _sheetType.gallery
? ImageSource.gallery
: ImageSource.camera);
if (image == null) return;
Navigator.of(context).pop();
Navigator.of(context).pushNamed('crop_page', arguments: {'image': image});
}
void _showActionSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min, // 设置最小的弹出
children: <Widget>[
new ListTile(
leading: new Icon(Icons.photo_camera),
title: new Text("相机拍照"),
onTap: () async {
getImage(_sheetType.camera);
},
),
new ListTile(
leading: new Icon(Icons.photo_library),
title: new Text("相册选择"),
onTap: () async {
getImage(_sheetType.gallery);
},
),
],
),
);
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('选择图片'),
),
floatingActionButton: FloatingActionButton(
onPressed: _showActionSheet,
tooltip: '增加',
child: Icon(Icons.add),
),
);
}
}
生成裁剪后的图像
class SimpleCropRoute extends StatefulWidget {
[@override](/user/override)
_SimpleCropRouteState createState() => _SimpleCropRouteState();
}
class _SimpleCropRouteState extends State<SimpleCropRoute> {
final cropKey = GlobalKey<ImgCropState>();
Future<void> showImage(BuildContext context, File file) async {
new FileImage(file)
.resolve(new ImageConfiguration())
.addListener(ImageStreamListener((ImageInfo info, bool _) {
print('-------------------------------------------$info');
}));
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
'当前截图:',
style: TextStyle(
fontFamily: 'Roboto',
fontWeight: FontWeight.w300,
color: Theme.of(context).primaryColor,
letterSpacing: 1.1),
),
content: Image.file(file));
});
}
[@override](/user/override)
Widget build(BuildContext context) {
final Map args = ModalRoute.of(context)?.settings.arguments as Map;
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text(
'缩放和裁剪',
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
leading: new IconButton(
icon:
new Icon(Icons.navigate_before, color: Colors.black, size: 40),
onPressed: () => Navigator.of(context).pop(),
),
),
body: Center(
child: ImgCrop(
key: cropKey,
// chipRadius: 100,
// chipShape: 'rect',
maximumScale: 3,
image: FileImage(args['image']),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
final croppedFile =
await cropKey.currentState!.cropCompleted(args['image'], pictureQuality: 600);
showImage(context, croppedFile);
},
tooltip: '增加',
child: Text('裁剪'),
));
}
}
更多关于Flutter图片裁剪插件simple_image_crop_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter图片裁剪插件simple_image_crop_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用simple_image_crop_plus
插件来实现图片裁剪功能的代码示例。
首先,确保你已经在pubspec.yaml
文件中添加了simple_image_crop_plus
依赖:
dependencies:
flutter:
sdk: flutter
simple_image_crop_plus: ^x.y.z # 替换为最新版本号
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter项目中,你可以按照以下步骤使用simple_image_crop_plus
插件:
- 导入必要的包:
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:simple_image_crop_plus/simple_image_crop_plus.dart';
注意:simple_image_crop_plus
通常与image_picker
插件一起使用,以便从设备中选择图片。
- 定义裁剪和选择图片的函数:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
File? _imageFile;
CropController? _cropController;
@override
void initState() {
super.initState();
_cropController = CropController();
}
@override
void dispose() {
_cropController?.dispose();
super.dispose();
}
Future<void> _pickImage() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imageFile = File(pickedFile.path);
});
// 打开裁剪页面
final croppedFile = await SimpleImageCrop.cropImage(
sourcePath: pickedFile.path,
aspectRatioPresets: [
CropAspectRatioPreset.square,
CropAspectRatioPreset.ratio3x2,
CropAspectRatioPreset.original,
CropAspectRatioPreset.ratio4x3,
CropAspectRatioPreset.ratio16x9
],
androidUiSettings: AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: Colors.deepOrange,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.original,
lockAspectRatio: false),
iosUiSettings: IOSUiSettings(
minimumAspectRatio: 1.0,
),
);
if (croppedFile != null) {
setState(() {
_imageFile = croppedFile;
});
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Image Crop Example'),
),
body: Center(
child: _imageFile == null
? ElevatedButton(
onPressed: _pickImage,
child: Text('Pick Image'),
)
: Image.file(_imageFile!),
),
),
);
}
}
- 运行应用:
将上述代码放入你的主dart
文件中(通常是lib/main.dart
),然后运行你的Flutter应用。
这个示例展示了如何从设备的图库中选择一张图片,并使用simple_image_crop_plus
插件进行裁剪。裁剪后的图片将被显示在界面上。
注意:
CropController
用于管理裁剪操作的状态,但在简单的用例中可能不需要显式使用。aspectRatioPresets
和UI设置(如androidUiSettings
和iosUiSettings
)允许你自定义裁剪界面的外观和行为。- 在实际使用中,你可能需要处理更多的错误检查和状态管理,以确保应用的健壮性。
希望这能帮助你理解如何在Flutter项目中使用simple_image_crop_plus
插件进行图片裁剪。