chip.dart 3.19 KB
Newer Older
1 2 3 4 5 6 7
// 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 'package:flutter/widgets.dart';

import 'colors.dart';
8
import 'debug.dart';
9
import 'icon.dart';
10
import 'icons.dart';
Hixie's avatar
Hixie committed
11
import 'tooltip.dart';
12

13 14 15
const double _kChipHeight = 32.0;
const double _kAvatarDiamater = _kChipHeight;

16 17 18 19 20 21 22 23
const TextStyle _kLabelStyle = const TextStyle(
  inherit: false,
  fontSize: 13.0,
  fontWeight: FontWeight.w400,
  color: Colors.black87,
  textBaseline: TextBaseline.alphabetic
);

24 25 26 27 28 29 30 31 32 33
/// A material design chip.
///
/// Chips represent complex entities in small blocks, such as a contact.
///
/// Supplying a non-null [onDeleted] callback will cause the chip to include a
/// button for deleting the chip.
///
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
34
///
35
///  * [CircleAvatar]
36
///  * <https://material.google.com/components/chips.html>
37
class Chip extends StatelessWidget {
38 39 40 41
  /// Creates a material design chip.
  ///
  ///  * [onDeleted] determines whether the chip has a delete button. This
  ///    callback runs when the delete button is pressed.
42 43
  const Chip({
    Key key,
44
    this.avatar,
45 46 47 48
    this.label,
    this.onDeleted
  }) : super(key: key);

49 50 51
  /// A widget to display prior to the chip's label.
  ///
  /// Typically a [CircleAvatar] widget.
52
  final Widget avatar;
53 54 55 56

  /// The primary content of the chip.
  ///
  /// Typically a [Text] widget.
57
  final Widget label;
58 59 60

  /// Called when the user deletes the chip, e.g., by tapping the delete button.
  ///
61
  /// The delete button is included in the chip only if this callback is non-null.
62 63
  final VoidCallback onDeleted;

64
  @override
65
  Widget build(BuildContext context) {
66
    assert(debugCheckHasMaterial(context));
67
    final bool deletable = onDeleted != null;
68 69 70 71 72 73 74
    double leftPadding = 12.0;
    double rightPadding = 12.0;

    List<Widget> children = <Widget>[];

    if (avatar != null) {
      leftPadding = 0.0;
Hixie's avatar
Hixie committed
75 76
      children.add(new ExcludeSemantics(
        child: new Container(
77
          margin: const EdgeInsets.only(right: 8.0),
Hixie's avatar
Hixie committed
78 79 80 81
          width: _kAvatarDiamater,
          height: _kAvatarDiamater,
          child: avatar
        )
82 83
      ));
    }
84

85
    children.add(new DefaultTextStyle(
86 87 88
      style: _kLabelStyle,
      child: label
    ));
89 90

    if (deletable) {
91
      rightPadding = 0.0;
92 93
      children.add(new GestureDetector(
        onTap: onDeleted,
Hixie's avatar
Hixie committed
94 95 96
        child: new Tooltip(
          message: 'Delete "$label"',
          child: new Container(
97
            padding: const EdgeInsets.symmetric(horizontal: 4.0),
Hixie's avatar
Hixie committed
98
            child: new Icon(
Ian Hickson's avatar
Ian Hickson committed
99
              Icons.cancel,
100
              size: 18.0,
Hixie's avatar
Hixie committed
101 102
              color: Colors.black54
            )
103 104 105 106 107
          )
        )
      ));
    }

Hixie's avatar
Hixie committed
108 109 110 111
    return new Semantics(
      container: true,
      child: new Container(
        height: _kChipHeight,
112
        padding: new EdgeInsets.only(left: leftPadding, right: rightPadding),
Hixie's avatar
Hixie committed
113 114
        decoration: new BoxDecoration(
          backgroundColor: Colors.grey[300],
115
          borderRadius: new BorderRadius.circular(16.0)
Hixie's avatar
Hixie committed
116 117 118
        ),
        child: new Row(
          children: children,
119
          mainAxisSize: MainAxisSize.min
Hixie's avatar
Hixie committed
120
        )
121
      )
122 123 124
    );
  }
}