icon.dart 2.75 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 'colors.dart';
Adam Barth's avatar
Adam Barth committed
8 9
import 'icon_theme.dart';
import 'icon_theme_data.dart';
10
import 'theme.dart';
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25
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,
};

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

38
  final IconSize size;
39
  final String icon;
Adam Barth's avatar
Adam Barth committed
40 41
  final IconThemeColor colorTheme;
  final Color color;
42

43
  IconThemeColor _getIconThemeColor(BuildContext context) {
Adam Barth's avatar
Adam Barth committed
44
    IconThemeColor iconThemeColor = colorTheme;
45
    if (iconThemeColor == null) {
46
      IconThemeData iconThemeData = IconTheme.of(context);
47 48 49
      iconThemeColor = iconThemeData == null ? null : iconThemeData.color;
    }
    if (iconThemeColor == null) {
50
      ThemeBrightness themeBrightness = Theme.of(context).brightness;
51 52
      iconThemeColor = themeBrightness == ThemeBrightness.dark ? IconThemeColor.white : IconThemeColor.black;
    }
53
    return iconThemeColor;
54 55
  }

56
  Widget build(BuildContext context) {
57 58
    String category = '';
    String subtype = '';
59
    List<String> parts = icon.split('/');
60 61 62 63
    if (parts.length == 2) {
      category = parts[0];
      subtype = parts[1];
    }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    final IconThemeColor iconThemeColor = _getIconThemeColor(context);
    final int iconSize = _kIconSize[size];

    String colorSuffix;
    switch(iconThemeColor) {
      case IconThemeColor.black:
        colorSuffix = "black";
        break;
      case IconThemeColor.white:
        colorSuffix = "white";
        break;
    }

    Color iconColor = color;
    final int iconAlpha = (255.0 * (IconTheme.of(context)?.clampedOpacity ?? 1.0)).round();
    if (iconAlpha != 255) {
      if (color != null)
        iconColor = color.withAlpha(iconAlpha);
      else {
        switch(iconThemeColor) {
          case IconThemeColor.black:
            iconColor = Colors.black.withAlpha(iconAlpha);
            break;
          case IconThemeColor.white:
            iconColor = Colors.white.withAlpha(iconAlpha);
            break;
        }
      }
    }

94
    return new AssetImage(
95
      name: '$category/ic_${subtype}_${colorSuffix}_${iconSize}dp.png',
96 97
      width: iconSize.toDouble(),
      height: iconSize.toDouble(),
98
      color: iconColor
99 100
    );
  }
Hixie's avatar
Hixie committed
101 102 103

  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
104
    description.add('$icon');
Hixie's avatar
Hixie committed
105 106
    description.add('size: $size');
  }
107
}