Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] What is the foregroundColor and backgroundColor for QrPainter? #112

Closed
amadeu01 opened this issue Sep 15, 2020 · 2 comments
Closed

Comments

@amadeu01
Copy link

I'm using the QrPainter basically because I need the ui.Image to share the generated image with other apps. So, I'm not sure if I really must use that.

But, I saw that emptyColor is deprecated for QrPainter and it has the following comment:

You should us the background color value of your container widget

That would be the CustomPaint widget? Anyway the QrPainter does not offer the foregroundColor and backgroundColor as the QrImage. What would be the proper setup of the colour with QrPainter?

@fbuireu
Copy link

fbuireu commented Dec 17, 2021

The short answer is there isn't.

However, a workaround to export the QrPainter as image is to use the QrDrawing (where there's a background and foreground attributes) within a GlobalKey and create the raw image based on that key. Something like:

  Future<Uint8List> createImageFromRenderKey({GlobalKey<State<StatefulWidget>>? renderKey}) async {
    try {
      final RenderRepaintBoundary boundary = renderKey?.currentContext?.findRenderObject()! as RenderRepaintBoundary;
      final ui.Image image = await boundary.toImage(pixelRatio: 3);
      final ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);

      return byteData!.buffer.asUint8List();
    } catch(_) {
      rethrow;
    }
  }

createImageFromRenderKey is the method to be called on your "export" button created from:

   final GlobalKey globalKey = GlobalKey();

The rethrow is a workaround to bypass Flutter's limitation/bug from flutter/flutter#22308 where the RepaintBoundary class may be being used when the UI is being rehydrated (exposed as boundary.debugNeedsPaint), causing an unhandled exception or creating a low-quality image. So if the UI is being painted at that time, I rethrow the method.

@lukef
Copy link
Collaborator

lukef commented Mar 3, 2022

This will be totally changing in 5.0.

@lukef lukef closed this as completed Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants