-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flutter_svg] Adopt code excerpts (#8181)
Converts the README from unmanaged code examples to code excerpted from analyzed and compiled code. Part of flutter/flutter#102679
- Loading branch information
1 parent
2033119
commit e6932b7
Showing
11 changed files
with
201 additions
and
38 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
103 changes: 103 additions & 0 deletions
103
third_party/packages/flutter_svg/example/lib/readme_excerpts.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,103 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This file exists solely to host compiled excerpts for README.md, and is not | ||
// intended for use as an actual example application. | ||
|
||
// #docregion OutputConversion | ||
import 'dart:ui' as ui; | ||
// #enddocregion OutputConversion | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_svg/flutter_svg.dart'; | ||
// #docregion PrecompiledAsset | ||
import 'package:vector_graphics/vector_graphics.dart'; | ||
// #enddocregion PrecompiledAsset | ||
|
||
/// Loads an SVG asset. | ||
Widget loadAsset() { | ||
// #docregion SimpleAsset | ||
const String assetName = 'assets/dart.svg'; | ||
final Widget svg = SvgPicture.asset( | ||
assetName, | ||
semanticsLabel: 'Dart Logo', | ||
); | ||
// #enddocregion SimpleAsset | ||
return svg; | ||
} | ||
|
||
/// Loads an SVG asset. | ||
Widget loadColorizedAsset() { | ||
// #docregion ColorizedAsset | ||
const String assetName = 'assets/simple/dash_path.svg'; | ||
final Widget svgIcon = SvgPicture.asset( | ||
assetName, | ||
colorFilter: const ColorFilter.mode(Colors.red, BlendMode.srcIn), | ||
semanticsLabel: 'Red dash paths', | ||
); | ||
// #enddocregion ColorizedAsset | ||
return svgIcon; | ||
} | ||
|
||
/// Demonstrates loading an asset that doesn't exist. | ||
Widget loadMissingAsset() { | ||
// #docregion MissingAsset | ||
// Will print error messages to the console. | ||
const String assetName = 'assets/image_that_does_not_exist.svg'; | ||
final Widget svg = SvgPicture.asset( | ||
assetName, | ||
); | ||
// #enddocregion MissingAsset | ||
return svg; | ||
} | ||
|
||
/// Demonstrates loading an asset with a placeholder. | ||
// This method should *not* be called in tests, as tests should not be | ||
// attempting to load from random uncontrolled locations. Using a real URL, | ||
// such as a GitHub URL pointing to this package's assets, would make the | ||
// README example harder to understand. | ||
Widget loadNetworkAssetWithPlaceholder() { | ||
// #docregion AssetWithPlaceholder | ||
final Widget networkSvg = SvgPicture.network( | ||
'https://site-that-takes-a-while.com/image.svg', | ||
semanticsLabel: 'A shark?!', | ||
placeholderBuilder: (BuildContext context) => Container( | ||
padding: const EdgeInsets.all(30.0), | ||
child: const CircularProgressIndicator()), | ||
); | ||
// #enddocregion AssetWithPlaceholder | ||
return networkSvg; | ||
} | ||
|
||
/// Demonstrates loading a precompiled asset. | ||
// This asset doesn't exist in the example app, but this code can still be run | ||
// to sanity-check the structure of the example code. | ||
Widget loadPrecompiledAsset() { | ||
// #docregion PrecompiledAsset | ||
const Widget svg = SvgPicture(AssetBytesLoader('assets/foo.svg.vec')); | ||
// #enddocregion PrecompiledAsset | ||
return svg; | ||
} | ||
|
||
/// Demonstrates converting SVG to another type. | ||
Future<ui.Image> convertSvgOutput() async { | ||
final Canvas canvas = Canvas(ui.PictureRecorder()); | ||
const int width = 100; | ||
const int height = 100; | ||
|
||
// #docregion OutputConversion | ||
const String rawSvg = '''<svg ...>...</svg>'''; | ||
final PictureInfo pictureInfo = | ||
await vg.loadPicture(const SvgStringLoader(rawSvg), null); | ||
|
||
// You can draw the picture to a canvas: | ||
canvas.drawPicture(pictureInfo.picture); | ||
|
||
// Or convert the picture to an image: | ||
final ui.Image image = await pictureInfo.picture.toImage(width, height); | ||
|
||
pictureInfo.picture.dispose(); | ||
// #enddocregion OutputConversion | ||
return image; | ||
} |
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
29 changes: 29 additions & 0 deletions
29
third_party/packages/flutter_svg/example/test/readme_excerpts_test.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,29 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_svg_example/readme_excerpts.dart' as excerpts; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
test('example simple loadAsset works', () async { | ||
final Widget svg = excerpts.loadAsset(); | ||
expect(svg, isNotNull); | ||
}); | ||
|
||
test('example loadAsset with color filter works', () async { | ||
final Widget svg = excerpts.loadAsset(); | ||
expect(svg, isNotNull); | ||
}); | ||
|
||
test('example loadAsset with a non-existent asset works', () async { | ||
final Widget svg = excerpts.loadMissingAsset(); | ||
expect(svg, isNotNull); | ||
}); | ||
|
||
test('example loadAsset with a precompiled asset works', () async { | ||
final Widget svg = excerpts.loadPrecompiledAsset(); | ||
expect(svg, isNotNull); | ||
}); | ||
} |
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