| /* |
| * Copyright 2019 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package androidx.compose.material.samples |
| |
| import androidx.annotation.Sampled |
| import androidx.compose.foundation.layout.Column |
| import androidx.compose.foundation.layout.Row |
| import androidx.compose.foundation.layout.fillMaxWidth |
| import androidx.compose.foundation.layout.padding |
| import androidx.compose.foundation.layout.preferredHeight |
| import androidx.compose.foundation.selection.selectable |
| import androidx.compose.material.Checkbox |
| import androidx.compose.material.CheckboxDefaults |
| import androidx.compose.material.MaterialTheme |
| import androidx.compose.material.RadioButton |
| import androidx.compose.material.Switch |
| import androidx.compose.material.Text |
| import androidx.compose.material.TriStateCheckbox |
| import androidx.compose.runtime.Composable |
| import androidx.compose.runtime.getValue |
| import androidx.compose.runtime.mutableStateOf |
| import androidx.compose.runtime.remember |
| import androidx.compose.runtime.setValue |
| import androidx.compose.ui.Alignment |
| import androidx.compose.ui.Modifier |
| import androidx.compose.ui.semantics.clearAndSetSemantics |
| import androidx.compose.ui.state.ToggleableState |
| import androidx.compose.ui.unit.dp |
| |
| @Sampled |
| @Composable |
| fun TriStateCheckboxSample() { |
| Column { |
| // define dependent checkboxes states |
| val (state, onStateChange) = remember { mutableStateOf(true) } |
| val (state2, onStateChange2) = remember { mutableStateOf(true) } |
| |
| // TriStateCheckbox state reflects state of dependent checkboxes |
| val parentState = remember(state, state2) { |
| if (state && state2) ToggleableState.On |
| else if (!state && !state2) ToggleableState.Off |
| else ToggleableState.Indeterminate |
| } |
| // click on TriStateCheckbox can set state for dependent checkboxes |
| val onParentClick = { |
| val s = parentState != ToggleableState.On |
| onStateChange(s) |
| onStateChange2(s) |
| } |
| |
| TriStateCheckbox( |
| state = parentState, |
| onClick = onParentClick, |
| colors = CheckboxDefaults.colors( |
| checkedColor = MaterialTheme.colors.primary |
| ) |
| ) |
| Column(Modifier.padding(10.dp, 0.dp, 0.dp, 0.dp)) { |
| Checkbox(state, onStateChange) |
| Checkbox(state2, onStateChange2) |
| } |
| } |
| } |
| |
| @Sampled |
| @Composable |
| fun CheckboxSample() { |
| val checkedState = remember { mutableStateOf(true) } |
| Checkbox( |
| checked = checkedState.value, |
| onCheckedChange = { checkedState.value = it } |
| ) |
| } |
| |
| @Sampled |
| @Composable |
| fun SwitchSample() { |
| val checkedState = remember { mutableStateOf(true) } |
| Switch( |
| checked = checkedState.value, |
| onCheckedChange = { checkedState.value = it } |
| ) |
| } |
| |
| @Sampled |
| @Composable |
| fun RadioButtonSample() { |
| // We have two radio buttons and only one can be selected |
| var state by remember { mutableStateOf(true) } |
| Row { |
| RadioButton( |
| selected = state, |
| onClick = { state = true } |
| ) |
| RadioButton( |
| selected = !state, |
| onClick = { state = false } |
| ) |
| } |
| } |
| |
| @Sampled |
| @Composable |
| fun RadioGroupSample() { |
| val radioOptions = listOf("Calls", "Missed", "Friends") |
| val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) } |
| Column { |
| radioOptions.forEach { text -> |
| Row( |
| Modifier |
| .fillMaxWidth() |
| .preferredHeight(56.dp) |
| .selectable( |
| selected = (text == selectedOption), |
| onClick = { onOptionSelected(text) } |
| ) |
| .padding(horizontal = 16.dp), |
| verticalAlignment = Alignment.CenterVertically |
| ) { |
| // The [clearAndSetSemantics] causes the button's redundant |
| // selectable semantics to be cleared in favor of the [Row] |
| // selectable's, to improve usability with screen-readers. |
| RadioButton( |
| modifier = Modifier.clearAndSetSemantics {}, |
| selected = (text == selectedOption), |
| onClick = { onOptionSelected(text) } |
| ) |
| Text( |
| text = text, |
| style = MaterialTheme.typography.body1.merge(), |
| modifier = Modifier.padding(start = 16.dp) |
| ) |
| } |
| } |
| } |
| } |