Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling enhancement #1104

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
38 changes: 25 additions & 13 deletions packages/flutter_svg/example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
Expand All @@ -6,11 +12,6 @@ if (localPropertiesFile.exists()) {
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
Expand All @@ -21,12 +22,23 @@ if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
namespace = 'com.example.example'
compileSdkVersion 34
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// For Kotlin projects
kotlinOptions {
jvmTarget = "1.8"
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -35,8 +47,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.example"
minSdkVersion 16
targetSdkVersion 30
minSdkVersion flutter.minSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand All @@ -55,5 +66,6 @@ flutter {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// For AGP 7.4+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.2'
}
21 changes: 3 additions & 18 deletions packages/flutter_svg/example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
buildscript {
// Do not copy this. It's strictly because this application does not care
// about Kotlin version and wants to always do its best to work.
ext.kotlin_version = '+'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand All @@ -28,6 +13,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
30 changes: 22 additions & 8 deletions packages/flutter_svg/example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
include ':app'
pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}()

def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.5.2" apply false
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
}

include ":app"
1 change: 1 addition & 0 deletions packages/flutter_svg/example/assets/invalid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
157 changes: 103 additions & 54 deletions packages/flutter_svg/example/lib/grid.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

const List<String> _assetNames = <String>[
// 'assets/notfound.svg', // uncomment to test an asset that doesn't exist.
'assets/invalid.svg',
'assets/notfound.svg', // uncomment to test an asset that doesn't exist.
'assets/flutter_logo.svg',
'assets/dart.svg',
'assets/simple/clip_path_3.svg',
Expand Down Expand Up @@ -35,7 +38,7 @@ const List<String> _assetNames = <String>[
];

/// Assets treated as "icons" - using a color filter to render differently.
const List<String> iconNames = <String>[
const List<String> _iconNames = <String>[
'assets/deborah_ufw/new-action-expander.svg',
'assets/deborah_ufw/new-camera.svg',
'assets/deborah_ufw/new-gif-button.svg',
Expand All @@ -49,12 +52,27 @@ const List<String> iconNames = <String>[
];

/// Assets to test network access.
const List<String> uriNames = <String>[
const List<String> _uriNames = <String>[
'http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg',
'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/410.svg',
'https://upload.wikimedia.org/wikipedia/commons/b/b4/Chess_ndd45.svg',
];

const List<String> _uriFailedNames = <String>[
'an error image url.svg', // invalid url.
'https: /sadf.svg', // invalid url.
'http://www.google.com/404', // 404 url.
'https://picsum.photos/200', // wrong format image url.
];

const List<String> _stringNames = <String>[
'''<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.
'''<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg"> <circle r="45" cx="50" cy="50" fill="red" /> </svg> ''', // valid svg
'''<svg></svg>''', // empty svg.
'sdf sdf ', // invalid svg.
'', // empty string.
];

void main() {
runApp(_MyApp());
}
Expand All @@ -81,58 +99,10 @@ class _MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<_MyHomePage> {
final List<Widget> _painters = <Widget>[];
late double _dimension;

@override
void initState() {
super.initState();
_dimension = 203.0;
for (String assetName in _assetNames) {
_painters.add(
SvgPicture.asset(assetName),
);
}

for (int i = 0; i < iconNames.length; i++) {
_painters.add(
Directionality(
textDirection: TextDirection.ltr,
child: SvgPicture.asset(
iconNames[i],
colorFilter: ColorFilter.mode(
Colors.blueGrey[(i + 1) * 100] ?? Colors.blueGrey,
BlendMode.srcIn,
),
matchTextDirection: true,
),
),
);
}

for (String uriName in uriNames) {
_painters.add(
SvgPicture.network(
uriName,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
),
);
}
// Shows an example of an SVG image that will fetch a raster image from a URL.
_painters.add(SvgPicture.string('''<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>'''));
}
double _dimension = 60;

@override
Widget build(BuildContext context) {
if (_dimension > MediaQuery.of(context).size.width - 10.0) {
_dimension = MediaQuery.of(context).size.width - 10.0;
}
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
Expand All @@ -143,7 +113,7 @@ class _MyHomePageState extends State<_MyHomePage> {
max: MediaQuery.of(context).size.width - 10.0,
value: _dimension,
onChanged: (double val) {
setState(() => _dimension = val);
setState(() => _dimension = min(MediaQuery.of(context).size.width - 10.0, val));
},
),
Expanded(
Expand All @@ -153,7 +123,86 @@ class _MyHomePageState extends State<_MyHomePage> {
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: _painters.toList(),
children: [
..._assetNames.map(
(e) => SvgPicture.asset(
e,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
errorBuilder: (context, error, stackTrace) => Container(
color: Colors.brown,
width: 10,
height: 10,
),
),
),
..._iconNames.map(
(e) => Directionality(
textDirection: TextDirection.ltr,
child: SvgPicture.asset(
e,
colorFilter: ColorFilter.mode(
Colors.blueGrey[(_iconNames.indexOf(e) + 1) * 100] ?? Colors.blueGrey,
BlendMode.srcIn,
),
matchTextDirection: true,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
errorBuilder: (context, error, stackTrace) => Container(
color: Colors.yellow,
width: 10,
height: 10,
),
),
),
),
..._uriNames.map(
(e) => SvgPicture.network(
e,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
errorBuilder: (context, error, stackTrace) => Container(
color: Colors.red,
width: 10,
height: 10,
),
),
),
..._uriFailedNames.map(
(e) => SvgPicture.network(
e,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
errorBuilder: (context, error, stackTrace) => Container(
color: Colors.deepPurple,
width: 10,
height: 10,
),
),
),
..._stringNames.map(
(e) => SvgPicture.string(
e,
placeholderBuilder: (BuildContext context) => Container(
padding: const EdgeInsets.all(30.0),
child: const CircularProgressIndicator(),
),
errorBuilder: (context, error, stackTrace) => Container(
color: Colors.pinkAccent,
width: 10,
height: 10,
),
),
),
],
),
),
]),
Expand Down
Loading