flutter获取Android照片地址,Flutter 获取本地图片并剪切

安装依赖

dependencies:

...

image_picker:

image_cropper

android\app\src\main\AndroidManifest.xml

将UCropActivity添加到AndroidManifest.xml中

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 {

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 _pickImage() async {

imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);

if (imageFile != null) {

setState(() {

state = AppState.picked;

});

}

}

Future _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 _saveImage() async {}

void _clearImage() {

imageFile = null;

setState(() {

state = AppState.free;

});

}

}

更多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值