The Code A is from the main branch of the official sample project.
There are three subclass Overview, Accounts and Bills of the enum class RallyScreen in the project.
There is a function fun content(onScreenChange: (String) -> Unit) { body(onScreenChange) } which accept the paramater onScreenChange : (String) -> Unit in the class RallyScreen.
Although the statement is enum class RallyScreen(val body: @Composable ((String) -> Unit) -> Unit) {..}, the body = { OverviewBody() } in the class Overview, the body = { AccountsBody(UserData.accounts) } in the class Accounts and the body = { BillsBody(UserData.bills) } in the class Bills don't require to pass the paramter (String) -> Unit), why can Kotlin run well and navigate by Tab well when the App launch fun content(onScreenChange: (String) -> Unit) { body(onScreenChange)} ?
Code A
@Composable
fun RallyApp() {
RallyTheme {
val allScreens = RallyScreen.values().toList()
var currentScreen by rememberSaveable { mutableStateOf(RallyScreen.Overview) }
Scaffold(
topBar = {
RallyTabRow(
allScreens = allScreens,
onTabSelected = { screen -> currentScreen = screen },
currentScreen = currentScreen
)
}
) { innerPadding ->
Box(Modifier.padding(innerPadding)) {
currentScreen.content(
onScreenChange = { screen ->
currentScreen = RallyScreen.valueOf(screen)
}
)
}
}
}
}
enum class RallyScreen(
val icon: ImageVector,
val body: @Composable ((String) -> Unit) -> Unit
) {
Overview(
icon = Icons.Filled.PieChart,
body = { OverviewBody() }
),
Accounts(
icon = Icons.Filled.AttachMoney,
body = { AccountsBody(UserData.accounts) }
),
Bills(
icon = Icons.Filled.MoneyOff,
body = { BillsBody(UserData.bills) }
);
@Composable
fun content(onScreenChange: (String) -> Unit) {
body(onScreenChange)
}
}
@Composable
fun OverviewBody(
onClickSeeAllAccounts: () -> Unit = {},
onClickSeeAllBills: () -> Unit = {},
onAccountClick: (String) -> Unit = {},
) {
...
}
@Composable
fun AccountsBody(
accounts: List<Account>,
onAccountClick: (String) -> Unit = {},
) {
...
}
@Composable
fun BillsBody(bills: List<Bill>) {
...
}
Added Content:
To Gabriel Pizarro: Thanks!
By the statement of clas Overview, the body needn't any parameter, it use default values, how is the onScreenChange parameter passed ?
Overview(
icon = Icons.Filled.PieChart,
body = { OverviewBody() } //It needn't any parameter
)
@Composable
fun content(onScreenChange: (String) -> Unit) {
body(onScreenChange)
}
And more, the app can work well when I add two default parameters for fun OverviewBody(...) .
@Composable
fun OverviewBody(
onClickSeeAllAccounts: () -> Unit = {},
onClickSeeAllBills: () -> Unit = {},
a1: (String) -> Unit = {}, // I add
onAccountClick: (String) -> Unit = {},
a2: (String) -> Unit = {}, //I add
) {
...
}