Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose - Why does my Material3 TopAppBar have a huge padding at the top?

I'm having some trouble figuring out why this padding is being added at the top of my CenterAlignedTopAppBar, using Material2 works just fine and has a size of 56dp, material3 is doing this.

Here is some code showing what I'm doing, my composable is wrapped in a fragment. Currently I'm using a column, I tried with Scaffold as well with the same result.

enter image description here

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    return ComposeView(requireContext()).apply {
        setContent {

            val uiState = viewModel.uiState.collectAsState()
            viewModel.setIntent(MapIntent.GetOneOrAll(id))

            Column {
                if (uiState.value.hasToolbar) {
                    CenterAlignedTopAppBar(
                        modifier = Modifier.fillMaxWidth().wrapContentHeight(),
                        title = {
                            Text(
                                text = uiState.value.toolbarTitle ?: "", maxLines = 1, overflow = TextOverflow.Ellipsis,
                                color = colorResource(id = R.color.white)
                            )
                        },
                        navigationIcon = {
                            IconButton(onClick = { /* doSomething() */ }) {
                                Icon(
                                    imageVector = Icons.Filled.ArrowBack,
                                    contentDescription = null,
                                    tint = colorResource(id = R.color.white)
                                )
                            }
                        },
                        actions = {
                            IconButton(onClick = { /* doSomething() */ }) {
                                Icon(
                                    imageVector = Icons.Filled.Menu,
                                    contentDescription = null,
                                    tint = colorResource(id = R.color.white)
                                )
                            }
                        },
                        colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
                            containerColor = colorResource(id = R.color.colorPrimaryDark),
                        )
                    )
                }
                MapView(
                    modifier = Modifier.fillMaxSize(),
                    onMapClick = { viewModel.setIntent(MapIntent.OnClickMap) },
                    onMarkerClick = { viewModel.setIntent(MapIntent.OnClickMarker(it)) },
                    cameraPositionState = uiState.value.cameraPositionState,
                    markers = uiState.value.markers
                )
            }
        }
    }
}
like image 657
SomeKoder Avatar asked Dec 14 '25 04:12

SomeKoder


2 Answers

Not sure why I needed to specify this, but setting insets to zero fixed my problem.

windowInsets = WindowInsets(
    top = dimensionResource(id = R.dimen.size_0dp),
    bottom = dimensionResource(id = R.dimen.size_0dp)
),
like image 82
SomeKoder Avatar answered Dec 16 '25 19:12

SomeKoder


Seems like some parent composable has additional modifier like .statusBarsPadding() or .systemBarsPadding() or the .padding(it) from Scaffold composable

like image 21
Merkost Avatar answered Dec 16 '25 21:12

Merkost



Donate For Us

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