iT邦幫忙

2024 iThome 鐵人賽

DAY 15
0
Mobile Development

從零開始學習 Jetpack Compose系列 第 15

從零開始學習 Jetpack Compose Day14 - BottomSheet

  • 分享至 

  • xImage
  •  

BottomSheet

BottomSheet 在 Jetpack Compose 中為允許從螢幕底部顯示可拖動的元件,提供額外的操作選項或資訊,而不會中斷主要的使用者界面。

rememberModalBottomSheetState :記錄BottomSheet是否顯示

skipPartiallyExpanded:設定BottomSheet 被顯示時要呈現滿版還是部分畫面

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Greeting(modifier: Modifier = Modifier) {
    val sheetState = rememberModalBottomSheetState(
        skipPartiallyExpanded = false
    )
    var showBottomSheet by remember { mutableStateOf(false) }
    Column(
        modifier = Modifier.fillMaxWidth().fillMaxHeight().padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(
            onClick = { showBottomSheet = true }
        ) {
            Text("Show Bottomsheet")
        }

        if (showBottomSheet) {
            ModalBottomSheet(
                modifier = Modifier.fillMaxHeight(),
                sheetState = sheetState,
                onDismissRequest = { showBottomSheet = false }
            ) {
                Column {
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        IconButton(onClick = {  }) {
                            Icon(
                                imageVector = Icons.AutoMirrored.Filled.Send,
                                contentDescription = ""
                            )
                        }
                        Text("Send")
                    }

                    Row (
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        IconButton(onClick = {  }) {
                            Icon(
                                imageVector = Icons.Filled.AddCircle,
                                contentDescription = ""
                            )
                        }
                        Text("Add")
                    }
                    Row (
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        IconButton(onClick = {  }) {
                            Icon(
                                imageVector = Icons.Filled.Create,
                                contentDescription = ""
                            )
                        }
                        Text("Create")
                    }
                }
            }
        }
    }
}

https://raw.githubusercontent.com/jian-fu-hung/ithelp-2024/refs/heads/main/Images/Day14/%E6%88%AA%E5%9C%96%202024-09-29%20%E6%99%9A%E4%B8%8A11.29.00.png

當 skipPartiallyExpanded 設為 true 時

https://raw.githubusercontent.com/jian-fu-hung/ithelp-2024/refs/heads/main/Images/Day14/%E6%88%AA%E5%9C%96%202024-09-29%20%E6%99%9A%E4%B8%8A11.32.06.png

ModalBottomSheet 不設定 fillMaxHeight 時

https://raw.githubusercontent.com/jian-fu-hung/ithelp-2024/refs/heads/main/Images/Day14/%E6%88%AA%E5%9C%96%202024-09-29%20%E6%99%9A%E4%B8%8A11.32.26.png


上一篇
從零開始學習 Jetpack Compose Day13 - LazyColumn與LazyRow
下一篇
從零開始學習 Jetpack Compose Day15 - Canvas
系列文
從零開始學習 Jetpack Compose30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言