grid_tile.dart 1.89 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4
// 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.

5
import 'package:flutter/foundation.dart';
Hans Muller's avatar
Hans Muller committed
6 7
import 'package:flutter/widgets.dart';

8 9
/// A tile in a material design grid list.
///
10
/// A grid list is a [GridView] of tiles in a vertical and horizontal
11 12 13 14 15
/// array. Each tile typically contains some visually rich content (e.g., an
/// image) together with a [GridTileBar] in either a [header] or a [footer].
///
/// See also:
///
16
///  * [GridView], which is a scrollable grid of tiles.
17 18
///  * [GridTileBar], which is typically used in either the [header] or
///    [footer].
19
///  * <https://material.google.com/components/grid-lists.html>
20
class GridTile extends StatelessWidget {
21 22 23
  /// Creates a grid tile.
  ///
  /// Must have a child. Does not typically have both a header and a footer.
24
  const GridTile({
25 26 27 28
    Key key,
    this.header,
    this.footer,
    @required this.child,
29 30
  }) : assert(child != null),
       super(key: key);
Hans Muller's avatar
Hans Muller committed
31

32
  /// The widget to show over the top of this grid tile.
33 34
  ///
  /// Typically a [GridTileBar].
Hans Muller's avatar
Hans Muller committed
35
  final Widget header;
36 37

  /// The widget to show over the bottom of this grid tile.
38 39
  ///
  /// Typically a [GridTileBar].
Hans Muller's avatar
Hans Muller committed
40
  final Widget footer;
41 42

  /// The widget that fills the tile.
Hans Muller's avatar
Hans Muller committed
43 44
  final Widget child;

45
  @override
Hans Muller's avatar
Hans Muller committed
46 47 48 49 50
  Widget build(BuildContext context) {
    if (header == null && footer == null)
      return child;

    final List<Widget> children = <Widget>[
51
      new Positioned.fill(
52 53
        child: child,
      ),
Hans Muller's avatar
Hans Muller committed
54 55 56 57 58 59
    ];
    if (header != null) {
      children.add(new Positioned(
        top: 0.0,
        left: 0.0,
        right: 0.0,
60
        child: header,
Hans Muller's avatar
Hans Muller committed
61 62 63 64 65 66 67
      ));
    }
    if (footer != null) {
      children.add(new Positioned(
        left: 0.0,
        bottom: 0.0,
        right: 0.0,
68
        child: footer,
Hans Muller's avatar
Hans Muller committed
69 70 71 72 73
      ));
    }
    return new Stack(children: children);
  }
}