Christoph
11/18/2020, 2:26 PMTopAppBar
) conditionally.
I want to set a navigationIcon
depending on a condition. but doing
navigationIcon = {
if (showBackArrow) {
IconButton(onClick = onBackClick) {
Icon(Icons.Default.ArrowBack)
}
}
}
this results in a "empty" composable set to the app bar and therefore the title is inset.
what's the best approach here?Zach Klippenstein (he/him) [MOD]
11/18/2020, 2:34 PMnavigationIcon = if (showBackArrow) {
@Composable fun() {
IconButton(onClick = onBackClick) {
Icon(Icons.Default.ArrowBack)
}
}
} else null
Zach Klippenstein (he/him) [MOD]
11/18/2020, 2:34 PMTopAppBar
has some conditional logic around whether the navigationIcon
function is null or notChristoph
11/18/2020, 2:36 PMLouis Pullen-Freilich [G]
11/18/2020, 2:37 PMnull
disables the inset.
navigationIcon = if (showBackArrow) {
@Composable {
IconButton(onClick = onBackClick) {
Icon(Icons.Default.ArrowBack)
}
}
} else null
Should work? Maybe the fun()
is tripping up the compilerLouis Pullen-Freilich [G]
11/18/2020, 2:38 PMnavigationIcon = (@Composable {
IconButton(onClick = onBackClick) {
Icon(Icons.Default.ArrowBack)
}
}).takeIf { showBackArrow }
should workChristoph
11/18/2020, 2:40 PMZach Klippenstein (he/him) [MOD]
11/18/2020, 2:40 PMTopAppBar
would handle an empty composable automatically. I think it would need to be refactored to use subcomposition to do that though, probably not worth the effort.Christoph
11/18/2020, 2:41 PMLouis Pullen-Freilich [G]
11/18/2020, 2:57 PMIt would be cool ifÂWe could currently do this but we don't want to introduce a kind of 'type system' into would handle an empty composable automatically.TopAppBar
@Composable {}
- components shouldn't care whether there are no layouts, one layout, or multiple layouts inside for behavior IMO. null
does make this a bit less friendly to work with, but it's semantically meaningful and there's a well understand difference between a type and the nullable version of that type, along with understood behavior changes.
Maybe an explicit boolean parameter here might be better though, not sure.Christoph
11/18/2020, 3:01 PMZach Klippenstein (he/him) [MOD]
11/18/2020, 3:03 PMLouis Pullen-Freilich [G]
11/18/2020, 3:05 PMMy thinking is that if a composable function doesn’t have any contentThe problem is that this is very hard to know when you are accepting a lambda that comes from another function that accepts a lambda, etc - which makes it quite hard to look at the code and understand the behavior. And as soon as you add some modifiers (maybe just some padding), this will appear to not be empty, so even if the 'content' is just an empty box with some padding suddenly the title will be inset - this is the sort of behavior I'd like to avoid