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
312 lines
10 KiB
Dart
312 lines
10 KiB
Dart
// Copyright 2015 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';
|
|
|
|
class _ContactCategory extends StatelessWidget {
|
|
_ContactCategory({ Key key, this.icon, this.children }) : super(key: key);
|
|
|
|
final IconData icon;
|
|
final List<Widget> children;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return new Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
decoration: new BoxDecoration(
|
|
border: new Border(bottom: new BorderSide(color: Theme.of(context).dividerColor))
|
|
),
|
|
child: new DefaultTextStyle(
|
|
style: Theme.of(context).textTheme.subhead,
|
|
child: new Row(
|
|
children: <Widget>[
|
|
new SizedBox(
|
|
width: 72.0,
|
|
child: new Icon(icon: icon, color: Theme.of(context).primaryColor)
|
|
),
|
|
new Flexible(child: new Column(children: children))
|
|
]
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ContactItem extends StatelessWidget {
|
|
_ContactItem({ Key key, this.icon, this.lines, this.tooltip, this.onPressed }) : super(key: key) {
|
|
assert(lines.length > 1);
|
|
}
|
|
|
|
final IconData icon;
|
|
final List<String> lines;
|
|
final String tooltip;
|
|
final VoidCallback onPressed;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
List<Widget> columnChildren = lines.sublist(0, lines.length - 1).map((String line) => new Text(line)).toList();
|
|
columnChildren.add(new Text(lines.last, style: Theme.of(context).textTheme.caption));
|
|
|
|
List<Widget> rowChildren = <Widget>[
|
|
new Flexible(
|
|
child: new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: columnChildren
|
|
)
|
|
)
|
|
];
|
|
if (icon != null) {
|
|
rowChildren.add(new SizedBox(
|
|
width: 72.0,
|
|
child: new IconButton(icon: icon, onPressed: onPressed)
|
|
));
|
|
}
|
|
return new Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
child: new Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: rowChildren
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
class ContactsDemo extends StatefulWidget {
|
|
static const String routeName = '/contacts';
|
|
|
|
@override
|
|
ContactsDemoState createState() => new ContactsDemoState();
|
|
}
|
|
|
|
class ContactsDemoState extends State<ContactsDemo> {
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
|
final double _appBarHeight = 256.0;
|
|
AppBarBehavior _appBarBehavior = AppBarBehavior.scroll;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final double statusBarHeight = MediaQuery.of(context).padding.top;
|
|
return new Theme(
|
|
data: new ThemeData(
|
|
brightness: Brightness.light,
|
|
primarySwatch: Colors.indigo
|
|
),
|
|
child: new Scaffold(
|
|
key: _scaffoldKey,
|
|
appBarBehavior: _appBarBehavior,
|
|
appBar: new AppBar(
|
|
expandedHeight: _appBarHeight,
|
|
actions: <Widget>[
|
|
new IconButton(
|
|
icon: Icons.create,
|
|
tooltip: 'Edit',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('This is actually just a demo. Editing isn\'t supported.')
|
|
));
|
|
}
|
|
),
|
|
new PopupMenuButton<AppBarBehavior>(
|
|
onSelected: (AppBarBehavior value) {
|
|
setState(() {
|
|
_appBarBehavior = value;
|
|
});
|
|
},
|
|
itemBuilder: (BuildContext context) => <PopupMenuItem<AppBarBehavior>>[
|
|
new PopupMenuItem<AppBarBehavior>(
|
|
value: AppBarBehavior.scroll,
|
|
child: new Text('Toolbar scrolls away')
|
|
),
|
|
new PopupMenuItem<AppBarBehavior>(
|
|
value: AppBarBehavior.under,
|
|
child: new Text('Toolbar stays put')
|
|
)
|
|
]
|
|
)
|
|
],
|
|
flexibleSpace: new FlexibleSpaceBar(
|
|
title : new Text('Ali Connors'),
|
|
background: new Stack(
|
|
children: <Widget>[
|
|
new Image(
|
|
image: new AssetImage('packages/flutter_gallery_assets/ali_connors.png'),
|
|
fit: ImageFit.cover,
|
|
height: _appBarHeight
|
|
),
|
|
// This gradient ensures that the toolbar icons are distinct
|
|
// against the background image.
|
|
new DecoratedBox(
|
|
decoration: new BoxDecoration(
|
|
gradient: new LinearGradient(
|
|
begin: const FractionalOffset(0.5, 0.0),
|
|
end: const FractionalOffset(0.5, 0.30),
|
|
colors: <Color>[const Color(0x60000000), const Color(0x00000000)]
|
|
)
|
|
)
|
|
)
|
|
]
|
|
)
|
|
)
|
|
),
|
|
body: new Block(
|
|
padding: new EdgeInsets.only(top: _appBarHeight + statusBarHeight),
|
|
children: <Widget>[
|
|
new _ContactCategory(
|
|
icon: Icons.call,
|
|
children: <Widget>[
|
|
new _ContactItem(
|
|
icon: Icons.message,
|
|
tooltip: 'Send message',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('Pretend that this opened your SMS application.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'(650) 555-1234',
|
|
'Mobile'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
icon: Icons.message,
|
|
tooltip: 'Send message',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('In this demo, this button doesn\'t do anything.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'(323) 555-6789',
|
|
'Work'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
icon: Icons.message,
|
|
tooltip: 'Send message',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('Imagine if you will, a messaging application.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'(650) 555-6789',
|
|
'Home'
|
|
]
|
|
),
|
|
]
|
|
),
|
|
new _ContactCategory(
|
|
icon: Icons.email,
|
|
children: <Widget>[
|
|
new _ContactItem(
|
|
icon: Icons.email,
|
|
tooltip: 'Send personal e-mail',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('Here, your e-mail application would open.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'ali_connors@example.com',
|
|
'Personal'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
icon: Icons.email,
|
|
tooltip: 'Send work e-mail',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('This is a demo, so this button does not actually work.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'aliconnors@example.com',
|
|
'Work'
|
|
]
|
|
)
|
|
]
|
|
),
|
|
new _ContactCategory(
|
|
icon: Icons.location_on,
|
|
children: <Widget>[
|
|
new _ContactItem(
|
|
icon: Icons.map,
|
|
tooltip: 'Open map',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('This would show a map of San Francisco.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'2000 Main Street',
|
|
'San Francisco, CA',
|
|
'Home'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
icon: Icons.map,
|
|
tooltip: 'Open map',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('This would show a map of Mountain View.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'1600 Amphitheater Parkway',
|
|
'Mountain View, CA',
|
|
'Work'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
icon: Icons.map,
|
|
tooltip: 'Open map',
|
|
onPressed: () {
|
|
_scaffoldKey.currentState.showSnackBar(new SnackBar(
|
|
content: new Text('This would also show a map, if this was not a demo.')
|
|
));
|
|
},
|
|
lines: <String>[
|
|
'126 Severyns Ave',
|
|
'Mountain View, CA',
|
|
'Jet Travel'
|
|
]
|
|
)
|
|
]
|
|
),
|
|
new _ContactCategory(
|
|
icon: Icons.today,
|
|
children: <Widget>[
|
|
new _ContactItem(
|
|
lines: <String>[
|
|
'Birthday',
|
|
'January 9th, 1989'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
lines: <String>[
|
|
'Wedding anniversary',
|
|
'June 21st, 2014'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
lines: <String>[
|
|
'First day in office',
|
|
'January 20th, 2015'
|
|
]
|
|
),
|
|
new _ContactItem(
|
|
lines: <String>[
|
|
'Last day in office',
|
|
'August 9th, 2015'
|
|
]
|
|
)
|
|
]
|
|
)
|
|
]
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|