Commit 54bf20e1 authored by Adam Barth's avatar Adam Barth

Add DefaultAssetBundle

Now AssetImage will look in the default bundle if you don't supply an explicit
bundle.

Fixes #680
parent 4816997b
...@@ -2,22 +2,12 @@ ...@@ -2,22 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'theme.dart'; import 'theme.dart';
import 'icon_theme.dart'; import 'icon_theme.dart';
import 'icon_theme_data.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 { class Icon extends StatelessComponent {
Icon({ Icon({
Key key, Key key,
...@@ -66,7 +56,6 @@ class Icon extends StatelessComponent { ...@@ -66,7 +56,6 @@ class Icon extends StatelessComponent {
String density = 'drawable-xxhdpi'; String density = 'drawable-xxhdpi';
String colorSuffix = _getColorSuffix(context); String colorSuffix = _getColorSuffix(context);
return new AssetImage( return new AssetImage(
bundle: _iconBundle,
name: '$category/$density/ic_${subtype}_${colorSuffix}_${size}dp.png', name: '$category/$density/ic_${subtype}_${colorSuffix}_${size}dp.png',
width: size.toDouble(), width: size.toDouble(),
height: size.toDouble(), height: size.toDouble(),
......
...@@ -19,6 +19,15 @@ const TextStyle _errorTextStyle = const TextStyle( ...@@ -19,6 +19,15 @@ const TextStyle _errorTextStyle = const TextStyle(
decorationStyle: TextDecorationStyle.double 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 { class MaterialApp extends StatefulComponent {
MaterialApp({ MaterialApp({
Key key, Key key,
...@@ -68,12 +77,15 @@ class _MaterialAppState extends State<MaterialApp> { ...@@ -68,12 +77,15 @@ class _MaterialAppState extends State<MaterialApp> {
data: config.theme ?? new ThemeData.fallback(), data: config.theme ?? new ThemeData.fallback(),
child: new DefaultTextStyle( child: new DefaultTextStyle(
style: _errorTextStyle, style: _errorTextStyle,
child: new Title( child: new DefaultAssetBundle(
title: config.title, bundle: _defaultBundle,
child: new Navigator( child: new Title(
key: _navigator, title: config.title,
routes: config.routes, child: new Navigator(
onGenerateRoute: config.onGenerateRoute key: _navigator,
routes: config.routes,
onGenerateRoute: config.onGenerateRoute
)
) )
) )
) )
......
...@@ -1017,6 +1017,26 @@ class NetworkImage extends StatelessComponent { ...@@ -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 { class AssetImage extends StatelessComponent {
AssetImage({ AssetImage({
Key key, Key key,
...@@ -1041,7 +1061,7 @@ class AssetImage extends StatelessComponent { ...@@ -1041,7 +1061,7 @@ class AssetImage extends StatelessComponent {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new ImageListener( return new ImageListener(
image: bundle.loadImage(name), image: (bundle ?? DefaultAssetBundle.of(context)).loadImage(name),
width: width, width: width,
height: height, height: height,
colorFilter: colorFilter, colorFilter: colorFilter,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment