drawer_item.dart 2.71 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'dart:sky' as sky;

7
import 'package:sky/painting.dart';
8
import 'package:sky/material.dart';
9 10 11 12 13 14 15 16
import 'package:sky/src/widgets/basic.dart';
import 'package:sky/src/widgets/button_base.dart';
import 'package:sky/src/widgets/default_text_style.dart';
import 'package:sky/src/widgets/framework.dart';
import 'package:sky/src/widgets/gesture_detector.dart';
import 'package:sky/src/widgets/icon.dart';
import 'package:sky/src/widgets/ink_well.dart';
import 'package:sky/src/widgets/theme.dart';
17

18
typedef void OnPressedFunction();
19

20
class DrawerItem extends ButtonBase {
21
  DrawerItem({ Key key, this.icon, this.child, this.onPressed, this.selected: false })
22 23 24
    : super(key: key);

  String icon;
25
  Widget child;
26
  OnPressedFunction onPressed;
27 28
  bool selected;

29
  void syncConstructorArguments(DrawerItem source) {
30
    icon = source.icon;
31
    child = source.child;
32 33
    onPressed = source.onPressed;
    selected = source.selected;
34
    super.syncConstructorArguments(source);
35 36 37 38 39 40 41 42 43 44 45 46 47 48
  }

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

  Color _getBackgroundColor(ThemeData themeData) {
    if (highlight)
      return themeData.highlightColor;
    if (selected)
      return themeData.selectedColor;
49
    return Colors.transparent;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  }

  sky.ColorFilter _getColorFilter(ThemeData themeData) {
    if (selected)
      return new sky.ColorFilter.mode(themeData.primaryColor, sky.TransferMode.srcATop);
    return new sky.ColorFilter.mode(const Color(0x73000000), sky.TransferMode.dstIn);
  }

  Widget buildContent() {
    ThemeData themeData = Theme.of(this);

    List<Widget> flexChildren = new List<Widget>();
    if (icon != null) {
      flexChildren.add(
        new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new Icon(
            type: icon,
            size: 24,
            colorFilter: _getColorFilter(themeData))
        )
      );
    }
    flexChildren.add(
      new Flexible(
        child: new Padding(
          padding: const EdgeDims.symmetric(horizontal: 16.0),
          child: new DefaultTextStyle(
            style: _getTextStyle(themeData),
79
            child: child
80 81 82 83 84
          )
        )
      )
    );

85 86
    return new GestureDetector(
      onTap: onPressed,
87 88 89 90
      child: new Container(
        height: 48.0,
        decoration: new BoxDecoration(backgroundColor: _getBackgroundColor(themeData)),
        child: new InkWell(
91
          child: new Row(flexChildren)
92 93 94 95 96
        )
      )
    );
  }
}