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

Add offline support #10

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ build/
**/ios/Flutter/flutter_assets/
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*
**/ios/**/flutter_export_environment.sh

# Exceptions to above rules.
!**/ios/**/default.mode1v3
Expand Down
1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
1 change: 1 addition & 0 deletions example/assets/3d_modeling.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class MyHomePage extends StatefulWidget {

class _MyHomePageState extends State<MyHomePage> {
Color color = Colors.red;
UnDrawIllustration illustration = UnDrawIllustration.mobile_application;
String illustration = '3d_modeling';
String mode = 'offline';

@override
Widget build(BuildContext context) {
Expand All @@ -41,6 +42,7 @@ class _MyHomePageState extends State<MyHomePage> {
body: UnDraw(
color: color,
illustration: illustration,
mode: mode,
placeholder: Text("Illustration is loading..."),
errorWidget: Icon(Icons.error_outline, color: Colors.red, size: 50),
),
Expand All @@ -64,8 +66,11 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: () {
Random random = new Random();
setState(() {
mode = 'online';
illustration = UnDrawIllustration
.values[random.nextInt(UnDrawIllustration.values.length)];
.values[random.nextInt(UnDrawIllustration.values.length)]
.toString()
.split('UnDrawIllustration.')[1];
});
},
backgroundColor: Colors.red,
Expand Down
54 changes: 41 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.11"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.2"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
charcode:
dependency: transitive
description:
Expand All @@ -36,6 +50,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -74,27 +95,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.1.8"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
version: "1.6.4"
path_drawing:
dependency: transitive
description:
Expand All @@ -115,21 +143,21 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0+1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
version: "2.4.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -162,7 +190,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
term_glyph:
dependency: transitive
description:
Expand All @@ -176,7 +204,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand All @@ -190,7 +218,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.1"
version: "1.0.2"
vector_math:
dependency: transitive
description:
Expand All @@ -204,7 +232,7 @@ packages:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.4.1"
version: "3.5.0"
sdks:
dart: ">=2.2.2 <3.0.0"
dart: ">=2.4.0 <3.0.0"
flutter: ">=1.5.9-pre.94 <2.0.0"
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ flutter:
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
assets:
- assets/
# - images/a_dot_ham.jpeg

# An image asset can refer to one or more resolution-specific "variants", see
Expand Down
31 changes: 23 additions & 8 deletions lib/undraw.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
library undraw;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:http/http.dart' as http;
import 'package:undraw/illustrations.dart';
Expand All @@ -12,6 +13,7 @@ class UnDraw extends StatelessWidget {
Key key,
@required this.illustration,
@required this.color,
this.mode,
this.semanticLabel,
this.alignment = Alignment.center,
this.fit = BoxFit.contain,
Expand All @@ -24,8 +26,9 @@ class UnDraw extends StatelessWidget {
}) : assert(illustration != null),
assert(color != null);

final UnDrawIllustration illustration;
final String illustration;
final Color color;
final String mode;
final String semanticLabel;
final AlignmentGeometry alignment;
final BoxFit fit;
Expand All @@ -37,11 +40,23 @@ class UnDraw extends StatelessWidget {
final EdgeInsets padding;

Future<SvgPicture> renderIllustration(String name, Color _exColor) async {
var nameSplit = name.toString().split("UnDrawIllustration.");
var illustration =
illustrationList.where((i) => i["identifier"] == nameSplit[1]);
String url = illustration.toList()[0]["url"];
String image = await _getSvgString(url);
String image;
String url;
if (this.mode == 'offline') {
image = await rootBundle.loadString('assets/$name.svg');
} else {
if (name.toString().contains('UnDrawIllustration.')) {
var nameSplit = name.toString().split("UnDrawIllustration.");
var illustration =
illustrationList.where((i) => i["identifier"] == nameSplit[1]);
url = illustration.toList()[0]["url"];
} else {
var illustration =
illustrationList.where((i) => i["identifier"] == name);
url = illustration.toList()[0]["url"];
}
image = await _getSvgFromUrl(url);
}

String valueString = color.toString().split('(0x')[1].split(')')[0];
valueString = valueString.substring(2, valueString.length);
Expand All @@ -61,7 +76,7 @@ class UnDraw extends StatelessWidget {
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: renderIllustration(illustration.toString(), color),
future: renderIllustration(illustration, color),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return Container(
Expand All @@ -82,7 +97,7 @@ class UnDraw extends StatelessWidget {
);
}

Future<String> _getSvgString(url) async {
Future<String> _getSvgFromUrl(url) async {
http.Response response = await http.get(url);
return response.body;
}
Expand Down
Loading