裁剪圖片
這里推薦flutter 插件:simple_image_crop。用法非常簡單虱黄,支持圓形和方形選區(qū)悦即,支持放大縮小,完美兼容iOS 和安卓: packages地址
創(chuàng)建一個編輯圖片的組件:
final imgCropKey = GlobalKey<CropState>();
Widget _buildCropImage() {
return Container(
color: Colors.black,
child: ImgCrop(
key: cropKey,
chipRadius: 150, // 裁剪區(qū)域半徑
chipShape: 'circle', // 裁剪區(qū)域類型 支持圓形"circle" 和 方形"rect"
image: Image.file(imageFile), // 傳入圖片資源
),
);
}
生成裁剪后的圖片:
- 選擇圖片推薦使用
image-picker
插件, 圖片也許是上個頁面?zhèn)鬟^來的(當(dāng)然也可以是其他任意圖片資源):
final Map args = ModalRoute.of(context).settings.arguments
- 一個簡單的異步函數(shù)獲取裁剪后的圖片:
crop.cropCompleted('傳入的圖片資源', {pictureQuality: '圖片質(zhì)量 int '})
floatingActionButton: FloatingActionButton(
onPressed: () async {
final crop = cropKey.currentState;
final croppedFile =
await crop.cropCompleted(args['image'], pictureQuality: 900);
// show you croppedFile ……
showImage(context, croppedFile);
},
上傳裁剪后的圖片
上傳圖片同樣非常簡單橱乱,使用dio 庫直接建立
formData
數(shù)據(jù)上傳至服務(wù)器:
Future uploadImage(BuildContext context, File file) async {
final path = file.path;
final name = path.substring(path.lastIndexOf("/") + 1, path.length);
var args = FormData.fromMap(
{'file': await MultipartFile.fromFile(path, filename: name)});
await net.request(requestUrl, args: args);
}
非常簡單 ! 對你有幫助的話記得點(diǎn)亮小星星 github地址