a

    AG

    1 year ago
    can someone help me to understand why changing bottomSheetScaffoldState value inside launchedeffect not updating bottom sheet, with coroutinescope.launch everything works right, code in a thread
    Surface(color = MaterialTheme.colors.background) {
        var state by remember {
            mutableStateOf(false)
        }
        BottomSheetSample(
            onExpandButtonClicked = { state = !state },
            onMainButtonClicked = { /**TODO**/ },
            expand = state
        )
    }
    @ExperimentalMaterialApi
    @Composable
    fun BottomSheetSample(
        onMainButtonClicked: () -> Unit,
        onExpandButtonClicked: () -> Unit,
        expand: Boolean
    ) {
        val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
            bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
        )
    
        LaunchedEffect(expand) {
            if (expand) {
                bottomSheetScaffoldState.bottomSheetState.expand()
            } else {
                bottomSheetScaffoldState.bottomSheetState.collapse()
            }
        }
    
        BottomSheetScaffold(
            scaffoldState = bottomSheetScaffoldState,
            sheetContent = {
                Box(
                    Modifier
                        .fillMaxWidth()
                        .fillMaxHeight(0.5f)
                ) {
                    Column(modifier = Modifier.matchParentSize()) {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .weight(1f)
                        ) {
                            Text(
                                modifier = Modifier.align(Alignment.Center),
                                text = "Text",
                                style = TextStyle(fontSize = 26.sp)
                            )
                        }
                        Button(
                            onClick = { onMainButtonClicked() },
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(horizontal = 16.dp, vertical = 8.dp)
                                .height(48.dp)
                        ) {
                            Text("Main button")
                        }
                    }
                }
            }, sheetPeekHeight = 0.dp,
            sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)
        ) {
            Button(onClick = {
                onExpandButtonClicked()
            }) {
                Text(text = "Expand/Collapse Bottom Sheet")
            }
        }
    }
    LaunchedEffect(expand) {
        if (expand) {
            bottomSheetScaffoldState.bottomSheetState.expand()
        } else {
            bottomSheetScaffoldState.bottomSheetState.collapse()
        }
    }
    so this code has no affect bottom sheet not expanding
    m

    ms

    1 year ago
    You need to remember BottomSheetState also
    a

    AG

    1 year ago
    thx! 👍 it's helped