1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.material
18 
19 import androidx.compose.foundation.BorderStroke
20 import androidx.compose.foundation.interaction.Interaction
21 import androidx.compose.foundation.interaction.MutableInteractionSource
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.NonRestartableComposable
24 import androidx.compose.ui.Modifier
25 import androidx.compose.ui.graphics.Color
26 import androidx.compose.ui.graphics.Shape
27 import androidx.compose.ui.unit.Dp
28 import androidx.compose.ui.unit.dp
29 
30 /**
31  * [Material Design card](https://material.io/components/cards)
32  *
33  * Cards contain content and actions about a single subject.
34  *
35  * ![Cards
36  * image](https://developer.android.com/images/reference/androidx/compose/material/cards.png)
37  *
38  * This version of Card will block clicks behind it. For clickable card, please use another overload
39  * that accepts `onClick` as a parameter.
40  *
41  * @sample androidx.compose.material.samples.CardSample
42  * @param modifier Modifier to be applied to the layout of the card.
43  * @param shape Defines the card's shape as well its shadow. A shadow is only displayed if the
44  *   [elevation] is greater than zero.
45  * @param backgroundColor The background color.
46  * @param contentColor The preferred content color provided by this card to its children. Defaults
47  *   to either the matching content color for [backgroundColor], or if [backgroundColor] is not a
48  *   color from the theme, this will keep the same value set above this card.
49  * @param border Optional border to draw on top of the card
50  * @param elevation The z-coordinate at which to place this card. This controls the size of the
51  *   shadow below the card.
52  * @param content The content displayed on the card.
53  */
54 @Composable
55 @NonRestartableComposable
Cardnull56 fun Card(
57     modifier: Modifier = Modifier,
58     shape: Shape = MaterialTheme.shapes.medium,
59     backgroundColor: Color = MaterialTheme.colors.surface,
60     contentColor: Color = contentColorFor(backgroundColor),
61     border: BorderStroke? = null,
62     elevation: Dp = 1.dp,
63     content: @Composable () -> Unit
64 ) {
65     Surface(
66         modifier = modifier,
67         shape = shape,
68         color = backgroundColor,
69         contentColor = contentColor,
70         elevation = elevation,
71         border = border,
72         content = content
73     )
74 }
75 
76 /**
77  * Cards are [Surface]s that display content and actions on a single topic.
78  *
79  * This version of Card provides click handling as well. If you do not want Card to handle clicks,
80  * consider using another overload.
81  *
82  * @sample androidx.compose.material.samples.ClickableCardSample
83  * @param onClick callback to be called when the card is clicked
84  * @param modifier Modifier to be applied to the layout of the card.
85  * @param enabled Controls the enabled state of the card. When `false`, this card will not be
86  *   clickable
87  * @param shape Defines the card's shape as well its shadow. A shadow is only displayed if the
88  *   [elevation] is greater than zero.
89  * @param backgroundColor The background color.
90  * @param contentColor The preferred content color provided by this card to its children. Defaults
91  *   to either the matching content color for [backgroundColor], or if [backgroundColor] is not a
92  *   color from the theme, this will keep the same value set above this card.
93  * @param border Optional border to draw on top of the card
94  * @param elevation The z-coordinate at which to place this card. This controls the size of the
95  *   shadow below the card.
96  * @param interactionSource an optional hoisted [MutableInteractionSource] for observing and
97  *   emitting [Interaction]s for this card. You can use this to change the card's appearance or
98  *   preview the card in different states. Note that if `null` is provided, interactions will still
99  *   happen internally.
100  * @param content The content displayed on the card.
101  */
102 @ExperimentalMaterialApi
103 @Composable
104 @NonRestartableComposable
Cardnull105 fun Card(
106     onClick: () -> Unit,
107     modifier: Modifier = Modifier,
108     enabled: Boolean = true,
109     shape: Shape = MaterialTheme.shapes.medium,
110     backgroundColor: Color = MaterialTheme.colors.surface,
111     contentColor: Color = contentColorFor(backgroundColor),
112     border: BorderStroke? = null,
113     elevation: Dp = 1.dp,
114     interactionSource: MutableInteractionSource? = null,
115     content: @Composable () -> Unit
116 ) {
117     Surface(
118         onClick = onClick,
119         modifier = modifier,
120         enabled = enabled,
121         shape = shape,
122         color = backgroundColor,
123         contentColor = contentColor,
124         border = border,
125         elevation = elevation,
126         interactionSource = interactionSource,
127         content = content
128     )
129 }
130