Flutter 获取本地图片并剪切
flutter 获取 图片 本地 剪切
2023-09-14 08:58:44 时间
安装依赖
dependencies:
...
image_picker:
image_cropper
android\app\src\main\AndroidManifest.xml
将UCropActivity添加到AndroidManifest.xml中
<activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
android\gradle.properties
添加AndroidX兼容
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
lib\main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ImageCropper',
home: MyHomePage(
title: 'ImageCropper',
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({this.title});
@override
_MyHomePageState createState() => _MyHomePageState();
}
enum AppState {
free,
picked,
cropped,
}
class _MyHomePageState extends State<MyHomePage> {
AppState state;
File imageFile;
@override
void initState() {
super.initState();
state = AppState.free;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: imageFile != null ? Image.file(imageFile) : Container(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (state == AppState.free)
_pickImage();
else if (state == AppState.picked)
_cropImage();
else if (state == AppState.cropped) _clearImage();
},
child: _buildButtonIcon(),
),
);
}
Widget _buildButtonIcon() {
if (state == AppState.free)
return Icon(Icons.add);
else if (state == AppState.picked)
return Icon(Icons.crop);
else if (state == AppState.cropped)
return Icon(Icons.clear);
else
return Container();
}
Future<Null> _pickImage() async {
imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
if (imageFile != null) {
setState(() {
state = AppState.picked;
});
}
}
Future<Null> _cropImage() async {
File croppedFile = await ImageCropper.cropImage(
sourcePath: imageFile.path,
toolbarTitle: 'Cropper',
toolbarColor: Colors.blue,
toolbarWidgetColor: Colors.white,
);
if (croppedFile != null) {
imageFile = croppedFile;
setState(() {
state = AppState.cropped;
});
}
}
Future<Null> _saveImage() async {}
void _clearImage() {
imageFile = null;
setState(() {
state = AppState.free;
});
}
}
更多
相关文章
- Flutter: 获取本地json数据
- Flutter: Dismissible 通过在指示的方向上拖动来解除的Widget
- flutter:适配屏幕(flutter_screenutil: ^5.6.1 / flutter 3.7.0 )
- [Flutter] lib/main.dart:1: Warning: Interpreting this as package URI, 'package:flutter_app/main.dart'.
- Flutter是跨平台开发终极之选吗?Android开发该如何快速上手Flutter?
- Flutter原理 flutter架构、flutter UI系统、BuildContext、Widget与Element、命中测试hitTest、flutter显示流程分析
- flutter 图片切成圆形
- flutter enum枚举型
- flutter URL有方法获取其中参数
- flutter 获取系统默认铃声 flutter_ringtone_player 3.2.0
- flutter 获取当前时间后5分钟的时间戳示例代码:
- flutter 获取直播时长
- flutter 字节和模型的转换
- flutter 弹窗用跳转的方式实现背景全透明
- flutter path_provider: ^2.0.10可以获取临时目录
- Flutter Unable to ‘pub upgrade‘ flutter tool
- Flutter获取packageName和versionCode
- Flutter组件学习之ClipOval圆形裁剪——筑梦之路