ボトムシート

マテリアル デザイン 3 のモーダル ボトムシートの例。

ボトムシートを実装する場合は、ModalBottomSheet コンポーザブルを使用できます。

content スロットを使用できます。このスロットは、ColumnScope を使用して、シート コンテンツ コンポーザブルを列にレイアウトします。

ModalBottomSheet(onDismissRequest = { /* Executed when the sheet is dismissed */ }) {
    // Sheet content
}

プログラムでシートの状態を制御する

プログラムでシートを開閉するには、SheetState を使用します。rememberModalBottomSheetState を使用して SheetState のインスタンスを作成できます。このインスタンスは、sheetState パラメータを使用して ModalBottomSheet に渡す必要があります。SheetState を使用すると、show 関数と hide 関数、および現在のシートの状態に関連するプロパティにアクセスできます。これらの suspend 関数には CoroutineScope が必要です(rememberCoroutineScope を使用するなど)。また、UI イベントに応じて呼び出すことができます。ボトムシートを非表示にするときに、コンポジションから ModalBottomSheet を削除してください。

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")
            }
        }
    }
}

コンテンツを表示する Jetpack Compose のモーダル ボトムシート。