card.dart 4.48 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/widgets.dart';
6 7

import 'material.dart';
Hans Muller's avatar
Hans Muller committed
8
import 'theme.dart';
9

10
/// A material design card. A card has slightly rounded corners and a shadow.
11 12 13
///
/// A card is a sheet of [Material] used to represent some related information,
/// for example an album, a geographical location, a meal, contact details, etc.
14
///
15 16 17 18 19
/// ## Sample code
///
/// Here is an example of using a [Card] widget.
///
/// ```dart
20 21
/// Card(
///   child: Column(
22 23 24
///     mainAxisSize: MainAxisSize.min,
///     children: <Widget>[
///       const ListTile(
25 26 27
///         leading: Icon(Icons.album),
///         title: Text('The Enchanted Nightingale'),
///         subtitle: Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
28
///       ),
29 30
///       ButtonTheme.bar( // make buttons use the appropriate styles for cards
///         child: ButtonBar(
31
///           children: <Widget>[
32
///             FlatButton(
33 34 35
///               child: const Text('BUY TICKETS'),
///               onPressed: () { /* ... */ },
///             ),
36
///             FlatButton(
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
///               child: const Text('LISTEN'),
///               onPressed: () { /* ... */ },
///             ),
///           ],
///         ),
///       ),
///     ],
///   ),
/// )
/// ```
///
/// This is what it would look like:
///
/// ![A card with a slight shadow, consisting of two rows, one with an icon and
/// some text describing a musical, and the other with buttons for buying
52
/// tickets or listening to the show.](https://flutter.github.io/assets-for-api-docs/assets/material/card.png)
53
///
54 55
/// See also:
///
56
///  * [ListTile], to display icons and text in a card.
57
///  * [ButtonBar], to display buttons at the bottom of a card. Typically these
58
///    would be styled using a [ButtonTheme] created with [new ButtonTheme.bar].
59
///  * [showDialog], to display a modal card.
60
///  * <https://material.google.com/components/cards.html>
61
class Card extends StatelessWidget {
62
  /// Creates a material design card.
63 64
  ///
  /// The [clipBehavior] argument must not be null.
65 66
  const Card({
    Key key,
67
    this.color,
Hans Muller's avatar
Hans Muller committed
68 69
    this.elevation,
    this.shape,
70
    this.margin = const EdgeInsets.all(4.0),
71
    this.clipBehavior = Clip.none,
72
    this.child,
73
    this.semanticContainer = true,
74
  }) : super(key: key);
75

Hans Muller's avatar
Hans Muller committed
76
  /// The card's background color.
77
  ///
Hans Muller's avatar
Hans Muller committed
78 79 80
  /// Defines the card's [Material.color].
  ///
  /// The default color is defined by the ambient [Theme]: [ThemeData.cardColor].
81 82
  final Color color;

83 84
  /// The z-coordinate at which to place this card. This controls the size of
  /// the shadow below the card.
85
  ///
Hans Muller's avatar
Hans Muller committed
86 87 88
  /// Defines the card's [Material.elevation].
  ///
  /// The default elevation is 1.0.
89
  final double elevation;
90

Hans Muller's avatar
Hans Muller committed
91 92 93 94 95 96 97 98
  /// The shape of the card's [Material].
  ///
  /// Defines the card's [Material.shape].
  ///
  /// The default shape is a [RoundedRectangleBorder] with a circular corner
  /// radius of 4.0.
  final ShapeBorder shape;

99 100 101
  /// {@macro flutter.widgets.Clip}
  final Clip clipBehavior;

102 103 104 105 106 107 108 109
  /// The empty space that surrounds the card.
  ///
  /// Defines the card's outer [Container.margin].
  ///
  /// The default margin is 4.0 logical pixels on all sides:
  /// `EdgeInsets.all(4.0)`.
  final EdgeInsetsGeometry margin;

110 111 112
  /// Whether this widget represents a single semantic container, or if false
  /// a collection of individual semantic nodes.
  ///
113
  /// Defaults to true.
114 115 116 117 118 119 120 121 122
  ///
  /// Setting this flag to true will attempt to merge all child semantics into
  /// this node. Setting this flag to false will force all child semantic nodes
  /// to be explicit.
  ///
  /// This flag should be false if the card contains multiple different types
  /// of content.
  final bool semanticContainer;

Hans Muller's avatar
Hans Muller committed
123 124 125 126 127
  /// The widget below this widget in the tree.
  ///
  /// {@macro flutter.widgets.child}
  final Widget child;

128
  @override
129
  Widget build(BuildContext context) {
130
    return Semantics(
131 132
      container: semanticContainer,
      explicitChildNodes: !semanticContainer,
133
      child: Container(
134
        margin: margin ?? const EdgeInsets.all(4.0),
135
        child: Material(
136
          type: MaterialType.card,
Hans Muller's avatar
Hans Muller committed
137 138 139
          color: color ?? Theme.of(context).cardColor,
          elevation: elevation ?? 1.0,
          shape: shape ?? const RoundedRectangleBorder(
140
            borderRadius: BorderRadius.all(Radius.circular(4.0)),
Hans Muller's avatar
Hans Muller committed
141
          ),
142
          clipBehavior: clipBehavior,
Hans Muller's avatar
Hans Muller committed
143 144
          child: child,
        ),
145
      ),
146 147 148
    );
  }
}