-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(passkit_ui): Export rendered PkPass as image (#79)
- Loading branch information
Showing
9 changed files
with
119 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
passkit_ui/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export 'src/widgets/widgets.dart'; | ||
export 'src/extensions/extensions.dart'; | ||
export 'src/pk_pass_widget.dart'; | ||
export 'src/export_pass_image.dart'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'dart:ui' as ui; | ||
|
||
import 'package:passkit/passkit.dart'; | ||
import 'package:passkit_ui/passkit_ui.dart'; | ||
|
||
/// Creates a PNG from the given [pass] | ||
/// | ||
/// Remarks: | ||
/// - Sometimes, images aren't correctly rendered on a pass. Calling this method | ||
/// a second time with the same pass resolves that issue. | ||
@experimental | ||
Future<Uint8List?> exportPassAsImage( | ||
PkPass pass, { | ||
Size logicalSize = const Size(320, 460), | ||
double pixelRatio = 3, | ||
}) async { | ||
final repaintBoundary = RenderRepaintBoundary(); | ||
final pipelineOwner = PipelineOwner(); | ||
final buildOwner = BuildOwner(focusManager: FocusManager()); | ||
|
||
try { | ||
final renderView = RenderView( | ||
view: ui.PlatformDispatcher.instance.implicitView!, | ||
child: RenderPositionedBox( | ||
alignment: Alignment.center, | ||
child: repaintBoundary, | ||
), | ||
configuration: ViewConfiguration( | ||
logicalConstraints: BoxConstraints.tight(logicalSize), | ||
devicePixelRatio: pixelRatio, | ||
), | ||
); | ||
|
||
pipelineOwner.rootNode = renderView; | ||
renderView.prepareInitialFrame(); | ||
|
||
final widget = MediaQuery( | ||
data: MediaQueryData(devicePixelRatio: pixelRatio), | ||
child: Directionality( | ||
textDirection: TextDirection.ltr, | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Material( | ||
color: Colors.transparent, | ||
child: PkPassWidget(pass: pass), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
|
||
final rootElement = RenderObjectToWidgetAdapter<RenderBox>( | ||
container: repaintBoundary, | ||
child: widget, | ||
).attachToRenderTree(buildOwner); | ||
|
||
buildOwner.buildScope(rootElement); | ||
buildOwner.buildScope(rootElement); | ||
buildOwner.finalizeTree(); | ||
pipelineOwner.flushLayout(); | ||
pipelineOwner.flushCompositingBits(); | ||
pipelineOwner.flushPaint(); | ||
|
||
final image = await repaintBoundary.toImage(pixelRatio: pixelRatio); | ||
|
||
final byteData = await image.toByteData(format: ui.ImageByteFormat.png); | ||
|
||
return byteData?.buffer.asUint8List( | ||
byteData.offsetInBytes, | ||
byteData.lengthInBytes, | ||
); | ||
} catch (e) { | ||
throw Exception('Failed to render the widget: $e'); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ dependencies: | |
csslib: ^1.0.0 | ||
flutter: | ||
sdk: flutter | ||
meta: any | ||
passkit: ^0.0.5 | ||
|
||
dev_dependencies: | ||
|