icon.dart 2.26 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/widgets.dart';
6 7

import 'theme.dart';
Adam Barth's avatar
Adam Barth committed
8 9
import 'icon_theme.dart';
import 'icon_theme_data.dart';
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24
enum IconSize {
  s18,
  s24,
  s36,
  s48,
}

const Map<IconSize, int> _kIconSize = const <IconSize, int>{
  IconSize.s18: 18,
  IconSize.s24: 24,
  IconSize.s36: 36,
  IconSize.s48: 48,
};

25
class Icon extends StatelessComponent {
26
  Icon({
27
    Key key,
28
    this.size: IconSize.s24,
29
    this.icon: '',
30 31
    this.color,
    this.colorFilter
Hixie's avatar
Hixie committed
32 33
  }) : super(key: key) {
    assert(size != null);
34
    assert(icon != null);
Hixie's avatar
Hixie committed
35
  }
36

37
  final IconSize size;
38
  final String icon;
39
  final IconThemeColor color;
Adam Barth's avatar
Adam Barth committed
40
  final ColorFilter colorFilter;
41

42
  String _getColorSuffix(BuildContext context) {
43 44
    IconThemeColor iconThemeColor = color;
    if (iconThemeColor == null) {
45
      IconThemeData iconThemeData = IconTheme.of(context);
46 47 48
      iconThemeColor = iconThemeData == null ? null : iconThemeData.color;
    }
    if (iconThemeColor == null) {
49
      ThemeBrightness themeBrightness = Theme.of(context).brightness;
50 51 52 53 54 55 56 57 58 59
      iconThemeColor = themeBrightness == ThemeBrightness.dark ? IconThemeColor.white : IconThemeColor.black;
    }
    switch(iconThemeColor) {
      case IconThemeColor.white:
        return "white";
      case IconThemeColor.black:
        return "black";
    }
  }

60
  Widget build(BuildContext context) {
61 62
    String category = '';
    String subtype = '';
63
    List<String> parts = icon.split('/');
64 65 66 67 68 69 70
    if (parts.length == 2) {
      category = parts[0];
      subtype = parts[1];
    }
    // TODO(eseidel): This clearly isn't correct.  Not sure what would be.
    // Should we use the ios images on ios?
    String density = 'drawable-xxhdpi';
71
    String colorSuffix = _getColorSuffix(context);
72
    int iconSize = _kIconSize[size];
73
    return new AssetImage(
74 75 76
      name: '$category/$density/ic_${subtype}_${colorSuffix}_${iconSize}dp.png',
      width: iconSize.toDouble(),
      height: iconSize.toDouble(),
77 78 79
      colorFilter: colorFilter
    );
  }
Hixie's avatar
Hixie committed
80 81 82

  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
83
    description.add('$icon');
Hixie's avatar
Hixie committed
84 85
    description.add('size: $size');
  }
86
}