1 /*
<lambda>null2 * 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.demos
18
19 import androidx.compose.animation.animateColorAsState
20 import androidx.compose.foundation.BorderStroke
21 import androidx.compose.foundation.layout.Arrangement
22 import androidx.compose.foundation.layout.Column
23 import androidx.compose.foundation.layout.PaddingValues
24 import androidx.compose.foundation.layout.Row
25 import androidx.compose.foundation.layout.Spacer
26 import androidx.compose.foundation.layout.fillMaxWidth
27 import androidx.compose.foundation.layout.height
28 import androidx.compose.foundation.layout.size
29 import androidx.compose.foundation.lazy.LazyColumn
30 import androidx.compose.foundation.shape.GenericShape
31 import androidx.compose.material.Button
32 import androidx.compose.material.ButtonDefaults
33 import androidx.compose.material.Icon
34 import androidx.compose.material.IconToggleButton
35 import androidx.compose.material.MaterialTheme
36 import androidx.compose.material.OutlinedButton
37 import androidx.compose.material.Text
38 import androidx.compose.material.TextButton
39 import androidx.compose.material.icons.Icons
40 import androidx.compose.material.icons.filled.Favorite
41 import androidx.compose.material.samples.ButtonSample
42 import androidx.compose.material.samples.ButtonWithIconSample
43 import androidx.compose.material.samples.FluidExtendedFab
44 import androidx.compose.material.samples.IconButtonSample
45 import androidx.compose.material.samples.IconToggleButtonSample
46 import androidx.compose.material.samples.OutlinedButtonSample
47 import androidx.compose.material.samples.SimpleExtendedFabNoIcon
48 import androidx.compose.material.samples.SimpleExtendedFabWithIcon
49 import androidx.compose.material.samples.SimpleFab
50 import androidx.compose.material.samples.TextButtonSample
51 import androidx.compose.runtime.Composable
52 import androidx.compose.runtime.getValue
53 import androidx.compose.runtime.mutableStateOf
54 import androidx.compose.runtime.remember
55 import androidx.compose.runtime.setValue
56 import androidx.compose.ui.Modifier
57 import androidx.compose.ui.graphics.Color
58 import androidx.compose.ui.unit.dp
59
60 private val DefaultSpace = 20.dp
61
62 @Composable
63 fun ButtonDemo() {
64 LazyColumn(
65 contentPadding = PaddingValues(10.dp),
66 verticalArrangement = Arrangement.spacedBy(DefaultSpace)
67 ) {
68 item { Buttons() }
69 item { Fabs() }
70 item { IconButtons() }
71 item { CustomShapeButton() }
72 }
73 }
74
75 @Composable
Buttonsnull76 private fun Buttons() {
77 Text("Buttons")
78 Spacer(Modifier.height(DefaultSpace))
79 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
80 ButtonSample()
81 OutlinedButtonSample()
82 TextButtonSample()
83 }
84
85 Spacer(Modifier.height(DefaultSpace))
86
87 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
88 Button(
89 onClick = {},
90 colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.secondary)
91 ) {
92 Text("Secondary Color")
93 }
94 ButtonWithIconSample()
95 }
96
97 Spacer(Modifier.height(DefaultSpace))
98
99 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
100 Button(onClick = {}, enabled = false) { Text("Disabled") }
101 OutlinedButton(onClick = {}, enabled = false) { Text("Disabled") }
102 TextButton(onClick = {}, enabled = false) { Text("Disabled") }
103 }
104 }
105
106 @Composable
Fabsnull107 private fun Fabs() {
108 Text("Floating action buttons")
109 Spacer(Modifier.height(DefaultSpace))
110
111 Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
112 SimpleFab()
113 SimpleExtendedFabNoIcon()
114 SimpleExtendedFabWithIcon()
115 }
116 Spacer(Modifier.height(DefaultSpace))
117 FluidExtendedFab()
118 }
119
120 @Composable
IconButtonsnull121 private fun IconButtons() {
122 Text("Icon buttons")
123 Spacer(Modifier.height(DefaultSpace))
124
125 Row {
126 IconButtonSample()
127 IconToggleButtonSample()
128 IconToggleButtonDisabled()
129 }
130 }
131
132 @Composable
CustomShapeButtonnull133 private fun CustomShapeButton() {
134 Text("Custom shape button")
135 Spacer(Modifier.height(DefaultSpace))
136 OutlinedButton(
137 onClick = {},
138 modifier = Modifier.size(110.dp),
139 shape = TriangleShape,
140 colors = ButtonDefaults.outlinedButtonColors(backgroundColor = Color.Yellow),
141 border = BorderStroke(width = 2.dp, color = Color.Black)
142 ) {
143 Column {
144 Text("Click")
145 Text("here")
146 }
147 }
148 }
149
150 @Composable
IconToggleButtonDisablednull151 private fun IconToggleButtonDisabled() {
152 var checked by remember { mutableStateOf(false) }
153
154 IconToggleButton(checked = checked, enabled = false, onCheckedChange = { checked = it }) {
155 val tint by animateColorAsState(if (checked) Color(0xFFEC407A) else Color(0xFFB0BEC5))
156 Icon(Icons.Filled.Favorite, contentDescription = "Favorite", tint = tint)
157 }
158 }
159
sizenull160 private val TriangleShape = GenericShape { size, _ ->
161 moveTo(size.width / 2f, 0f)
162 lineTo(size.width, size.height)
163 lineTo(0f, size.height)
164 }
165