grid_tile_bar.dart 3.75 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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';

Adam Barth's avatar
Adam Barth committed
7
import 'colors.dart';
8 9 10 11
import 'icon_theme.dart';
import 'icon_theme_data.dart';
import 'typography.dart';

12 13 14 15 16 17 18 19 20 21 22
/// A header used in a material design [GridTile].
///
/// Typically used to add a one or two line header or footer on a [GridTile].
///
/// For a one-line header, include a [title] widget. To add a second line, also
/// include a [subtitle] wiget. Use [leading] or [trailing] to add an icon.
///
/// See also:
///
///  * [GridTile]
///  * <https://www.google.com/design/spec/components/grid-lists.html#grid-lists-specs>
23
class GridTileBar extends StatelessWidget {
24 25 26
  /// Creates a grid tile bar.
  ///
  /// Typically used to with [GridTile].
27 28 29 30 31 32 33 34
  GridTileBar({
    Key key,
    this.backgroundColor,
    this.leading,
    this.title,
    this.subtitle,
    this.trailing
  }) : super(key: key);
35

36 37 38
  /// The color to paint behind the child widgets.
  ///
  /// Defaults to transparent.
39
  final Color backgroundColor;
40 41 42 43

  /// A widget to display before the title.
  ///
  /// Typically an [Icon] or an [IconButton] widget.
44
  final Widget leading;
45 46 47 48

  /// The primary content of the list item.
  ///
  /// Typically a [Text] widget.
49
  final Widget title;
50 51 52 53

  /// Additional content displayed below the title.
  ///
  /// Typically a [Text] widget.
54
  final Widget subtitle;
55 56 57 58

  /// A widget to display after the title.
  ///
  /// Typically an [Icon] or an [IconButton] widget.
59
  final Widget trailing;
60

61
  @override
62 63 64 65 66
  Widget build(BuildContext context) {
    BoxDecoration decoration;
    if (backgroundColor != null)
      decoration = new BoxDecoration(backgroundColor: backgroundColor);

67
    EdgeInsets padding;
68
    if (leading != null && trailing != null)
69
      padding = const EdgeInsets.symmetric(vertical: 16.0, horizontal: 8.0);
70
    else if (leading != null)
71
      padding = const EdgeInsets.only(left: 8.0, right: 16.0, top: 16.0, bottom: 16.0);
72
    else // trailing != null || (leading == null && trailing == null)
73
      padding = const EdgeInsets.only(left: 16.0, right: 8.0, top: 16.0, bottom: 16.0);
74 75 76

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

77 78
    if (leading != null)
      children.add(new Padding(padding: const EdgeInsets.only(right: 8.0), child: leading));
79

80
    if (title != null && subtitle != null) {
81 82 83
      children.add(
        new Flexible(
          child: new Column(
84
            crossAxisAlignment: CrossAxisAlignment.start,
85
            children: <Widget>[
86
              new DefaultTextStyle(
87
                style: Typography.white.subhead,
88 89
                softWrap: false,
                overflow: TextOverflow.ellipsis,
90 91
                child: title
              ),
92
              new DefaultTextStyle(
93
                style: Typography.white.caption,
94 95
                softWrap: false,
                overflow: TextOverflow.ellipsis,
96
                child: subtitle
97 98 99 100 101
              )
            ]
          )
        )
      );
102
    } else if (title != null || subtitle != null) {
103 104
      children.add(
        new Flexible(
105
          child: new DefaultTextStyle(
106
            style: Typography.white.subhead,
107 108
            softWrap: false,
            overflow: TextOverflow.ellipsis,
109
            child: title ?? subtitle
110 111 112 113 114
          )
        )
      );
    }

115 116
    if (trailing != null)
      children.add(new Padding(padding: const EdgeInsets.only(left: 8.0), child: trailing));
117 118 119 120

    return new Container(
      padding: padding,
      decoration: decoration,
Ian Hickson's avatar
Ian Hickson committed
121 122
      child: new IconTheme.merge(
        context: context,
Adam Barth's avatar
Adam Barth committed
123
        data: new IconThemeData(color: Colors.white),
124
        child: new Row(
125
          crossAxisAlignment: CrossAxisAlignment.center,
126 127 128 129 130 131
          children: children
        )
      )
    );
  }
}