compose-collapsing-toolbar icon indicating copy to clipboard operation
compose-collapsing-toolbar copied to clipboard

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

Open alperenbabagil opened this issue 3 years ago • 6 comments
trafficstars

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")
            }
        }

alperenbabagil avatar Jul 19 '22 13:07 alperenbabagil

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.

onebone avatar Jul 19 '22 15:07 onebone

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.

alperenbabagil avatar Jul 19 '22 21:07 alperenbabagil

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

onebone avatar Aug 01 '22 15:08 onebone

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?

witchcraft2001 avatar Dec 11 '22 15:12 witchcraft2001

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?

onebone avatar Dec 14 '22 12:12 onebone

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?

witchcraft2001 avatar Dec 21 '22 10:12 witchcraft2001