From 54bf20e10776f5f3a4feff36f9a4f80883581599 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 22 Oct 2015 17:02:06 -0700 Subject: [PATCH] Add DefaultAssetBundle Now AssetImage will look in the default bundle if you don't supply an explicit bundle. Fixes #680 --- packages/flutter/lib/src/material/icon.dart | 11 --------- .../lib/src/material/material_app.dart | 24 ++++++++++++++----- packages/flutter/lib/src/widgets/basic.dart | 22 ++++++++++++++++- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/packages/flutter/lib/src/material/icon.dart b/packages/flutter/lib/src/material/icon.dart index da620a6521..e5e47b45b4 100644 --- a/packages/flutter/lib/src/material/icon.dart +++ b/packages/flutter/lib/src/material/icon.dart @@ -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(), diff --git a/packages/flutter/lib/src/material/material_app.dart b/packages/flutter/lib/src/material/material_app.dart index afe72df031..75a59ea546 100644 --- a/packages/flutter/lib/src/material/material_app.dart +++ b/packages/flutter/lib/src/material/material_app.dart @@ -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 { 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 + ) ) ) ) diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 343552dd50..d5b521e457 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -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,