OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import '../mojo/asset_bundle.dart'; | 5 import '../mojo/asset_bundle.dart'; |
6 import 'basic.dart'; | 6 import 'basic.dart'; |
| 7 import 'theme.dart'; |
| 8 import 'widget.dart'; |
| 9 |
| 10 enum IconThemeColor { white, black } |
| 11 |
| 12 class IconThemeData { |
| 13 const IconThemeData({ this.color }); |
| 14 final IconThemeColor color; |
| 15 } |
| 16 |
| 17 class IconTheme extends Inherited { |
| 18 |
| 19 IconTheme({ |
| 20 String key, |
| 21 this.data, |
| 22 Widget child |
| 23 }) : super(key: key, child: child) { |
| 24 assert(data != null); |
| 25 assert(child != null); |
| 26 } |
| 27 |
| 28 final IconThemeData data; |
| 29 |
| 30 static IconThemeData of(Component component) { |
| 31 IconTheme result = component.inheritedOfType(IconTheme); |
| 32 return result == null ? null : result.data; |
| 33 } |
| 34 |
| 35 bool syncShouldNotify(IconTheme old) => data != old.data; |
| 36 |
| 37 } |
7 | 38 |
8 AssetBundle _initIconBundle() { | 39 AssetBundle _initIconBundle() { |
9 if (rootBundle != null) | 40 if (rootBundle != null) |
10 return rootBundle; | 41 return rootBundle; |
11 const String _kAssetBase = '/packages/sky/assets/material-design-icons/'; | 42 const String _kAssetBase = '/packages/sky/assets/material-design-icons/'; |
12 return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase)); | 43 return new NetworkAssetBundle(Uri.base.resolve(_kAssetBase)); |
13 } | 44 } |
14 | 45 |
15 final AssetBundle _iconBundle = _initIconBundle(); | 46 final AssetBundle _iconBundle = _initIconBundle(); |
16 | 47 |
17 class Icon extends Component { | 48 class Icon extends Component { |
18 Icon({ String key, this.size, this.type: '' }) : super(key: key); | 49 Icon({ String key, this.size, this.type: '', this.color }) : super(key: key); |
19 | 50 |
20 final int size; | 51 final int size; |
21 final String type; | 52 final String type; |
| 53 final IconThemeColor color; |
| 54 |
| 55 String get colorSuffix { |
| 56 IconThemeColor iconThemeColor = color; |
| 57 if (iconThemeColor == null) { |
| 58 IconThemeData iconThemeData = IconTheme.of(this); |
| 59 iconThemeColor = iconThemeData == null ? null : iconThemeData.color; |
| 60 } |
| 61 if (iconThemeColor == null) { |
| 62 ThemeBrightness themeBrightness = Theme.of(this).brightness; |
| 63 iconThemeColor = themeBrightness == ThemeBrightness.dark ? IconThemeColor.
white : IconThemeColor.black; |
| 64 } |
| 65 switch(iconThemeColor) { |
| 66 case IconThemeColor.white: |
| 67 return "white"; |
| 68 case IconThemeColor.black: |
| 69 return "black"; |
| 70 } |
| 71 } |
22 | 72 |
23 Widget build() { | 73 Widget build() { |
24 String category = ''; | 74 String category = ''; |
25 String subtype = ''; | 75 String subtype = ''; |
26 List<String> parts = type.split('/'); | 76 List<String> parts = type.split('/'); |
27 if (parts.length == 2) { | 77 if (parts.length == 2) { |
28 category = parts[0]; | 78 category = parts[0]; |
29 subtype = parts[1]; | 79 subtype = parts[1]; |
30 } | 80 } |
31 // TODO(eseidel): This clearly isn't correct. Not sure what would be. | 81 // TODO(eseidel): This clearly isn't correct. Not sure what would be. |
32 // Should we use the ios images on ios? | 82 // Should we use the ios images on ios? |
33 String density = 'drawable-xxhdpi'; | 83 String density = 'drawable-xxhdpi'; |
34 return new AssetImage( | 84 return new AssetImage( |
35 bundle: _iconBundle, | 85 bundle: _iconBundle, |
36 name: '${category}/${density}/ic_${subtype}_${size}dp.png', | 86 name: '${category}/${density}/ic_${subtype}_${colorSuffix}_${size}dp.png', |
37 size: new Size(size.toDouble(), size.toDouble()) | 87 size: new Size(size.toDouble(), size.toDouble()) |
38 ); | 88 ); |
39 } | 89 } |
40 } | 90 } |
OLD | NEW |