drawer_item.dart 2.5 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 StatelessWidget {
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
  /// The icon to display before the child widget.
24
  final IconData icon;
25 26

  /// The widget below this widget in the tree.
27
  final Widget child;
28

29
  final VoidCallback onPressed;
30

31
  final bool selected;
32

Adam Barth's avatar
Adam Barth committed
33
  Color _getIconColor(ThemeData themeData) {
34 35 36 37 38 39 40 41 42 43 44 45 46
    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
47 48 49
    }
  }

50
  TextStyle _getTextStyle(ThemeData themeData) {
51
    TextStyle result = themeData.textTheme.body2;
52
    if (selected) {
53 54 55 56 57 58
      switch (themeData.brightness) {
        case ThemeBrightness.light:
          return result.copyWith(color: themeData.primaryColor);
        case ThemeBrightness.dark:
          return result.copyWith(color: themeData.accentColor);
      }
59
    }
60 61 62
    return result;
  }

63
  @override
64
  Widget build(BuildContext context) {
65
    assert(debugCheckHasMaterial(context));
66
    ThemeData themeData = Theme.of(context);
67

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

Hixie's avatar
Hixie committed
92 93 94 95 96 97 98
    return new MergeSemantics(
      child: new Container(
        height: 48.0,
        child: new InkWell(
          onTap: onPressed,
          child: new Row(children: children)
        )
99 100 101
      )
    );
  }
102

103
}