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
Column
modifier toweight
.From,
To
The
fillMaxWidth
modifier is used to make theColumn
as wide as possible. Whereas here the requirement is to fill as wide as possible leaving space for theSpacer
andSwitch
. That is achieved by using theweight
modifier.Weight
FillMaxWidth
Reference
weight
fillMaxWidth