Compare commits

..

1 Commits

Author SHA1 Message Date
45a4c0b981 Remove direct dependency on dart:io (#90) 2020-07-26 23:09:58 +02:00
9 changed files with 70 additions and 37 deletions

View File

@ -1,3 +1,6 @@
## [0.5.1]
- Remove direct dependencies on dart:io to support Flutter Web
## [0.5.0] ## [0.5.0]
- Support loading animation from network in a web app - Support loading animation from network in a web app
- Fix a couple of bugs with the web dev compiler - Fix a couple of bugs with the web dev compiler

View File

@ -1,4 +1,3 @@
import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
@ -72,7 +71,7 @@ class Lottie extends StatefulWidget {
/// Creates a widget that displays an [LottieComposition] obtained from a [File]. /// Creates a widget that displays an [LottieComposition] obtained from a [File].
static LottieBuilder file( static LottieBuilder file(
File file, { Object /*io.File|html.File*/ file, {
Animation<double> controller, Animation<double> controller,
bool animate, bool animate,
bool repeat, bool repeat,

View File

@ -1,5 +1,4 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -83,7 +82,7 @@ class LottieBuilder extends StatefulWidget {
/// `android.permission.READ_EXTERNAL_STORAGE` permission. /// `android.permission.READ_EXTERNAL_STORAGE` permission.
/// ///
LottieBuilder.file( LottieBuilder.file(
File file, { Object /*io.File|html.File*/ file, {
this.controller, this.controller,
this.animate, this.animate,
this.reverse, this.reverse,

View File

@ -1,25 +1,24 @@
import 'dart:io';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import '../composition.dart'; import '../composition.dart';
import '../lottie_image_asset.dart'; import '../lottie_image_asset.dart';
import 'load_image.dart'; import 'load_image.dart';
import 'lottie_provider.dart'; import 'lottie_provider.dart';
import 'provider_io.dart' if (dart.library.html) 'provider_web.dart' as io;
class FileLottie extends LottieProvider { class FileLottie extends LottieProvider {
FileLottie(this.file, {LottieImageProviderFactory imageProviderFactory}) FileLottie(this.file, {LottieImageProviderFactory imageProviderFactory})
: super(imageProviderFactory: imageProviderFactory); : super(imageProviderFactory: imageProviderFactory);
final File file; final Object /*io.File|html.File*/ file;
@override @override
Future<LottieComposition> load() async { Future<LottieComposition> load() async {
var cacheKey = 'file-${file.path}'; var cacheKey = 'file-${io.filePath(file)}';
return sharedLottieCache.putIfAbsent(cacheKey, () async { return sharedLottieCache.putIfAbsent(cacheKey, () async {
var bytes = await file.readAsBytes(); var bytes = await io.loadFile(file);
var composition = await LottieComposition.fromBytes(bytes, var composition = await LottieComposition.fromBytes(bytes,
name: p.basenameWithoutExtension(file.path)); name: p.basenameWithoutExtension(io.filePath(file)));
for (var image in composition.images.values) { for (var image in composition.images.values) {
image.loadedImage ??= await _loadImage(composition, image); image.loadedImage ??= await _loadImage(composition, image);
@ -33,11 +32,7 @@ class FileLottie extends LottieProvider {
LottieComposition composition, LottieImageAsset lottieImage) { LottieComposition composition, LottieImageAsset lottieImage) {
var imageProvider = getImageProvider(lottieImage); var imageProvider = getImageProvider(lottieImage);
if (imageProvider == null) { imageProvider ??= io.loadImageForFile(file, lottieImage);
var imagePath = p.url.join(
p.dirname(file.path), lottieImage.dirName, lottieImage.fileName);
imageProvider = FileImage(File(imagePath));
}
return loadImage(composition, lottieImage, imageProvider); return loadImage(composition, lottieImage, imageProvider);
} }
@ -52,5 +47,5 @@ class FileLottie extends LottieProvider {
int get hashCode => file.hashCode; int get hashCode => file.hashCode;
@override @override
String toString() => '$runtimeType(file: ${file.path})'; String toString() => '$runtimeType(file: ${io.filePath(file)})';
} }

View File

@ -6,8 +6,7 @@ import '../composition.dart';
import '../lottie_image_asset.dart'; import '../lottie_image_asset.dart';
import 'load_image.dart'; import 'load_image.dart';
import 'lottie_provider.dart'; import 'lottie_provider.dart';
import 'network_provider_io.dart' import 'provider_io.dart' if (dart.library.html) 'provider_web.dart' as network;
if (dart.library.html) 'network_provider_web.dart' as network;
class NetworkLottie extends LottieProvider { class NetworkLottie extends LottieProvider {
NetworkLottie(this.url, NetworkLottie(this.url,
@ -22,7 +21,7 @@ class NetworkLottie extends LottieProvider {
var cacheKey = 'network-$url'; var cacheKey = 'network-$url';
return sharedLottieCache.putIfAbsent(cacheKey, () async { return sharedLottieCache.putIfAbsent(cacheKey, () async {
var resolved = Uri.base.resolve(url); var resolved = Uri.base.resolve(url);
var bytes = await network.load(resolved, headers: headers); var bytes = await network.loadHttp(resolved, headers: headers);
var composition = await LottieComposition.fromBytes(bytes, var composition = await LottieComposition.fromBytes(bytes,
name: p.url.basenameWithoutExtension(url)); name: p.url.basenameWithoutExtension(url));

View File

@ -1,16 +0,0 @@
import 'dart:html';
import 'dart:typed_data';
Future<Uint8List> load(Uri uri, {Map<String, String> headers}) async {
var request = await HttpRequest.request(uri.toString(),
requestHeaders: headers, responseType: 'blob');
var reader = FileReader();
reader.readAsArrayBuffer(request.response as Blob);
await reader.onLoadEnd.first;
if (reader.readyState != FileReader.DONE) {
throw Exception('Error while reading $uri');
}
return reader.result as Uint8List;
}

View File

@ -1,10 +1,13 @@
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:path/path.dart' as p;
import '../lottie_image_asset.dart';
final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false; final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;
Future<Uint8List> load(Uri uri, {Map<String, String> headers}) async { Future<Uint8List> loadHttp(Uri uri, {Map<String, String> headers}) async {
var request = await _sharedHttpClient.getUrl(uri); var request = await _sharedHttpClient.getUrl(uri);
headers?.forEach((String name, String value) { headers?.forEach((String name, String value) {
request.headers.add(name, value); request.headers.add(name, value);
@ -21,3 +24,19 @@ Future<Uint8List> load(Uri uri, {Map<String, String> headers}) async {
return bytes; return bytes;
} }
Future<Uint8List> loadFile(Object file) {
return (file as File).readAsBytes();
}
String filePath(Object file) {
return (file as File).path;
}
ImageProvider loadImageForFile(Object file, LottieImageAsset lottieImage) {
var fileIo = file as File;
var imagePath = p.url
.join(p.dirname(fileIo.path), lottieImage.dirName, lottieImage.fileName);
return FileImage(File(imagePath));
}

View File

@ -0,0 +1,35 @@
import 'dart:html';
import 'dart:typed_data';
import 'package:flutter/rendering.dart';
import '../lottie_image_asset.dart';
Future<Uint8List> loadHttp(Uri uri, {Map<String, String> headers}) async {
var request = await HttpRequest.request(uri.toString(),
requestHeaders: headers, responseType: 'blob');
return _loadBlob(request.response as Blob);
}
Future<Uint8List> loadFile(Object file) {
return _loadBlob(file as File);
}
Future<Uint8List> _loadBlob(Blob file) async {
var reader = FileReader();
reader.readAsArrayBuffer(file);
await reader.onLoadEnd.first;
if (reader.readyState != FileReader.DONE) {
throw Exception('Error while reading blob');
}
return reader.result as Uint8List;
}
String filePath(Object file) {
return (file as File).relativePath;
}
ImageProvider loadImageForFile(Object file, LottieImageAsset lottieImage) {
return null;
}

View File

@ -1,6 +1,6 @@
name: lottie name: lottie
description: Render After Effects animations natively on Flutter. This package is a pure Dart implementation of a Lottie player. description: Render After Effects animations natively on Flutter. This package is a pure Dart implementation of a Lottie player.
version: 0.5.0 version: 0.5.1
homepage: https://github.com/xvrh/lottie-flutter homepage: https://github.com/xvrh/lottie-flutter
environment: environment: