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

AG

07/18/2021, 7:48 AM
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
Copy code
Surface(color = MaterialTheme.colors.background) {
    var state by remember {
        mutableStateOf(false)
    }
    BottomSheetSample(
        onExpandButtonClicked = { state = !state },
        onMainButtonClicked = { /**TODO**/ },
        expand = state
    )
}
Copy code
@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")
        }
    }
}
Copy code
LaunchedEffect(expand) {
    if (expand) {
        bottomSheetScaffoldState.bottomSheetState.expand()
    } else {
        bottomSheetScaffoldState.bottomSheetState.collapse()
    }
}
so this code has no affect bottom sheet not expanding
m

ms

07/18/2021, 8:11 AM
You need to remember BottomSheetState also
🙌 1
a

AG

07/18/2021, 8:22 AM
thx! 👍 it's helped
4 Views