1+ import 'dart:math' ;
2+
13import 'package:flutter/material.dart' ;
24import 'package:flutter_svg/flutter_svg.dart' ;
35
46const List <String > _assetNames = < String > [
5- // 'assets/notfound.svg', // uncomment to test an asset that doesn't exist.
7+ 'assets/invalid.svg' ,
8+ 'assets/notfound.svg' , // uncomment to test an asset that doesn't exist.
69 'assets/flutter_logo.svg' ,
710 'assets/dart.svg' ,
811 'assets/simple/clip_path_3.svg' ,
@@ -35,7 +38,7 @@ const List<String> _assetNames = <String>[
3538];
3639
3740/// Assets treated as "icons" - using a color filter to render differently.
38- const List <String > iconNames = < String > [
41+ const List <String > _iconNames = < String > [
3942 'assets/deborah_ufw/new-action-expander.svg' ,
4043 'assets/deborah_ufw/new-camera.svg' ,
4144 'assets/deborah_ufw/new-gif-button.svg' ,
@@ -49,12 +52,27 @@ const List<String> iconNames = <String>[
4952];
5053
5154/// Assets to test network access.
52- const List <String > uriNames = < String > [
55+ const List <String > _uriNames = < String > [
5356 'http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg' ,
5457 'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/410.svg' ,
5558 'https://upload.wikimedia.org/wikipedia/commons/b/b4/Chess_ndd45.svg' ,
5659];
5760
61+ const List <String > _uriFailedNames = < String > [
62+ 'an error image url.svg' , // invalid url.
63+ 'https: /sadf.svg' , // invalid url.
64+ 'http://www.google.com/404' , // 404 url.
65+ 'https://picsum.photos/200' , // wrong format image url.
66+ ];
67+
68+ const List <String > _stringNames = < String > [
69+ '''<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <image xlink:href="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" height="200" width="200"/></svg>''' , // Shows an example of an SVG image that will fetch a raster image from a URL.
70+ '''<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg"> <circle r="45" cx="50" cy="50" fill="red" /> </svg> ''' , // valid svg
71+ '''<svg></svg>''' , // empty svg.
72+ 'sdf sdf ' , // invalid svg.
73+ '' , // empty string.
74+ ];
75+
5876void main () {
5977 runApp (_MyApp ());
6078}
@@ -81,59 +99,10 @@ class _MyHomePage extends StatefulWidget {
8199}
82100
83101class _MyHomePageState extends State <_MyHomePage > {
84- final List <Widget > _painters = < Widget > [];
85- late double _dimension;
86-
87- @override
88- void initState () {
89- super .initState ();
90- _dimension = 203.0 ;
91- for (final String assetName in _assetNames) {
92- _painters.add (
93- SvgPicture .asset (assetName),
94- );
95- }
96-
97- for (int i = 0 ; i < iconNames.length; i++ ) {
98- _painters.add (
99- Directionality (
100- textDirection: TextDirection .ltr,
101- child: SvgPicture .asset (
102- iconNames[i],
103- colorFilter: ColorFilter .mode (
104- Colors .blueGrey[(i + 1 ) * 100 ] ?? Colors .blueGrey,
105- BlendMode .srcIn,
106- ),
107- matchTextDirection: true ,
108- ),
109- ),
110- );
111- }
112-
113- for (final String uriName in uriNames) {
114- _painters.add (
115- SvgPicture .network (
116- uriName,
117- placeholderBuilder: (BuildContext context) => Container (
118- padding: const EdgeInsets .all (30.0 ),
119- child: const CircularProgressIndicator (),
120- ),
121- ),
122- );
123- }
124- // Shows an example of an SVG image that will fetch a raster image from a URL.
125- _painters.add (SvgPicture .string ('''
126- <svg viewBox="0 0 200 200"
127- xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
128- <image xlink:href="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" height="200" width="200"/>
129- </svg>''' ));
130- }
102+ double _dimension = 60 ;
131103
132104 @override
133105 Widget build (BuildContext context) {
134- if (_dimension > MediaQuery .of (context).size.width - 10.0 ) {
135- _dimension = MediaQuery .of (context).size.width - 10.0 ;
136- }
137106 return Scaffold (
138107 appBar: AppBar (
139108 title: Text (widget.title),
@@ -144,7 +113,7 @@ class _MyHomePageState extends State<_MyHomePage> {
144113 max: MediaQuery .of (context).size.width - 10.0 ,
145114 value: _dimension,
146115 onChanged: (double val) {
147- setState (() => _dimension = val);
116+ setState (() => _dimension = min ( MediaQuery . of (context).size.width - 10.0 , val) );
148117 },
149118 ),
150119 Expanded (
@@ -154,7 +123,86 @@ class _MyHomePageState extends State<_MyHomePage> {
154123 padding: const EdgeInsets .all (4.0 ),
155124 mainAxisSpacing: 4.0 ,
156125 crossAxisSpacing: 4.0 ,
157- children: _painters.toList (),
126+ children: < Widget > [
127+ ..._assetNames.map (
128+ (String e) => SvgPicture .asset (
129+ e,
130+ placeholderBuilder: (BuildContext context) => Container (
131+ padding: const EdgeInsets .all (30.0 ),
132+ child: const CircularProgressIndicator (),
133+ ),
134+ errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) => Container (
135+ color: Colors .brown,
136+ width: 10 ,
137+ height: 10 ,
138+ ),
139+ ),
140+ ),
141+ ..._iconNames.map (
142+ (String e) => Directionality (
143+ textDirection: TextDirection .ltr,
144+ child: SvgPicture .asset (
145+ e,
146+ colorFilter: ColorFilter .mode (
147+ Colors .blueGrey[(_iconNames.indexOf (e) + 1 ) * 100 ] ?? Colors .blueGrey,
148+ BlendMode .srcIn,
149+ ),
150+ matchTextDirection: true ,
151+ placeholderBuilder: (BuildContext context) => Container (
152+ padding: const EdgeInsets .all (30.0 ),
153+ child: const CircularProgressIndicator (),
154+ ),
155+ errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) => Container (
156+ color: Colors .yellow,
157+ width: 10 ,
158+ height: 10 ,
159+ ),
160+ ),
161+ ),
162+ ),
163+ ..._uriNames.map (
164+ (String e) => SvgPicture .network (
165+ e,
166+ placeholderBuilder: (BuildContext context) => Container (
167+ padding: const EdgeInsets .all (30.0 ),
168+ child: const CircularProgressIndicator (),
169+ ),
170+ errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) => Container (
171+ color: Colors .red,
172+ width: 10 ,
173+ height: 10 ,
174+ ),
175+ ),
176+ ),
177+ ..._uriFailedNames.map (
178+ (String e) => SvgPicture .network (
179+ e,
180+ placeholderBuilder: (BuildContext context) => Container (
181+ padding: const EdgeInsets .all (30.0 ),
182+ child: const CircularProgressIndicator (),
183+ ),
184+ errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) => Container (
185+ color: Colors .deepPurple,
186+ width: 10 ,
187+ height: 10 ,
188+ ),
189+ ),
190+ ),
191+ ..._stringNames.map (
192+ (String e) => SvgPicture .string (
193+ e,
194+ placeholderBuilder: (BuildContext context) => Container (
195+ padding: const EdgeInsets .all (30.0 ),
196+ child: const CircularProgressIndicator (),
197+ ),
198+ errorBuilder: (BuildContext context, Object error, StackTrace stackTrace) => Container (
199+ color: Colors .pinkAccent,
200+ width: 10 ,
201+ height: 10 ,
202+ ),
203+ ),
204+ ),
205+ ],
158206 ),
159207 ),
160208 ]),
0 commit comments