Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion third_party/packages/flutter_svg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,25 @@ import 'dart:ui' as ui;
canvas.drawPicture(pictureInfo.picture);

// Or convert the picture to an image:
final ui.Image image = await pictureInfo.picture.toImage(width, height);
ui.Image image = await pictureInfo.picture.toImage(width, height);

// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);
Comment on lines +125 to +143

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Assigning a value to image and then immediately reassigning it can be confusing in an example, as it might suggest to users that both steps are necessary. To make it clearer that creating an unscaled image and a scaled image are alternative operations, I suggest commenting out the first assignment. This also allows you to use final for the image variable, which is good practice.

Suggested change
ui.Image image = await pictureInfo.picture.toImage(width, height);
// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);
// To create an unscaled image, you can do:
// final ui.Image image = await pictureInfo.picture.toImage(width, height);
// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
final ui.Image image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);


pictureInfo.picture.dispose();
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Widget loadPrecompiledAsset() {

/// Demonstrates converting SVG to another type.
Future<ui.Image> convertSvgOutput() async {
final canvas = Canvas(ui.PictureRecorder());
var canvas = Canvas(ui.PictureRecorder());
const width = 100;
const height = 100;

Expand All @@ -94,7 +94,25 @@ Future<ui.Image> convertSvgOutput() async {
canvas.drawPicture(pictureInfo.picture);

// Or convert the picture to an image:
final ui.Image image = await pictureInfo.picture.toImage(width, height);
ui.Image image = await pictureInfo.picture.toImage(width, height);

// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);
Comment on lines +97 to +115

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Assigning a value to image and then immediately reassigning it can be confusing in an example, as it might suggest to users that both steps are necessary. To make it clearer that creating an unscaled image and a scaled image are alternative operations, I suggest commenting out the first assignment. This also allows you to use final for the image variable, which is good practice.

Suggested change
ui.Image image = await pictureInfo.picture.toImage(width, height);
// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);
// To create an unscaled image, you can do:
// final ui.Image image = await pictureInfo.picture.toImage(width, height);
// Or convert the picture to a scaled image:
const double targetWidth = 512;
const double targetHeight = 512;
final pictureRecorder = ui.PictureRecorder();
canvas = Canvas(
pictureRecorder,
Rect.fromPoints(Offset.zero, const Offset(targetWidth, targetHeight)),
);
canvas.scale(
targetWidth / pictureInfo.size.width,
targetHeight / pictureInfo.size.height,
);
canvas.drawPicture(pictureInfo.picture);
final ui.Image image = await pictureRecorder.endRecording().toImage(
targetWidth.ceil(),
targetHeight.ceil(),
);


pictureInfo.picture.dispose();
// #enddocregion OutputConversion
Expand Down