grid_tile_bar.dart 2.91 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 12 13 14 15
import 'icon_theme.dart';
import 'icon_theme_data.dart';
import 'typography.dart';


/// Typically used to stack a one or two line header or footer on a Grid tile.
/// The layout is based on the "Grid Lists" section of the Material Design spec:
/// https://www.google.com/design/spec/components/grid-lists.html#grid-lists-specs
16 17
/// For a one-line header specify [title] and to add a second line specify [subtitle].
/// Use [leading] or [trailing] to add an icon.
18
class GridTileBar extends StatelessWidget {
19 20 21 22 23 24 25 26
  GridTileBar({
    Key key,
    this.backgroundColor,
    this.leading,
    this.title,
    this.subtitle,
    this.trailing
  }) : super(key: key);
27 28

  final Color backgroundColor;
29
  final Widget leading;
30
  final Widget title;
31 32
  final Widget subtitle;
  final Widget trailing;
33

34
  @override
35 36 37 38 39
  Widget build(BuildContext context) {
    BoxDecoration decoration;
    if (backgroundColor != null)
      decoration = new BoxDecoration(backgroundColor: backgroundColor);

40
    EdgeInsets padding;
41
    if (leading != null && trailing != null)
42
      padding = const EdgeInsets.symmetric(vertical: 16.0, horizontal: 8.0);
43
    else if (leading != null)
44
      padding = const EdgeInsets.only(left: 8.0, right: 16.0, top: 16.0, bottom: 16.0);
45
    else // trailing != null || (leading == null && trailing == null)
46
      padding = const EdgeInsets.only(left: 16.0, right: 8.0, top: 16.0, bottom: 16.0);
47 48 49

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

50 51
    if (leading != null)
      children.add(new Padding(padding: const EdgeInsets.only(right: 8.0), child: leading));
52

53
    if (title != null && subtitle != null) {
54 55 56
      children.add(
        new Flexible(
          child: new Column(
57
            crossAxisAlignment: CrossAxisAlignment.start,
58 59 60 61 62 63 64
            children: <Widget>[
              new DefaultTextStyle(
                style: Typography.white.subhead,
                child: title
              ),
              new DefaultTextStyle(
                style: Typography.white.caption,
65
                child: subtitle
66 67 68 69 70
              )
            ]
          )
        )
      );
71
    } else if (title != null || subtitle != null) {
72 73 74 75
      children.add(
        new Flexible(
          child: new DefaultTextStyle(
            style: Typography.white.subhead,
76
            child: title ?? subtitle
77 78 79 80 81
          )
        )
      );
    }

82 83
    if (trailing != null)
      children.add(new Padding(padding: const EdgeInsets.only(left: 8.0), child: trailing));
84 85 86 87 88

    return new Container(
      padding: padding,
      decoration: decoration,
      child: new IconTheme(
Adam Barth's avatar
Adam Barth committed
89
        data: new IconThemeData(color: Colors.white),
90
        child: new Row(
91
          crossAxisAlignment: CrossAxisAlignment.center,
92 93 94 95 96 97
          children: children
        )
      )
    );
  }
}