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

15 16 17 18 19 20 21
/// An item in a material design drawer.
///
/// Part of the material design [Drawer].
///
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
22
///
23 24 25
///  * [Drawer]
///  * [DrawerHeader]
///  * <https://www.google.com/design/spec/patterns/navigation-drawer.html>
26
class DrawerItem extends StatelessWidget {
27 28 29
  /// Creates a material design drawer item.
  ///
  /// Requires one of its ancestors to be a [Material] widget.
30 31 32 33 34 35 36
  const DrawerItem({
    Key key,
    this.icon,
    this.child,
    this.onPressed,
    this.selected: false
  }) : super(key: key);
37

38
  /// The icon to display before the child widget.
39
  final IconData icon;
40 41

  /// The widget below this widget in the tree.
42
  final Widget child;
43

44
  /// Called when the user taps this drawer item.
45 46
  ///
  /// If null, the drawer item is displayed as disabled.
47
  final VoidCallback onPressed;
48

49 50 51 52
  /// Whether this drawer item is currently selected.
  ///
  /// The currently selected item is highlighted to distinguish it from other
  /// drawer items.
53
  final bool selected;
54

Adam Barth's avatar
Adam Barth committed
55
  Color _getIconColor(ThemeData themeData) {
56 57 58 59 60 61 62 63 64 65 66 67 68
    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
69 70 71
    }
  }

72
  TextStyle _getTextStyle(ThemeData themeData) {
73
    TextStyle result = themeData.textTheme.body2;
74
    if (selected) {
75 76 77 78 79 80
      switch (themeData.brightness) {
        case ThemeBrightness.light:
          return result.copyWith(color: themeData.primaryColor);
        case ThemeBrightness.dark:
          return result.copyWith(color: themeData.accentColor);
      }
81
    }
82 83 84
    return result;
  }

85
  @override
86
  Widget build(BuildContext context) {
87
    assert(debugCheckHasMaterial(context));
88
    ThemeData themeData = Theme.of(context);
89

90
    List<Widget> children = <Widget>[];
91
    if (icon != null) {
92
      children.add(
93
        new Padding(
94
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
95
          child: new Icon(
96
            icon: icon,
Adam Barth's avatar
Adam Barth committed
97
            color: _getIconColor(themeData)
98
          )
99 100 101
        )
      );
    }
102
    children.add(
103 104
      new Flexible(
        child: new Padding(
105
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
106
          child: new AnimatedDefaultTextStyle(
107
            style: _getTextStyle(themeData),
108
            duration: kThemeChangeDuration,
109
            child: child
110 111 112 113 114
          )
        )
      )
    );

Hixie's avatar
Hixie committed
115 116 117 118 119 120 121
    return new MergeSemantics(
      child: new Container(
        height: 48.0,
        child: new InkWell(
          onTap: onPressed,
          child: new Row(children: children)
        )
122 123 124
      )
    );
  }
125

126
}