I want to hide the navigationIcon but when I pass null or Unit the icon space remains visible, as can be seen in the image below.

@Composable
fun DefaultScaffold(
    title: String? = null,
    icon: ImageVector? = null,
    content: @Composable() () -> Unit
) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    if (title != null) {
                        Text(text = "My Title")
                    }
                },
                navigationIcon = {
                    if (icon != null) {
                        IconButton(
                            onClick = {}
                        ) {
                            Icon(imageVector = Icons.Filled.Close, contentDescription = null)
                        }
                    } else Unit
                }
            )
        }
    ) {
        content()
    }
}
Remove the parentheses that surround your conditional statement, then return null instead of Unit when you don't want to show the icon space.
So, it will become
navigationIcon = if (icon != null) {
    {
        IconButton(
            onClick = {}
        ) {
            Icon(imageVector = Icons.Filled.Close, contentDescription = null)
        }
    }
} else null
You can use something like
navigationIcon = if (icon != null) {
    { /*... your code ..*/ }
} else null
but the constructor of TopAppBar with title and navigationIcon parameters preserves the slots for a title and the navigation icon.
If you check the source code you can find:
if (navigationIcon == null) {
            Spacer(TitleInsetWithoutIcon)
        } else {
You should use the constructor with the content parameter, that has no restriction on content.
Then you can use something like:
    TopAppBar(
        content = {
            Row(Modifier.fillMaxHeight(), verticalAlignment = Alignment.CenterVertically){
                if (icon != null) {
                    IconButton(
                        onClick = {},
                    ) {
                        Icon(imageVector = icon, contentDescription = null)
                    }
                }
                if (title != null) {
                    ProvideTextStyle(value = MaterialTheme.typography.h6) {
                        CompositionLocalProvider(
                            LocalContentAlpha provides ContentAlpha.high,
                        ){
                            Text(text = title)
                        }
                    }
                }
            }
        }
    )
 

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With