My code:
@ExperimentalMaterial3Api
@Composable
fun Settings() {
Card(modifier = Modifier
.fillMaxWidth()
) {
SettingItem(itemTitle = "Test Title", itemDescription = "Description")
}
}
@ExperimentalMaterial3Api
@Composable
fun SettingItem(itemTitle: String, itemDescription: String) {
Row(modifier = Modifier.padding(12.dp).fillMaxWidth()) {
Column(modifier = Modifier.fillMaxWidth()) {
Text(text = itemTitle, color = MaterialTheme.colorScheme.primary)
Text(text = itemDescription, color = MaterialTheme.colorScheme.secondary)
}
Spacer(modifier = Modifier.size(width = 12.dp, height = 4.dp))
Switch(checked = false, onCheckedChange = {})
}
}
I want to put a Switch at the end of a SettingItem, so I set fillMaxWidth(). But the Switch shows out of the layout of Card.
Change the
Columnmodifier toweight.From,
To
The
fillMaxWidthmodifier is used to make theColumnas wide as possible. Whereas here the requirement is to fill as wide as possible leaving space for theSpacerandSwitch. That is achieved by using theweightmodifier.Weight
FillMaxWidth
Reference
weight
fillMaxWidth