drawer_item.dart 2.15 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 9 10
import 'icon.dart';
import 'ink_well.dart';
import 'theme.dart';
11

12
class DrawerItem extends StatelessComponent {
13
  const DrawerItem({ Key key, this.icon, this.child, this.onPressed, this.selected: false })
14 15
    : super(key: key);

16 17
  final String icon;
  final Widget child;
18
  final VoidCallback onPressed;
19
  final bool selected;
20 21 22

  TextStyle _getTextStyle(ThemeData themeData) {
    TextStyle result = themeData.text.body2;
23
    if (selected)
24 25 26 27
      result = result.copyWith(color: themeData.primaryColor);
    return result;
  }

28 29
  Color _getBackgroundColor(ThemeData themeData, { bool highlight }) {
    if (highlight)
30
      return themeData.highlightColor;
31
    if (selected)
32
      return themeData.selectedColor;
33
    return Colors.transparent;
34 35
  }

Adam Barth's avatar
Adam Barth committed
36
  ColorFilter _getColorFilter(ThemeData themeData) {
37
    if (selected)
Adam Barth's avatar
Adam Barth committed
38 39
      return new ColorFilter.mode(themeData.primaryColor, TransferMode.srcATop);
    return new ColorFilter.mode(const Color(0x73000000), TransferMode.dstIn);
40 41
  }

42
  Widget build(BuildContext context) {
43
    ThemeData themeData = Theme.of(context);
44 45

    List<Widget> flexChildren = new List<Widget>();
46
    if (icon != null) {
47 48 49 50
      flexChildren.add(
        new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new Icon(
51
            icon: icon,
52 53
            colorFilter: _getColorFilter(themeData)
          )
54 55 56 57 58 59 60 61 62
        )
      );
    }
    flexChildren.add(
      new Flexible(
        child: new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new DefaultTextStyle(
            style: _getTextStyle(themeData),
63
            child: child
64 65 66 67 68
          )
        )
      )
    );

69 70 71
    return new Container(
      height: 48.0,
      child: new InkWell(
72 73 74
        onTap: onPressed,
        defaultColor: _getBackgroundColor(themeData, highlight: false),
        highlightColor: _getBackgroundColor(themeData, highlight: true),
75
        child: new Row(flexChildren)
76 77 78
      )
    );
  }
79

80
}