หากต้องการใช้ Bottom Sheet คุณสามารถใช้
ModalBottomSheet Composable ได้
คุณสามารถใช้ช่อง content ซึ่งใช้ ColumnScope เพื่อจัดวาง Composable ของเนื้อหาชีต
ในคอลัมน์
ModalBottomSheet(onDismissRequest = { /* Executed when the sheet is dismissed */ }) { // Sheet content }
ควบคุมสถานะชีตด้วยโปรแกรม
หากต้องการขยายและยุบชีตด้วยโปรแกรม ให้ใช้
SheetState คุณสามารถใช้ rememberModalBottomSheetState เพื่อสร้าง
อินสแตนซ์ของ SheetState ที่ต้องส่งไปยัง ModalBottomSheet ด้วย
พารามิเตอร์ sheetState SheetState ช่วยให้เข้าถึงฟังก์ชัน show
และ hide รวมถึงพร็อพเพอร์ตี้ที่เกี่ยวข้องกับสถานะชีต
ปัจจุบันได้ ฟังก์ชันระงับเหล่านี้ต้องใช้ CoroutineScope เช่น
การใช้ rememberCoroutineScope และคุณสามารถเรียกฟังก์ชันเหล่านี้เพื่อตอบสนองต่อเหตุการณ์ UI
ได้ อย่าลืมนำ ModalBottomSheet ออกจากการจัดองค์ประกอบเมื่อซ่อน Bottom Sheet
val sheetState = rememberModalBottomSheetState() val scope = rememberCoroutineScope() var showBottomSheet by remember { mutableStateOf(false) } Scaffold( floatingActionButton = { ExtendedFloatingActionButton( text = { Text("Show bottom sheet") }, icon = { Icon(Icons.Filled.Add, contentDescription = "") }, onClick = { showBottomSheet = true } ) } ) { contentPadding -> // Screen content if (showBottomSheet) { ModalBottomSheet( onDismissRequest = { showBottomSheet = false }, sheetState = sheetState ) { // Sheet content Button(onClick = { scope.launch { sheetState.hide() }.invokeOnCompletion { if (!sheetState.isVisible) { showBottomSheet = false } } }) { Text("Hide bottom sheet") } } } }