Skip to content

Commit 7dca9cf

Browse files
authored
Additonal Compose components (#269)
* first commit - checkbox * Apply Spotless * Fixing region tags * updating parent example * Apply Spotless --------- Co-authored-by: jakeroseman <[email protected]>
1 parent bedb85e commit 7dca9cf

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.navigation.compose.rememberNavController
3030
import com.example.compose.snippets.animations.AnimationExamplesScreen
3131
import com.example.compose.snippets.components.AppBarExamples
3232
import com.example.compose.snippets.components.ButtonExamples
33+
import com.example.compose.snippets.components.CheckboxExamples
3334
import com.example.compose.snippets.components.ChipExamples
3435
import com.example.compose.snippets.components.ComponentsScreen
3536
import com.example.compose.snippets.components.DialogExamples
@@ -93,6 +94,7 @@ class SnippetsActivity : ComponentActivity() {
9394
TopComponentsDestination.ProgressIndicatorExamples -> ProgressIndicatorExamples()
9495
TopComponentsDestination.ScaffoldExample -> ScaffoldExample()
9596
TopComponentsDestination.AppBarExamples -> AppBarExamples()
97+
TopComponentsDestination.CheckboxExamples -> CheckboxExamples()
9698
}
9799
}
98100
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2023 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+
* https://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 com.example.compose.snippets.components
18+
19+
import androidx.compose.foundation.layout.Arrangement
20+
import androidx.compose.foundation.layout.Column
21+
import androidx.compose.foundation.layout.Row
22+
import androidx.compose.foundation.layout.fillMaxSize
23+
import androidx.compose.foundation.layout.padding
24+
import androidx.compose.material3.Checkbox
25+
import androidx.compose.material3.Text
26+
import androidx.compose.material3.TriStateCheckbox
27+
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.getValue
29+
import androidx.compose.runtime.mutableStateListOf
30+
import androidx.compose.runtime.mutableStateOf
31+
import androidx.compose.runtime.remember
32+
import androidx.compose.runtime.setValue
33+
import androidx.compose.ui.Alignment
34+
import androidx.compose.ui.Modifier
35+
import androidx.compose.ui.state.ToggleableState
36+
import androidx.compose.ui.tooling.preview.Preview
37+
import androidx.compose.ui.unit.dp
38+
39+
@Preview
40+
@Composable
41+
fun CheckboxExamples() {
42+
Column(
43+
modifier = Modifier
44+
.padding(16.dp)
45+
.fillMaxSize(),
46+
verticalArrangement = Arrangement.spacedBy(32.dp),
47+
horizontalAlignment = Alignment.CenterHorizontally,
48+
) {
49+
Column {
50+
Text("Minimal checkbox example")
51+
CheckboxMinimalExample()
52+
}
53+
Column {
54+
Text("Parent checkbox example")
55+
CheckboxParentExample()
56+
}
57+
}
58+
}
59+
60+
@Preview
61+
// [START android_compose_components_checkbox_minimal]
62+
@Composable
63+
fun CheckboxMinimalExample() {
64+
var checked by remember { mutableStateOf(true) }
65+
66+
Row(
67+
verticalAlignment = Alignment.CenterVertically,
68+
) {
69+
Text(
70+
"Minimal checkbox"
71+
)
72+
Checkbox(
73+
checked = checked,
74+
onCheckedChange = { checked = it }
75+
)
76+
}
77+
78+
Text(
79+
if (checked) "Checkbox is checked" else "Checkbox is unchecked"
80+
)
81+
}
82+
// [END android_compose_components_checkbox_minimal]
83+
84+
@Preview
85+
// [START android_compose_components_checkbox_parent]
86+
@Composable
87+
fun CheckboxParentExample() {
88+
// Initialize states for the child checkboxes
89+
val childCheckedStates = remember { mutableStateListOf(false, false, false) }
90+
91+
// Compute the parent state based on children's states
92+
val parentState = when {
93+
childCheckedStates.all { it } -> ToggleableState.On
94+
childCheckedStates.none { it } -> ToggleableState.Off
95+
else -> ToggleableState.Indeterminate
96+
}
97+
98+
Column {
99+
// Parent TriStateCheckbox
100+
Row(
101+
verticalAlignment = Alignment.CenterVertically,
102+
) {
103+
Text("Select all")
104+
TriStateCheckbox(
105+
state = parentState,
106+
onClick = {
107+
// Determine new state based on current state
108+
val newState = parentState != ToggleableState.On
109+
childCheckedStates.forEachIndexed { index, _ ->
110+
childCheckedStates[index] = newState
111+
}
112+
}
113+
)
114+
}
115+
116+
// Child Checkboxes
117+
childCheckedStates.forEachIndexed { index, checked ->
118+
Row(
119+
verticalAlignment = Alignment.CenterVertically,
120+
) {
121+
Text("Option ${index + 1}")
122+
Checkbox(
123+
checked = checked,
124+
onCheckedChange = { isChecked ->
125+
// Update the individual child state
126+
childCheckedStates[index] = isChecked
127+
}
128+
)
129+
}
130+
}
131+
}
132+
133+
if (childCheckedStates.all { it }) {
134+
Text("All options selected")
135+
}
136+
}
137+
// [END android_compose_components_checkbox_parent]

compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ enum class TopComponentsDestination(val route: String, val title: String) {
3737
ProgressIndicatorExamples("progressIndicatorExamples", "Progress Indicators"),
3838
ScaffoldExample("scaffoldExample", "Scaffold"),
3939
AppBarExamples("appBarExamples", "App bars"),
40+
CheckboxExamples("checkboxExamples", "Checkbox"),
4041
}

0 commit comments

Comments
 (0)