Created
October 24, 2019 03:00
-
-
Save CaiJingLong/0dcad0d707670428cd18333ed6be76d8 to your computer and use it in GitHub Desktop.
Example of image_editor and extended_image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:typed_data'; | |
import 'package:extended_image/extended_image.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:image_editor/image_editor.dart'; | |
import 'const/resource.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
GlobalKey<ExtendedImageEditorState> editorKey = GlobalKey(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Column( | |
children: <Widget>[ | |
AspectRatio( | |
child: ExtendedImage.asset( | |
R.ASSETS_123_JPG, | |
extendedImageEditorKey: editorKey, | |
mode: ExtendedImageMode.editor, | |
fit: BoxFit.contain, | |
initEditorConfigHandler: (state) { | |
return EditorConfig( | |
maxScale: 8.0, | |
cropRectPadding: EdgeInsets.all(20.0), | |
hitTestSize: 20.0, | |
cropAspectRatio: 1, | |
); | |
}, | |
), | |
aspectRatio: 1, | |
), | |
Expanded(child: Container()), | |
Container( | |
height: 50, | |
child: Row( | |
children: <Widget>[ | |
_buildIconButton(Icons.rotate_left, _rotateLeft), | |
_buildIconButton(Icons.rotate_left, _rotateRight), | |
_buildIconButton(Icons.flip, _flipHor), | |
_buildIconButton(Icons.check, _crop), | |
], | |
), | |
), | |
], | |
), | |
); | |
} | |
Widget _buildIconButton(IconData iconData, Function onTap) { | |
return Expanded( | |
child: IconButton( | |
icon: Icon(iconData), | |
onPressed: onTap, | |
), | |
); | |
} | |
_rotateLeft() { | |
editorKey.currentState.rotate(right: false); | |
} | |
_rotateRight() { | |
editorKey.currentState.rotate(); | |
} | |
_flipHor() { | |
editorKey.currentState.flip(); | |
} | |
_crop() async { | |
final state = editorKey.currentState; | |
final cropRect = state.getCropRect(); | |
final action = state.editAction; | |
final rotateAngle = action.rotateAngle.toInt(); | |
final flipHorizontal = action.flipY; | |
final flipVertical = action.flipX; | |
final img = state.rawImageData; | |
ImageEditorOption option = ImageEditorOption(); | |
if (action.needCrop) option.addOption(ClipOption.fromRect(cropRect)); | |
if (action.needFlip) | |
option.addOption( | |
FlipOption(horizontal: flipHorizontal, vertical: flipVertical)); | |
if (action.hasRotateAngle) option.addOption(RotateOption(rotateAngle)); | |
final start = DateTime.now(); | |
final result = await ImageEditor.editImage( | |
image: img, | |
imageEditorOption: option, | |
); | |
print("${DateTime.now().difference(start)} :total time"); | |
showPreviewDialog(result); | |
} | |
void showPreviewDialog(Uint8List image) { | |
showDialog( | |
context: context, | |
builder: (ctx) => GestureDetector( | |
onTap: () => Navigator.pop(context), | |
child: Container( | |
color: Colors.grey.withOpacity(0.5), | |
child: Center( | |
child: SizedBox.fromSize( | |
size: Size.square(200), | |
child: Container( | |
color: Colors.white, | |
child: Image.memory(image), | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment