Tin Tran
05/24/2021, 2:43 PMModalBottomSheetLayout
. Code in 🧵Tin Tran
05/24/2021, 2:43 PM@ExperimentalMaterialApi
@Composable
@Preview
fun Test() {
val scope = rememberCoroutineScope()
val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
var testVar by rememberSaveable { mutableStateOf(true)}
val openSheet: (Boolean) -> Unit = {
scope.launch {
testVar = it
sheetState.show()
Log.i("test", "test")
}
}
ModalBottomSheetLayout(
sheetState = sheetState,
sheetContent = {
Box(
modifier = Modifier
.padding(top = 12.dp)
.requiredSize(50.dp, 4.dp)
.clip(RoundedCornerShape(2.dp))
.background(DarkGray)
.align(alignment = Alignment.CenterHorizontally)
)
if (testVar) {
TestModal1()
} else
TestModal2()
},
sheetShape = RoundedCornerShape(topStart = 18.dp, topEnd = 18.dp),
) {
Row() {
Button(onClick = {
openSheet(true)
}) {
Text(text = "open sheet")
}
Button(onClick = {
openSheet(false)
}) {
Text(text = "open sheet2")
}
}
}
}
@Composable
@Preview
fun TestModal2() {
Column(
modifier = Modifier
.padding(start = 28.dp, end = 28.dp, top = 10.dp, bottom = 28.dp)
.fillMaxWidth(),
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Image(
modifier = Modifier.padding(end = 8.dp),
painter = painterResource(id = R.drawable.ic_full_swing_lesson),
contentDescription = ""
)
Text(text = "TITLE 2", style = Roboto17W700)
}
ModalOption("Option 2")
Spacer(modifier = Modifier.padding(8.dp))
ModalOption("Option 2")
}
}
@Composable
@Preview
fun TestModal1() {
Column(
modifier = Modifier
.padding(start = 28.dp, end = 28.dp, top = 10.dp, bottom = 28.dp)
.fillMaxWidth(),
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Image(
modifier = Modifier.padding(end = 8.dp),
painter = painterResource(id = R.drawable.ic_full_swing_lesson),
contentDescription = ""
)
Text(text = "TITLE 1", style = Roboto17W700)
}
ModalOption("Option 1")
Spacer(modifier = Modifier.padding(8.dp))
ModalOption("Option 1")
}
}
@Composable
fun ModalOption(text: String, onClick: () -> Unit = {}) {
Card(
modifier = Modifier.fillMaxWidth(),
elevation = 8.dp,
shape = Rounded8dpShape,
backgroundColor = Color.White
) {
Box(
modifier = Modifier.clickable {
onClick
},
contentAlignment = Alignment.CenterStart
) {
Row(
modifier = Modifier
.padding(14.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier.fillMaxWidth(.95f),
text = text
)
Image(
painter = painterResource(id = R.drawable.ic_orange_arrow_right),
contentDescription = ""
)
}
}
}
}
Tin Tran
05/24/2021, 2:44 PMModalBottomSheet
Tin Tran
05/24/2021, 2:46 PMtestVar
it will trigger a recompose and when that recompose finish after the I call the sheetState.show()
method it just do nothingTin Tran
05/24/2021, 2:46 PMTin Tran
05/24/2021, 2:47 PMdelay(150)
before calling the sheetState.show()
method to make sure the recompose has finish. It worked fineTin Tran
05/24/2021, 2:47 PMRavi
05/24/2021, 3:00 PMclass BottomSheetController constructor(private val scope: CoroutineScope) {
private var job: Job? = null
@OptIn(ExperimentalMaterialApi::class)
fun showBottomSheet(
sheetState: ModalBottomSheetState,
block: () -> Unit,
) {
job?.cancel()
job = scope.launch {
block()
delay(200)
sheetState.show()
}
}
}
Ravi
05/24/2021, 3:00 PM200
msRavi
05/24/2021, 3:02 PMval bottomController by remember { BottomSheetController(scope) }
bottomController.showBottomSheet(sheetState) {
//any init stuff
}
Tin Tran
05/24/2021, 3:08 PMTin Tran
05/24/2021, 3:08 PMRavi
05/24/2021, 3:15 PMTin Tran
05/24/2021, 3:27 PM