drawer_item.dart 2.38 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';
8
import 'debug.dart';
9
import 'icon.dart';
10
import 'icons.dart';
11 12
import 'ink_well.dart';
import 'theme.dart';
13

14
class DrawerItem extends StatelessComponent {
15 16 17 18 19 20 21
  const DrawerItem({
    Key key,
    this.icon,
    this.child,
    this.onPressed,
    this.selected: false
  }) : super(key: key);
22

23
  final IconData icon;
24
  final Widget child;
25
  final VoidCallback onPressed;
26
  final bool selected;
27

Adam Barth's avatar
Adam Barth committed
28
  Color _getIconColor(ThemeData themeData) {
29 30 31 32 33 34 35 36 37 38 39 40 41
    switch (themeData.brightness) {
      case ThemeBrightness.light:
        if (selected)
          return themeData.primaryColor;
        if (onPressed == null)
          return Colors.black26;
        return Colors.black45;
      case ThemeBrightness.dark:
        if (selected)
          return themeData.accentColor;
        if (onPressed == null)
          return Colors.white30;
        return null; // use default icon theme colour unmodified
42 43 44
    }
  }

45 46
  TextStyle _getTextStyle(ThemeData themeData) {
    TextStyle result = themeData.text.body2;
47
    if (selected) {
48 49 50 51 52 53
      switch (themeData.brightness) {
        case ThemeBrightness.light:
          return result.copyWith(color: themeData.primaryColor);
        case ThemeBrightness.dark:
          return result.copyWith(color: themeData.accentColor);
      }
54
    }
55 56 57
    return result;
  }

58
  Widget build(BuildContext context) {
59
    assert(debugCheckHasMaterial(context));
60
    ThemeData themeData = Theme.of(context);
61

62
    List<Widget> children = <Widget>[];
63
    if (icon != null) {
64
      children.add(
65 66 67
        new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new Icon(
68
            icon: icon,
Adam Barth's avatar
Adam Barth committed
69
            color: _getIconColor(themeData)
70
          )
71 72 73
        )
      );
    }
74
    children.add(
75 76 77 78 79
      new Flexible(
        child: new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new DefaultTextStyle(
            style: _getTextStyle(themeData),
80
            child: child
81 82 83 84 85
          )
        )
      )
    );

Hixie's avatar
Hixie committed
86 87 88 89 90 91 92
    return new MergeSemantics(
      child: new Container(
        height: 48.0,
        child: new InkWell(
          onTap: onPressed,
          child: new Row(children: children)
        )
93 94 95
      )
    );
  }
96

97
}