Giang
12/22/2022, 12:42 PMElement functions MUST accept a parameter of typeand. This parameter MUST be named “`modifier`” and MUST appear as the first optional parameter in the element function's parameter listModifier
Layout functions SHOULD use the name “`content`” for afunction parameter if they accept only one@Composable
function parameter.@Composable
Layout functions SHOULD use the name “`content`” for their primary or most commonfunction parameter if they accept more than one@Composable
function parameter.@Composable
Layout functions SHOULD place their primary or most commonSo an example of the combination would befunction parameter in the last parameter position to permit the use of Kotlin's trailing lambda syntax for that parameter.@Composable
@Composable
fun FancyButton(
text: String,
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
What if my Composable accepts a modifier and a lambda? Should the lambda appears after or before modifier (given that it's not optional parameter)?
My thinking is that I should move the lambda to the bottom of parameter list to leverage trailing lambda like:
@Composable
fun FancyButton(
text: String,
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
But in the guideline page, I often see
@Composable
fun FancyButton(
text: String,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
Can anyone explain the reason of having the lambda before the modifier?Filip Wiesner
12/22/2022, 12:49 PMFancyButton("hello", { print("world") })
and you will have to do
FancyButton("hello", onClick = { print("world") })
Giang
12/22/2022, 12:50 PMFancyButton("hello") { print("world") }
Filip Wiesner
12/22/2022, 12:51 PMprint
is content of FancyButton
Giang
12/22/2022, 12:58 PMZach Klippenstein (he/him) [MOD]
12/22/2022, 6:56 PM