grid_tile.dart 1.88 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
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';

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

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

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

  /// The widget that fills the tile.
42 43
  ///
  /// {@macro flutter.widgets.child}
Hans Muller's avatar
Hans Muller committed
44 45
  final Widget child;

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

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