|
1 |
| -library vector_graphics; |
| 1 | +import 'dart:typed_data'; |
| 2 | +import 'dart:ui' as ui; |
| 3 | +import 'dart:io'; |
2 | 4 |
|
3 |
| -/// A Calculator. |
4 |
| -class Calculator { |
5 |
| - /// Returns [value] plus 1. |
6 |
| - int addOne(int value) => value + 1; |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/rendering.dart'; |
| 7 | +import 'package:flutter/widgets.dart'; |
| 8 | + |
| 9 | +import 'package:vector_graphics_codec/vector_graphics_codec.dart'; |
| 10 | + |
| 11 | +import 'src/listener.dart'; |
| 12 | + |
| 13 | +const VectorGraphicsCodec _codec = VectorGraphicsCodec(); |
| 14 | + |
| 15 | +/// Decode a vector graphics binary asset into a [ui.Picture]. |
| 16 | +/// |
| 17 | +/// Throws a [StateError] if the data is invalid. |
| 18 | +ui.Picture decodeVectorGraphics(ByteData data) { |
| 19 | + final FlutterVectorGraphicsListener listener = |
| 20 | + FlutterVectorGraphicsListener(); |
| 21 | + _codec.decode(data, listener); |
| 22 | + return listener.toPicture(); |
| 23 | +} |
| 24 | + |
| 25 | +/// A widget that displays a vector_graphics formatted asset. |
| 26 | +/// |
| 27 | +/// A bytes loader class should not be constructed directly in a build method, |
| 28 | +/// if this is done the corresponding [VectorGraphic] widget may repeatedly |
| 29 | +/// reload the bytes. |
| 30 | +/// |
| 31 | +/// ```dart |
| 32 | +/// class MyVectorGraphic extends StatefulWidget { |
| 33 | +/// |
| 34 | +/// State<MyVectorGraphic> createState() => |
| 35 | +/// } |
| 36 | +/// |
| 37 | +/// class _MyVectorGraphicState extends State<MyVectorGraphic> { |
| 38 | +/// BytesLoader? loader; |
| 39 | +/// |
| 40 | +/// @override |
| 41 | +/// void initState() { |
| 42 | +/// super.initState(); |
| 43 | +/// loader = AssetBytesLoader(assetName: 'foobar', assetBundle: DefaultAssetBundle.of(context)); |
| 44 | +/// } |
| 45 | +/// |
| 46 | +/// @override |
| 47 | +/// Widget build(BuildContext context) { |
| 48 | +/// return VectorGraphic(bytesLoader: loader!); |
| 49 | +/// } |
| 50 | +/// } |
| 51 | +/// ``` |
| 52 | +class VectorGraphic extends StatefulWidget { |
| 53 | + const VectorGraphic({Key? key, required this.bytesLoader}) : super(key: key); |
| 54 | + |
| 55 | + final BytesLoader bytesLoader; |
| 56 | + |
| 57 | + @override |
| 58 | + State<VectorGraphic> createState() => _VectorGraphicsWidgetState(); |
| 59 | +} |
| 60 | + |
| 61 | +class _VectorGraphicsWidgetState extends State<VectorGraphic> { |
| 62 | + ui.Picture? _picture; |
| 63 | + |
| 64 | + @override |
| 65 | + void initState() { |
| 66 | + _loadAssetBytes(); |
| 67 | + super.initState(); |
| 68 | + } |
| 69 | + |
| 70 | + @override |
| 71 | + void didUpdateWidget(covariant VectorGraphic oldWidget) { |
| 72 | + if (oldWidget.bytesLoader != widget.bytesLoader) { |
| 73 | + _loadAssetBytes(); |
| 74 | + } |
| 75 | + super.didUpdateWidget(oldWidget); |
| 76 | + } |
| 77 | + |
| 78 | + @override |
| 79 | + void dispose() { |
| 80 | + _picture?.dispose(); |
| 81 | + _picture = null; |
| 82 | + super.dispose(); |
| 83 | + } |
| 84 | + |
| 85 | + void _loadAssetBytes() { |
| 86 | + widget.bytesLoader.loadBytes().then((ByteData data) { |
| 87 | + final ui.Picture picture = decodeVectorGraphics(data); |
| 88 | + setState(() { |
| 89 | + _picture?.dispose(); |
| 90 | + _picture = picture; |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @override |
| 96 | + Widget build(BuildContext context) { |
| 97 | + final ui.Picture? picture = _picture; |
| 98 | + if (picture == null) { |
| 99 | + return const SizedBox(); |
| 100 | + } |
| 101 | + return _RawVectorGraphicsWidget(picture: picture); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// An interface that can be implemented to support decoding vector graphic |
| 106 | +/// binary assets from different byte sources. |
| 107 | +/// |
| 108 | +/// A bytes loader class should not be constructed directly in a build method, |
| 109 | +/// if this is done the corresponding [VectorGraphic] widget may repeatedly |
| 110 | +/// reload the bytes. |
| 111 | +/// |
| 112 | +/// See also: |
| 113 | +/// * [AssetBytesLoader], for loading from the asset bundle. |
| 114 | +/// * [NetworkBytesLoader], for loading network bytes. |
| 115 | +abstract class BytesLoader { |
| 116 | + /// const constructor to allow subtypes to be const. |
| 117 | + const BytesLoader(); |
| 118 | + |
| 119 | + /// Load the byte data for a vector graphic binary asset. |
| 120 | + Future<ByteData> loadBytes(); |
| 121 | +} |
| 122 | + |
| 123 | +/// A controller for loading vector graphics data from an asset bundle. |
| 124 | +class AssetBytesLoader extends BytesLoader { |
| 125 | + /// Create a new [VectorGraphicsAssetController]. |
| 126 | + /// |
| 127 | + /// The default asset bundle can be acquired using [DefaultAssetBundle.of]. |
| 128 | + const AssetBytesLoader({ |
| 129 | + required this.assetName, |
| 130 | + this.packageName, |
| 131 | + required this.assetBundle, |
| 132 | + }); |
| 133 | + |
| 134 | + final String assetName; |
| 135 | + final String? packageName; |
| 136 | + final AssetBundle assetBundle; |
| 137 | + |
| 138 | + @override |
| 139 | + Future<ByteData> loadBytes() { |
| 140 | + return assetBundle.load(assetName); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/// A controller for loading vector graphics data from over the network. |
| 145 | +class NetworkBytesLoader extends BytesLoader { |
| 146 | + const NetworkBytesLoader({ |
| 147 | + required this.url, |
| 148 | + this.headers, |
| 149 | + this.client, |
| 150 | + }); |
| 151 | + |
| 152 | + final Map<String, String>? headers; |
| 153 | + final Uri url; |
| 154 | + final HttpClient? client; |
| 155 | + |
| 156 | + @override |
| 157 | + Future<ByteData> loadBytes() async { |
| 158 | + final HttpClient currentClient = client ?? HttpClient(); |
| 159 | + final HttpClientRequest request = await currentClient.getUrl(url); |
| 160 | + headers?.forEach(request.headers.add); |
| 161 | + |
| 162 | + final HttpClientResponse response = await request.close(); |
| 163 | + if (response.statusCode != HttpStatus.ok) { |
| 164 | + await response.drain<List<int>>(<int>[]); |
| 165 | + throw Exception('Failed to load VectorGraphic: ${response.statusCode}'); |
| 166 | + } |
| 167 | + final Uint8List bytes = await consolidateHttpClientResponseBytes( |
| 168 | + response, |
| 169 | + ); |
| 170 | + return bytes.buffer.asByteData(); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +class _RawVectorGraphicsWidget extends SingleChildRenderObjectWidget { |
| 175 | + const _RawVectorGraphicsWidget({Key? key, required this.picture}) |
| 176 | + : super(key: key); |
| 177 | + |
| 178 | + final ui.Picture picture; |
| 179 | + |
| 180 | + @override |
| 181 | + RenderObject createRenderObject(BuildContext context) { |
| 182 | + return _RenderVectorGraphics(picture); |
| 183 | + } |
| 184 | + |
| 185 | + @override |
| 186 | + void updateRenderObject( |
| 187 | + BuildContext context, covariant _RenderVectorGraphics renderObject) { |
| 188 | + renderObject.picture = picture; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +class _RenderVectorGraphics extends RenderProxyBox { |
| 193 | + _RenderVectorGraphics(this._picture); |
| 194 | + |
| 195 | + ui.Picture get picture => _picture; |
| 196 | + ui.Picture _picture; |
| 197 | + set picture(ui.Picture value) { |
| 198 | + if (identical(value, _picture)) { |
| 199 | + return; |
| 200 | + } |
| 201 | + _picture = value; |
| 202 | + markNeedsPaint(); |
| 203 | + } |
| 204 | + |
| 205 | + @override |
| 206 | + void paint(PaintingContext context, ui.Offset offset) { |
| 207 | + context.canvas.drawPicture(picture); |
| 208 | + } |
7 | 209 | }
|
0 commit comments