87

I recently started working with Jetpack Compose. I've got the following composable:

@Composable
fun SearchScreen(navController: NavHostController) {
    Scaffold(
        topBar = { SearchBar() },
        content = {
            Column(modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())) {
                Text(stringResource(id = R.string.genreFilter))
                Row(
                    modifier = Modifier
                        .horizontalScroll(rememberScrollState()),
                    horizontalArrangement = Arrangement.spacedBy(4.dp)
                ) {
                    // some nested Composables
                }

            }},
    )
}

But with this code as-is, the whole code within content = {...} is being underlined in red saying Jetpack Compose: Content padding parameter it is not used. I already read in this Stackoverflow Post that actually, PaddingValues only are provided in a Scaffold if the bottomBar is set, which obviously is not the case here. So I do not understand why I am getting this error.

Note: The app actually does use a BottomNavigation, but not within the Composable that I showed above. Can it be that this is still somehow propagated here?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
BenjyTec
  • 1,719
  • 2
  • 12
  • 22
  • 2
    I copy-pasted your composable and I can't reproduce the underlined error. I have also put the `content` argument as a trailing lambda and the code works fine. – Tonnie May 02 '22 at 09:57
  • Interesting. Could it be related to my compose version? I am using `compose_version = '1.2.0-alpha08`, as I need the FilterChip Composable. Which version are you using? – BenjyTec May 02 '22 at 10:01
  • 1
    Yeah, I am on `composeVersion = "1.1.1"` and I have seen `@Pylyp Dukhov` answer below which clarifies the differences. – Tonnie May 02 '22 at 10:20

6 Answers6

196

It's required to use padding parameter, passed into Scaffold content composable. You should apply it to the topmost container/view in content:

content = { padding ->
    Column(
        modifier = Modifier
            .padding(padding)
    // ...

This is done to prevent layout problems, for example, when the scaffold has a bottom bar, without the use of this padding part of your view will be under the bar.

You can always suppress it with @SuppressLint("UnusedMaterialScaffoldPaddingParameter"), but I would recommend doing this only when you know exactly what you are doing.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • 1
    Thanks, this explains it! One follow up question though, if I have a `NavHost` set as a child of a `Scaffold`, would I just set the padding directly to the `NavHost`, or do I need to somehow pass this padding to the seperate composables that are going to be displayed within the `NavHost`? – BenjyTec May 02 '22 at 11:32
  • 2
    @BenjyTec you should apply it to the topmost container, in this case `NavHost` – Phil Dukhov May 02 '22 at 11:41
  • I've applied this to my top most container and still get the error, supressing it seems to be the only way of getting rid of it – alfietap May 06 '22 at 05:42
  • @alfietap show the code how you did it, e.g. using [gist](https://gist.github.com). it should work for sure, if your code is correct and it doesn't work - it should be reported as IDE bug – Phil Dukhov May 06 '22 at 05:46
  • https://gist.github.com/alfietapping/f064e20e82b4d84aaf2c304be165cbdf - AppNavigation contains the NavHost – alfietap May 06 '22 at 05:48
  • 2
    @alfietap it doesn't show an error to me. Make sure you're using latest IDE and android gradle plugin. Try creating a new project with the same code to see if you can reproduce it. If yes, [report](https://issuetracker.google.com/issues/new?component=612128) including this sample project, your IDE version and sample project. – Phil Dukhov May 06 '22 at 06:03
  • Yeesh. I was like WTF; I copied a Composable from another project I'm working on where it was fine, and didn't see why it had a problem in my new project. My older project must have a bit older version of Compose I guess. Jeez thanks. – clamum Oct 13 '22 at 07:09
9

I seen a bug in version alpha and I was using SmallTopAppBar() Scaffold didn't provide paddings to two composables was on top of each other. After updating it, just realized the bug fixed and Scaffold has itself a padding so composables is not on top of rach other anymore.

Usage:

  Scaffold(topBar = { AppBar() }) { paddingValues ->
  AnyComposable(modifier = Modifier.padding(paddingValues)){

  }
7

Add the it keyword:

Scaffold(
        topBar = { SearchBar() },
        content = { it
            Column(modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState()))

This should work.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Daley Nyae
  • 79
  • 1
  • 3
3

you can use this annotation :

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
Sina Weisy
  • 31
  • 1
0

I fixed this issue by passing the content's padding to the Modifier padding method:

Example:

 content = { padding ->
                MainScreenContainer(
                    navController = navController,
                    modifier = Modifier.padding(padding)
                )
            }
ahmnouira
  • 1,607
  • 13
  • 8
-5

app build.gradle add this:

 buildFeatures {
    compose = true
    kotlinOptions.freeCompilerArgs += "-Xopt-in=androidx.compose.material.ExperimentalMaterialApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=androidx.compose.foundation.ExperimentalFoundationApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=androidx.compose.ui.ExperimentalComposeUiApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=androidx.compose.animation.ExperimentalAnimationApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=androidx.compose.animation.graphics.ExperimentalAnimationGraphicsApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlinx.serialization.ExperimentalSerializationApi"
    kotlinOptions.freeCompilerArgs += "-Xopt-in=com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi"
}