createImagePng method

Future<void> createImagePng(
  1. BuildContext context,
  2. EditImageProvider editImageProvider
)

Implementation

Future<void> createImagePng(BuildContext context, EditImageProvider editImageProvider) async {
  Timer.periodic(const Duration(milliseconds: 500), (Timer timer) async{
    timer.cancel();
    RenderRepaintBoundary? boundary = widgetKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
    try {
      if (boundary.debugNeedsPaint) {
        await Future.delayed(const Duration(milliseconds: 5));
        return createImagePng(context, editImageProvider);
      }
    } catch (_) {}
    try {
      ui.Image image = await boundary.toImage(pixelRatio: 5.0);
      ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
      Uint8List pngBytes = byteData!.buffer.asUint8List();
      File? tempFile = await saveUint8ListAsImage(pngBytes);
      double degrees = radiansToDegrees(editImageProvider.state.rotationAngle);
      if (tempFile != null) {
        Navigator.pop(context, FinishedImageData(
          tempFile,
          Size(
            degrees % 180 == 0 ? editImageProvider.state.width : editImageProvider.state.height,
            degrees % 180 == 0 ? editImageProvider.state.height : editImageProvider.state.width
          )
        ));
      } else {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: const Text('Error'),
              content: const Text('An error occurred while saving the image.'),
              actions: <Widget>[
                ElevatedButton(
                  child: const Text('OK'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          },
        );
      }
    } catch (_) {
      await Future.delayed(const Duration(milliseconds: 5));
      return createImagePng(context, editImageProvider);
    }
  });
}