Is there any way we can attach Bottom Sheet to Win...
# compose
s
Is there any way we can attach Bottom Sheet to Window directly without adding it in any Compose Activity inside Scaffold and invoking it. It should work like when it is needed just attach to window and display it on any composable. Any Idea ???
s
it is needed just attach to window and display it on any composable
Not sure you can do this with material's
BottomSheet
, however this is the idea:
Copy code
Box {
    val customBottomSheetState = rememberCustomBottomSheetState()

    CompositionLocalProvider(
        LocalCustomBottomSheetState provides customBottomSheetState
    ) {
        // or whatever, this is the entry point of your UI
        HostScreen() 
    }
    
    CustomBottomSheet(customBottomSheetState)
}

@Composable
fun CustomBottomSheet(state: CustomBottomSheetState) {
    if (state.isDisplayed) {
        state.content()
    }
}

class CustomBottomSheetState {
    var isDisplayed by mutableStateOf(false)

    var content: @Composable () -> Unit = {}

    fun display(content: @Composable () -> Unit) {
        this.content = content
        isDisplayed = true
    }
}
However I'm not quite sure this is a good practice. Also, you'd need your own logic to handle configuration changes.
s
Cool. Will give a try. Because adding a ModalSheet inside activity does hard binding with activity. So thought of this kind of working