drawer_item.dart 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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;

import 'package:sky/painting/text_style.dart';
import 'package:sky/theme/colors.dart' as colors;
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/button_base.dart';
import 'package:sky/widgets/default_text_style.dart';
import 'package:sky/widgets/icon.dart';
import 'package:sky/widgets/ink_well.dart';
import 'package:sky/widgets/theme.dart';
15
import 'package:sky/widgets/framework.dart';
16

17 18
typedef EventDisposition OnPressedFunction();

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

  String icon;
  List<Widget> children;
25
  OnPressedFunction onPressed;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 79 80 81 82 83 84 85 86
  bool selected;

  void syncFields(DrawerItem source) {
    icon = source.icon;
    children = source.children;
    onPressed = source.onPressed;
    selected = source.selected;
    super.syncFields(source);
  }

  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;
    return colors.transparent;
  }

  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),
            child: new Flex(children, direction: FlexDirection.horizontal)
          )
        )
      )
    );

    return new Listener(
      onGestureTap: (_) {
        if (onPressed != null)
87 88
          return onPressed();
        return EventDisposition.ignored;
89 90 91 92 93 94 95 96 97 98 99
      },
      child: new Container(
        height: 48.0,
        decoration: new BoxDecoration(backgroundColor: _getBackgroundColor(themeData)),
        child: new InkWell(
          child: new Flex(flexChildren)
        )
      )
    );
  }
}