onebone/compose-collapsing-toolbar

ToolbarState expand(), collapse() and scroll not working when toolbar's content is set with delay

Opened this issue · 6 comments

Hello, I can't expand or collapse the toolbar programmatically. It works nicely when I scroll a lazyColumn inside it but
collapsingToolbarScaffoldState.toolbarState.expand() or collapsingToolbarScaffoldState.toolbarState.collapse() never works. I know these are in experimental state, so decided to scroll it manually but it is not working either. I will share my code below and any help would be much appreciated.

val collapsingToolbarScaffoldState = rememberCollapsingToolbarScaffoldState()

CollapsingToolbarScaffold(
        state = collapsingToolbarScaffoldState, 
        toolbar = {
            basePageComposableParams.collapsableToolbarContent.value.invoke()
        },
        modifier = Modifier,
        scrollStrategy = ScrollStrategy.EnterAlways,
    ) {
        Scaffold(
            floatingActionButton = basePageComposableParams.floatingActionButtonContent.value
        ) {
            ModalBottomSheetLayout(
                modifier = Modifier,
                sheetState = sheetState,
                sheetContent = {
                    basePageComposableParams.bottomSheetContent.value.invoke()
                }) {
                content()
            }
        }
    }

I call the expand and collapse functions in an onClick callback of a FAB

val crScope = rememberCoroutineScope()
        floatingActionButtonContent.value = @Composable {
            FloatingActionButton(
                onClick = {
                    crScope.launch {
                        focusRequester.requestFocus()
                        collapsingToolbarScaffoldState.toolbarState.expand()
                    }
                },
            ) {
                Icon(Icons.Filled.Search, contentDescription = "search")
            }
        }

expand() and collapse() works fine on my mimicked sample that I wrote, can you check that expand() is called?

I do not know how your code is structured, if the coroutine scope is somehow canceled before the floating button is clicked then it might not be called.

expand() and collapse() works fine on my mimicked sample that I wrote, can you check that expand() is called?

I do not know how your code is structured, if the coroutine scope is somehow canceled before the floating button is clicked then it might not be called.

Thanks for the answer. The problem was I was setting the content of the toolbar after setting the content of the body. I've fixed it by doing 2 things: First I had to add a dummy 1.dp spacer to the toolbar. Second I set the toolbar content before body content. It changed my structure a little bit and made it uglier, I would make it the old way in the future :D
You can close the issue if you don't think there is no need for action.

I will leave it open for now. Actually I didn't think of the case where the layout is resolved with delay, maybe I should deal with it anyway :D

Hi onebone,

I faced the same problem. I have prepared an example code that shows the problem, it's based on sample code from your repository. Please tell me what is my mistake and how to make the toolbar expand when the expand() method is called?

Hi onebone,

I faced the same problem. I have prepared an example code that shows the problem, it's based on sample code from your repository. Please tell me what is my mistake and how to make the toolbar expand when the expand() method is called?

I have looked at your code, the toolbar seems to have a fixed height (hence it is not expandable). What layout do you expect when it is expanded?

I expect that after hiding the toolbar when scrolling through the contents of the list, the expand method will reveal this toolbar. How can this be achieved?