mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

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
144 lines
4.5 KiB
Dart
144 lines
4.5 KiB
Dart
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
class TravelDestination {
|
|
const TravelDestination({ this.assetName, this.title, this.description });
|
|
|
|
final String assetName;
|
|
final String title;
|
|
final List<String> description;
|
|
|
|
bool get isValid => assetName != null && title != null && description?.length == 3;
|
|
}
|
|
|
|
final List<TravelDestination> destinations = <TravelDestination>[
|
|
const TravelDestination(
|
|
assetName: 'packages/flutter_gallery_assets/top_10_australian_beaches.png',
|
|
title: 'Top 10 Australian beaches',
|
|
description: const <String>[
|
|
'Number 10',
|
|
'Whitehaven Beach',
|
|
'Whitsunday Island, Whitsunday Islands'
|
|
]
|
|
),
|
|
const TravelDestination(
|
|
assetName: 'packages/flutter_gallery_assets/kangaroo_valley_safari.png',
|
|
title: 'Kangaroo Valley Safari',
|
|
description: const <String>[
|
|
'2031 Moss Vale Road',
|
|
'Kangaroo Valley 2577',
|
|
'New South Wales'
|
|
]
|
|
)
|
|
];
|
|
|
|
class TravelDestinationItem extends StatelessWidget {
|
|
TravelDestinationItem({ Key key, this.destination }) : super(key: key) {
|
|
assert(destination != null && destination.isValid);
|
|
}
|
|
|
|
static final double height = 328.0;
|
|
final TravelDestination destination;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
ThemeData theme = Theme.of(context);
|
|
TextStyle titleStyle = theme.textTheme.headline.copyWith(color: Colors.white);
|
|
TextStyle descriptionStyle = theme.textTheme.subhead;
|
|
|
|
return new SizedBox(
|
|
height: height,
|
|
child: new Card(
|
|
child: new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
// photo and title
|
|
new SizedBox(
|
|
height: 184.0,
|
|
child: new Stack(
|
|
children: <Widget>[
|
|
new Positioned(
|
|
left: 0.0,
|
|
top: 0.0,
|
|
bottom: 0.0,
|
|
right: 0.0,
|
|
child: new Image(
|
|
image: new AssetImage(destination.assetName),
|
|
fit: ImageFit.cover
|
|
)
|
|
),
|
|
new Positioned(
|
|
bottom: 16.0,
|
|
left: 16.0,
|
|
child: new Text(destination.title, style: titleStyle)
|
|
)
|
|
]
|
|
)
|
|
),
|
|
// description and share/expore buttons
|
|
new Flexible(
|
|
child: new Padding(
|
|
padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
|
|
child: new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
// three line description
|
|
new Text(destination.description[0], style: descriptionStyle),
|
|
new Text(destination.description[1], style: descriptionStyle),
|
|
new Text(destination.description[2], style: descriptionStyle),
|
|
]
|
|
)
|
|
)
|
|
),
|
|
// share, explore buttons
|
|
// TODO(abarth): The theme and the bar should be part of card.
|
|
new ButtonTheme.bar(
|
|
child: new ButtonBar(
|
|
alignment: MainAxisAlignment.start,
|
|
children: <Widget>[
|
|
new FlatButton(
|
|
child: new Text('SHARE'),
|
|
onPressed: () { /* do nothing */ }
|
|
),
|
|
new FlatButton(
|
|
child: new Text('EXPLORE'),
|
|
onPressed: () { /* do nothing */ }
|
|
),
|
|
]
|
|
)
|
|
),
|
|
]
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
class CardsDemo extends StatelessWidget {
|
|
static const String routeName = '/cards';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return new Scaffold(
|
|
appBar: new AppBar(
|
|
title: new Text('Travel stream')
|
|
),
|
|
body: new ScrollableList(
|
|
itemExtent: TravelDestinationItem.height,
|
|
padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
|
|
children: destinations.map((TravelDestination destination) {
|
|
return new Container(
|
|
margin: const EdgeInsets.only(bottom: 8.0),
|
|
child: new TravelDestinationItem(destination: destination)
|
|
);
|
|
})
|
|
.toList()
|
|
)
|
|
);
|
|
}
|
|
}
|