2

Okay, so this is a bit of a specific issue, but hopefully someone understands what's happening:

I'm using a Jetpack Compose Material 3 CenterAlignedTopAppBar, with TopAppBarDefaults.pinnedScrollBehavior to make it so that the color of the appbar changes when I scroll down on nested content.

It works! In most cases. However, one of my screens has a large text field, that when clicked causes the content to scroll down by itself (to focus on the text field)—and that seems to confuse the appbar, which doesn't change color. It will only change color if I manually scroll up and down.

Relevant code:


val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())

Scaffold(
    contentWindowInsets = EmptyWindowInsets,
    modifier = Modifier
        .fillMaxHeight()
        .nestedScroll(scrollBehavior.nestedScrollConnection),
    topBar = {
        // just a wrapper around CenterAlignTopAppBar
        StandardTopAppBar(scrollBehavior = scrollBehavior)
    },
    content = { innerPadding ->
        
        // I've also tried with LazyColumn and see the same behavior
        Column(
            Modifier
                .padding(innerPadding)
                .padding(start = 10.dp, end = 10.dp)
                .fillMaxHeight()
                .verticalScroll(rememberScrollState()),
            verticalArrangement = Arrangement.spacedBy(10.dp),

            ) {

tl;dr; manually scrolling changes the appbar color, but scrolling caused by clicking into a text field that scrolls into view does not. Any idea how I could fix this?

Nathan
  • 73,987
  • 14
  • 40
  • 69

1 Answers1

1

Probably not quite what you are looking for unfortunately, but I had a similar problem. My TopAppBar color was also stuck, but when navigating between different screens. I fixed it by assigning the TopAppBar a different scrollBehaviour depending on the screen.

val scrollBehavior1 = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
val scrollBehavior2 = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())

In TopAppBar arguments:

scrollBehavior =
    when (currentRoute) {
        Screens.ExampleScreen1.route -> scrollBehavior1
        Screens.ExampleScreen2.route -> scrollBehavior2
        else -> null
    }

Hopefully this helps you in one way or another.

MisterrrX
  • 11
  • 1