Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

image crop widget flutter widget to crop images

a flutter widget to crop images. the widget is completely written in dart and has minimal dependencies.

the widget displays the image within the given layout space. on top of the image a transparent rectangular overlay, with handles on each corner, is drawn. the overlay handles can be dragged by touch to adjust the cropping area.

crop images

by calling the cropimage() method on the widget’s state object, the image area that is marked by the overlay is returned as a new image object.

to acquire the widget’s state you can use a globalkey object.

example:

import 'dart:ui'; // this imports the 'image' class.

final key = globalkey<imagecropstate>();
image imageobject = ...

...

imagecrop(key: key, image: imageobject) // this could be used inside a  build method.

...
await key.currentstate.rotateimage(); // rotate the image be 90° clockwise.
final cropedimage = await key.currentstate.cropimage(); // this could be used inside a 'onpress' handler method.

how to create an image object

the image class from dart:ui is typically not instantiated directly. instead, you could convert your image data into a uint8list and instantiate the image like this:

uint8list bytes = ...
final codec = await instantiateimagecodec(bytes);
final frame = await codec.getnextframe();
final image = frame.image;

how to display an image object

the image object can be displayed with the flutter image widget. one way to do this, is by converting the image into a uint8list and pass it into the widget’s memory constructor. please note, that the widget is not the same image class as the image object itself.

final cropedimage = await key.currentstate.cropimage();
final byte = await cropedimage.tobytedata(format: ui.imagebyteformat.png);
final bytelist = byte.buffer.asuint8list();

image.memory(bytelist)

how to persist an image object

the image object can be persisted into a file or a database. to do this, you can convert the image object into a uint8list and write it into a file or your database object.

final cropedimage = await key.currentstate.cropimage();
final byte = await cropedimage.tobytedata(format: ui.imagebyteformat.png);
final bytelist = byte.buffer.asuint8list();

final imagefile = file("some path and filename"); // you can use the path_provider package to locate the right path.
final result = await imagefile.writeasbytes(bytelist);

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.