https://kotlinlang.org logo
#compose
Title
# compose
s

Satyam G

05/30/2022, 7:36 AM
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

ste

05/30/2022, 8:22 AM
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

Satyam G

05/30/2022, 8:24 AM
Cool. Will give a try. Because adding a ModalSheet inside activity does hard binding with activity. So thought of this kind of working
4 Views