Refactor everything to do with images (#4583)

Overview
========

This patch refactors images to achieve the following goals:

* it allows references to unresolved assets to be passed
  around (previously, almost every layer of the system had to know about
  whether an image came from an asset bundle or the network or
  elsewhere, and had to manually interact with the image cache).

* it allows decorations to use the same API for declaring images as the
  widget tree.

It requires some minor changes to call sites that use images, as
discussed below.

Widgets
-------

Change this:

```dart
      child: new AssetImage(
        name: 'my_asset.png',
        ...
      )
```

...to this:

```dart
      child: new Image(
        image: new AssetImage('my_asset.png'),
        ...
      )
```

Decorations
-----------

Change this:

```dart
      child: new DecoratedBox(
        decoration: new BoxDecoration(
          backgroundImage: new BackgroundImage(
            image: DefaultAssetBundle.of(context).loadImage('my_asset.png'),
            ...
          ),
          ...
        ),
        child: ...
      )
```

...to this:

```dart
      child: new DecoratedBox(
        decoration: new BoxDecoration(
          backgroundImage: new BackgroundImage(
            image: new AssetImage('my_asset.png'),
            ...
          ),
          ...
        ),
        child: ...
      )
```

DETAILED CHANGE LOG
===================

The following APIs have been replaced in this patch:

* The `AssetImage` and `NetworkImage` widgets have been split in two,
  with identically-named `ImageProvider` subclasses providing the
  image-loading logic, and a single `Image` widget providing all the
  widget tree logic.

* `ImageResource` is now `ImageStream`. Rather than configuring it with
  a `Future<ImageInfo>`, you complete it with an `ImageStreamCompleter`.

* `ImageCache.load` and `ImageCache.loadProvider` are replaced by
  `ImageCache.putIfAbsent`.

The following APIs have changed in this patch:

* `ImageCache` works in terms of arbitrary keys and caches
  `ImageStreamCompleter` objects using those keys. With the new model,
  you should never need to interact with the cache directly.

* `Decoration` can now be `const`. The state has moved to the
  `BoxPainter` class. Instead of a list of listeners, there's now just a
  single callback and a `dispose()` method on the painter. The callback
  is passed in to the `createBoxPainter()` method. When invoked, you
  should repaint the painter.

The following new APIs are introduced:

* `AssetBundle.loadStructuredData`.

* `SynchronousFuture`, a variant of `Future` that calls the `then`
  callback synchronously. This enables the asynchronous and
  synchronous (in-the-cache) code paths to look identical yet for the
  latter to avoid returning to the event loop mid-paint.

* `ExactAssetImage`, a variant of `AssetImage` that doesn't do anything clever.

* `ImageConfiguration`, a class that describes parameters that configure
  the `AssetImage` resolver.

The following APIs are entirely removed by this patch:

* `AssetBundle.loadImage` is gone. Use an `AssetImage` instead.

* `AssetVendor` is gone. `AssetImage` handles everything `AssetVendor`
  used to handle.

* `RawImageResource` and `AsyncImage` are gone.

The following code-level changes are performed:

* `Image`, which replaces `AsyncImage`, `NetworkImage`, `AssetImage`,
  and `RawResourceImage`, lives in `image.dart`.

* `DecoratedBox` and `Container` live in their own file now,
  `container.dart` (they reference `image.dart`).

DIRECTIONS FOR FUTURE RESEARCH
==============================

* The `ImageConfiguration` fields are mostly aspirational. Right now
  only `devicePixelRatio` and `bundle` are implemented. `locale` isn't
  even plumbed through, it will require work on the localisation logic.

* We should go through and make `BoxDecoration`, `AssetImage`, and
  `NetworkImage` objects `const` where possible.

* This patch makes supporting animated GIFs much easier.

* This patch makes it possible to create an abstract concept of an
  "Icon" that could be either an image or a font-based glyph (using
  `IconData` or similar). (see
  https://github.com/flutter/flutter/issues/4494)

RELATED ISSUES
==============

Fixes https://github.com/flutter/flutter/issues/4500
Fixes https://github.com/flutter/flutter/issues/4495
Obsoletes https://github.com/flutter/flutter/issues/4496
This commit is contained in:
Ian Hickson
2016-06-16 09:49:48 -07:00
committed by GitHub
parent 2ce57eb3ce
commit 2dfdc840b1
50 changed files with 2070 additions and 1759 deletions

View File

@@ -57,9 +57,9 @@ class FancyItemDelegate extends LazyBlockDelegate {
@override
Widget buildItem(BuildContext context, int index) {
if (index % 2 == 0)
return new FancyImageItem(index, key: new Key("Item $index"));
return new FancyImageItem(index, key: new Key('Item $index'));
else
return new FancyGalleryItem(index, key: new Key("Item $index"));
return new FancyGalleryItem(index, key: new Key('Item $index'));
}
@override
@@ -78,7 +78,7 @@ class ComplexLayoutState extends State<ComplexLayout> {
icon: Icons.create,
tooltip: 'Search',
onPressed: () {
print("Pressed search");
print('Pressed search');
}
),
new TopBarMenu()
@@ -88,7 +88,7 @@ class ComplexLayoutState extends State<ComplexLayout> {
children: <Widget>[
new Flexible(
child: new LazyBlock(
key: new Key("main-scroll"),
key: new Key('main-scroll'),
delegate: new FancyItemDelegate()
)
),
@@ -104,47 +104,47 @@ class TopBarMenu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new PopupMenuButton<String>(
onSelected: (String value) { print("Selected: $value"); },
onSelected: (String value) { print('Selected: $value'); },
itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
new PopupMenuItem<String>(
value: "Friends",
child: new MenuItemWithIcon(Icons.people, "Friends", "5 new")
value: 'Friends',
child: new MenuItemWithIcon(Icons.people, 'Friends', '5 new')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.event, "Events", "12 upcoming")
value: 'Events',
child: new MenuItemWithIcon(Icons.event, 'Events', '12 upcoming')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.group, "Groups", "14")
value: 'Events',
child: new MenuItemWithIcon(Icons.group, 'Groups', '14')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.image, "Pictures", "12")
value: 'Events',
child: new MenuItemWithIcon(Icons.image, 'Pictures', '12')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.near_me, "Nearby", "33")
value: 'Events',
child: new MenuItemWithIcon(Icons.near_me, 'Nearby', '33')
),
new PopupMenuItem<String>(
value: "Friends",
child: new MenuItemWithIcon(Icons.people, "Friends", "5")
value: 'Friends',
child: new MenuItemWithIcon(Icons.people, 'Friends', '5')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.event, "Events", "12")
value: 'Events',
child: new MenuItemWithIcon(Icons.event, 'Events', '12')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.group, "Groups", "14")
value: 'Events',
child: new MenuItemWithIcon(Icons.group, 'Groups', '14')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.image, "Pictures", "12")
value: 'Events',
child: new MenuItemWithIcon(Icons.image, 'Pictures', '12')
),
new PopupMenuItem<String>(
value: "Events",
child: new MenuItemWithIcon(Icons.near_me, "Nearby", "33")
value: 'Events',
child: new MenuItemWithIcon(Icons.near_me, 'Nearby', '33')
)
]
);
@@ -182,7 +182,7 @@ class FancyImageItem extends StatelessWidget {
Widget build(BuildContext context) {
return new BlockBody(
children: <Widget>[
new UserHeader("Ali Connors $index"),
new UserHeader('Ali Connors $index'),
new ItemDescription(),
new ItemImageBox(),
new InfoBar(),
@@ -205,7 +205,7 @@ class FancyGalleryItem extends StatelessWidget {
Widget build(BuildContext context) {
return new BlockBody(
children: <Widget>[
new UserHeader("Ali Connors"),
new UserHeader('Ali Connors'),
new ItemGalleryBox(index),
new InfoBar(),
new Padding(
@@ -227,8 +227,8 @@ class InfoBar extends StatelessWidget {
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new MiniIconWithText(Icons.thumb_up, "42"),
new Text("3 Comments", style: Theme.of(context).textTheme.caption)
new MiniIconWithText(Icons.thumb_up, '42'),
new Text('3 Comments', style: Theme.of(context).textTheme.caption)
]
)
);
@@ -243,9 +243,9 @@ class IconBar extends StatelessWidget {
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new IconWithText(Icons.thumb_up, "Like"),
new IconWithText(Icons.comment, "Comment"),
new IconWithText(Icons.share, "Share"),
new IconWithText(Icons.thumb_up, 'Like'),
new IconWithText(Icons.comment, 'Comment'),
new IconWithText(Icons.share, 'Share'),
]
)
);
@@ -263,7 +263,7 @@ class IconWithText extends StatelessWidget {
return new Row(
mainAxisAlignment: MainAxisAlignment.collapse,
children: <Widget>[
new IconButton(icon: icon, onPressed: () { print("Pressed $title button"); } ),
new IconButton(icon: icon, onPressed: () { print('Pressed $title button'); } ),
new Text(title)
]
);
@@ -325,8 +325,8 @@ class UserHeader extends StatelessWidget {
children: <Widget>[
new Padding(
padding: new EdgeInsets.only(right: 8.0),
child: new AssetImage(
name: "packages/flutter_gallery_assets/ali_connors_sml.png",
child: new Image(
image: new AssetImage('packages/flutter_gallery_assets/ali_connors_sml.png'),
width: 32.0,
height: 32.0
)
@@ -340,13 +340,13 @@ class UserHeader extends StatelessWidget {
style: Theme.of(context).textTheme.body1,
children: <TextSpan>[
new TextSpan(text: userName, style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: " shared a new "),
new TextSpan(text: "photo", style: new TextStyle(fontWeight: FontWeight.bold))
new TextSpan(text: ' shared a new '),
new TextSpan(text: 'photo', style: new TextStyle(fontWeight: FontWeight.bold))
]
)),
new Row(
children: <Widget>[
new Text("Yesterday at 11:55 • ", style: Theme.of(context).textTheme.caption),
new Text('Yesterday at 11:55 • ', style: Theme.of(context).textTheme.caption),
new Icon(icon: Icons.people, size: 16.0, color: Theme.of(context).textTheme.caption.color)
]
)
@@ -365,7 +365,7 @@ class ItemDescription extends StatelessWidget {
Widget build(BuildContext context) {
return new Padding(
padding: new EdgeInsets.all(8.0),
child: new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
child: new Text('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.')
);
}
}
@@ -383,8 +383,8 @@ class ItemImageBox extends StatelessWidget {
children: <Widget>[
new SizedBox(
height: 230.0,
child: new AssetImage(
name: "packages/flutter_gallery_assets/top_10_australian_beaches.png"
child: new Image(
image: new AssetImage('packages/flutter_gallery_assets/top_10_australian_beaches.png')
)
),
new Theme(
@@ -392,8 +392,8 @@ class ItemImageBox extends StatelessWidget {
child: new Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new IconButton(icon: Icons.edit, onPressed: () { print("Pressed edit button"); }),
new IconButton(icon: Icons.zoom_in, onPressed: () { print("Pressed zoom button"); })
new IconButton(icon: Icons.edit, onPressed: () { print('Pressed edit button'); }),
new IconButton(icon: Icons.zoom_in, onPressed: () { print('Pressed zoom button'); })
]
)
),
@@ -411,11 +411,11 @@ class ItemImageBox extends StatelessWidget {
style: new TextStyle(color: Colors.white),
children: <TextSpan>[
new TextSpan(
text: "Photo by "
text: 'Photo by '
),
new TextSpan(
style: new TextStyle(fontWeight: FontWeight.bold),
text: "Magic Mike"
text: 'Magic Mike'
)
]
)
@@ -430,9 +430,9 @@ class ItemImageBox extends StatelessWidget {
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Text("Where can you find that amazing sunset?", style: Theme.of(context).textTheme.body2),
new Text("The sun sets over stinson beach", style: Theme.of(context).textTheme.body1),
new Text("flutter.io/amazingsunsets", style: Theme.of(context).textTheme.caption)
new Text('Where can you find that amazing sunset?', style: Theme.of(context).textTheme.body2),
new Text('The sun sets over stinson beach', style: Theme.of(context).textTheme.body1),
new Text('flutter.io/amazingsunsets', style: Theme.of(context).textTheme.caption)
]
)
)
@@ -451,7 +451,7 @@ class ItemGalleryBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<String> tabNames = <String>[
"A", "B", "C", "D"
'A', 'B', 'C', 'D'
];
return new SizedBox(
@@ -464,7 +464,7 @@ class ItemGalleryBox extends StatelessWidget {
child: new TabBarView<String>(
children: tabNames.map((String tabName) {
return new Container(
key: new Key("Tab $index - $tabName"),
key: new Key('Tab $index - $tabName'),
child: new Padding(
padding: new EdgeInsets.all(8.0),
child: new Card(
@@ -484,16 +484,16 @@ class ItemGalleryBox extends StatelessWidget {
children: <Widget>[
new IconButton(
icon: Icons.share,
onPressed: () { print("Pressed share"); }
onPressed: () { print('Pressed share'); }
),
new IconButton(
icon: Icons.event,
onPressed: () { print("Pressed event"); }
onPressed: () { print('Pressed event'); }
),
new Flexible(
child: new Padding(
padding: new EdgeInsets.only(left: 8.0),
child: new Text("This is item $tabName")
child: new Text('This is item $tabName')
)
)
]
@@ -531,11 +531,11 @@ class BottomBar extends StatelessWidget {
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new BottomBarButton(Icons.new_releases, "News"),
new BottomBarButton(Icons.people, "Requests"),
new BottomBarButton(Icons.chat, "Messenger"),
new BottomBarButton(Icons.bookmark, "Bookmark"),
new BottomBarButton(Icons.alarm, "Alarm")
new BottomBarButton(Icons.new_releases, 'News'),
new BottomBarButton(Icons.people, 'Requests'),
new BottomBarButton(Icons.chat, 'Messenger'),
new BottomBarButton(Icons.bookmark, 'Bookmark'),
new BottomBarButton(Icons.alarm, 'Alarm')
]
)
);
@@ -556,7 +556,7 @@ class BottomBarButton extends StatelessWidget {
children: <Widget>[
new IconButton(
icon: icon,
onPressed: () { print("Pressed: $title"); }
onPressed: () { print('Pressed: $title'); }
),
new Text(title, style: Theme.of(context).textTheme.caption)
]

View File

@@ -429,7 +429,7 @@ class CardCollectionState extends State<CardCollection> {
if (_sunshine) {
cardCollection = new Stack(
children: <Widget>[
new Column(children: <Widget>[new NetworkImage(src: _sunshineURL)]),
new Column(children: <Widget>[new Image(image: new NetworkImage(_sunshineURL))]),
new ShaderMask(child: cardCollection, shaderCallback: _createShader)
]
);

View File

@@ -157,8 +157,8 @@ class WeatherButton extends StatelessWidget {
child: new InkWell(
onTap: onPressed,
child: new Center(
child: new AssetImage(
name: icon,
child: new Image(
image: new AssetImage(icon),
width: _kWeatherIconSize,
height: _kWeatherIconSize
)