Add DefaultAssetBundle
Now AssetImage will look in the default bundle if you don't supply an explicit bundle. Fixes #680
This commit is contained in:
@@ -2,22 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'theme.dart';
|
||||
import 'icon_theme.dart';
|
||||
import 'icon_theme_data.dart';
|
||||
|
||||
AssetBundle _initIconBundle() {
|
||||
if (rootBundle != null)
|
||||
return rootBundle;
|
||||
const String _kAssetBase = '/packages/material_design_icons/icons/';
|
||||
return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase));
|
||||
}
|
||||
|
||||
final AssetBundle _iconBundle = _initIconBundle();
|
||||
|
||||
class Icon extends StatelessComponent {
|
||||
Icon({
|
||||
Key key,
|
||||
@@ -66,7 +56,6 @@ class Icon extends StatelessComponent {
|
||||
String density = 'drawable-xxhdpi';
|
||||
String colorSuffix = _getColorSuffix(context);
|
||||
return new AssetImage(
|
||||
bundle: _iconBundle,
|
||||
name: '$category/$density/ic_${subtype}_${colorSuffix}_${size}dp.png',
|
||||
width: size.toDouble(),
|
||||
height: size.toDouble(),
|
||||
|
||||
@@ -19,6 +19,15 @@ const TextStyle _errorTextStyle = const TextStyle(
|
||||
decorationStyle: TextDecorationStyle.double
|
||||
);
|
||||
|
||||
AssetBundle _initDefaultBundle() {
|
||||
if (rootBundle != null)
|
||||
return rootBundle;
|
||||
const String _kAssetBase = '/packages/material_design_icons/icons/';
|
||||
return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase));
|
||||
}
|
||||
|
||||
final AssetBundle _defaultBundle = _initDefaultBundle();
|
||||
|
||||
class MaterialApp extends StatefulComponent {
|
||||
MaterialApp({
|
||||
Key key,
|
||||
@@ -68,12 +77,15 @@ class _MaterialAppState extends State<MaterialApp> {
|
||||
data: config.theme ?? new ThemeData.fallback(),
|
||||
child: new DefaultTextStyle(
|
||||
style: _errorTextStyle,
|
||||
child: new Title(
|
||||
title: config.title,
|
||||
child: new Navigator(
|
||||
key: _navigator,
|
||||
routes: config.routes,
|
||||
onGenerateRoute: config.onGenerateRoute
|
||||
child: new DefaultAssetBundle(
|
||||
bundle: _defaultBundle,
|
||||
child: new Title(
|
||||
title: config.title,
|
||||
child: new Navigator(
|
||||
key: _navigator,
|
||||
routes: config.routes,
|
||||
onGenerateRoute: config.onGenerateRoute
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1017,6 +1017,26 @@ class NetworkImage extends StatelessComponent {
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultAssetBundle extends InheritedWidget {
|
||||
DefaultAssetBundle({
|
||||
Key key,
|
||||
this.bundle,
|
||||
Widget child
|
||||
}) : super(key: key, child: child) {
|
||||
assert(bundle != null);
|
||||
assert(child != null);
|
||||
}
|
||||
|
||||
final AssetBundle bundle;
|
||||
|
||||
static AssetBundle of(BuildContext context) {
|
||||
DefaultAssetBundle result = context.inheritedWidgetOfType(DefaultAssetBundle);
|
||||
return result?.bundle;
|
||||
}
|
||||
|
||||
bool updateShouldNotify(DefaultAssetBundle old) => bundle != old.bundle;
|
||||
}
|
||||
|
||||
class AssetImage extends StatelessComponent {
|
||||
AssetImage({
|
||||
Key key,
|
||||
@@ -1041,7 +1061,7 @@ class AssetImage extends StatelessComponent {
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return new ImageListener(
|
||||
image: bundle.loadImage(name),
|
||||
image: (bundle ?? DefaultAssetBundle.of(context)).loadImage(name),
|
||||
width: width,
|
||||
height: height,
|
||||
colorFilter: colorFilter,
|
||||
|
||||
Reference in New Issue
Block a user