flbaue / image_crop_widget

A Flutter widget to crop images.
MIT License
11 stars 13 forks source link

Support for Flutter Web? #2

Open ghost opened 4 years ago

ghost commented 4 years ago

Hey,

is there an implementation for flutter web planned as well? On pub.dev it said that web is supported, however, I tried it and it did not work.

Thanks! :)

pulstar commented 4 years ago

@senordonfelipe

It worked for me. Below is my test code, but you must create a widget and place the ImageCrop() inside it with a close button to the user go foward, then you call it in the MaterialPageRouter line below. The MemoryFileSystem() is from this package: https://pub.dev/packages/file

import 'dart:ui' as ui;
import 'package:file/memory.dart';
import 'package:image_crop_widget/image_crop_widget.dart';

Future<File> crop(File imageFile) async {
   File croppedFile = MemoryFileSystem().file('');
   final key = GlobalKey<ImageCropState>();
   final codec = await ui.instantiateImageCodec(imageFile.readAsBytesSync());
   final frame = await codec.getNextFrame();
   await Navigator.of(context).push(
      MaterialPageRoute(builder: (context) => ImageCrop(key: key, image: frame.image))
   );
   ui.Image croppedImage = await key.currentState.cropImage();
   final bytes = await croppedImage.toByteData(format: ui.ImageByteFormat.png);
   final byteList = bytes.buffer.asUint8List();
   return await croppedFile.writeAsBytes(byteList);
}