Skip to content

Commit 07b51c2

Browse files
authored
Partial bottom sheet example (#279)
* Example of a bottom sheet that only partially displays * Minor tweaks, adding regions tags * Apply Spotless * Adding padding --------- Co-authored-by: jakeroseman <[email protected]>
1 parent b883fb9 commit 07b51c2

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.example.compose.snippets.components.ComponentsScreen
3737
import com.example.compose.snippets.components.DialogExamples
3838
import com.example.compose.snippets.components.DividerExamples
3939
import com.example.compose.snippets.components.FloatingActionButtonExamples
40+
import com.example.compose.snippets.components.PartialBottomSheet
4041
import com.example.compose.snippets.components.ProgressIndicatorExamples
4142
import com.example.compose.snippets.components.ScaffoldExample
4243
import com.example.compose.snippets.components.SliderExamples
@@ -101,6 +102,7 @@ class SnippetsActivity : ComponentActivity() {
101102
TopComponentsDestination.CheckboxExamples -> CheckboxExamples()
102103
TopComponentsDestination.DividerExamples -> DividerExamples()
103104
TopComponentsDestination.BadgeExamples -> BadgeExamples()
105+
TopComponentsDestination.PartialBottomSheet -> PartialBottomSheet()
104106
}
105107
}
106108
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.components
18+
19+
import androidx.compose.foundation.layout.Column
20+
import androidx.compose.foundation.layout.fillMaxHeight
21+
import androidx.compose.foundation.layout.fillMaxWidth
22+
import androidx.compose.foundation.layout.padding
23+
import androidx.compose.material3.Button
24+
import androidx.compose.material3.ExperimentalMaterial3Api
25+
import androidx.compose.material3.ModalBottomSheet
26+
import androidx.compose.material3.Text
27+
import androidx.compose.material3.rememberModalBottomSheetState
28+
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.getValue
30+
import androidx.compose.runtime.mutableStateOf
31+
import androidx.compose.runtime.remember
32+
import androidx.compose.runtime.setValue
33+
import androidx.compose.ui.Alignment
34+
import androidx.compose.ui.Modifier
35+
import androidx.compose.ui.tooling.preview.Preview
36+
import androidx.compose.ui.unit.dp
37+
38+
@OptIn(ExperimentalMaterial3Api::class)
39+
@Preview
40+
// [START android_compose_components_partialbottomsheet]
41+
@Composable
42+
fun PartialBottomSheet() {
43+
var showBottomSheet by remember { mutableStateOf(false) }
44+
val sheetState = rememberModalBottomSheetState(
45+
skipPartiallyExpanded = false,
46+
)
47+
48+
Column(
49+
modifier = Modifier.fillMaxWidth(),
50+
horizontalAlignment = Alignment.CenterHorizontally,
51+
) {
52+
Button(
53+
onClick = { showBottomSheet = true }
54+
) {
55+
Text("Display partial bottom sheet")
56+
}
57+
58+
if (showBottomSheet) {
59+
ModalBottomSheet(
60+
modifier = Modifier.fillMaxHeight(),
61+
sheetState = sheetState,
62+
onDismissRequest = { showBottomSheet = false }
63+
) {
64+
Text(
65+
"Swipe up to open sheet. Swipe down to dismiss.",
66+
modifier = Modifier.padding(16.dp)
67+
)
68+
}
69+
}
70+
}
71+
}
72+
// [END android_compose_components_partialbottomsheet]

compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ enum class TopComponentsDestination(val route: String, val title: String) {
4040
CheckboxExamples("checkboxExamples", "Checkbox"),
4141
DividerExamples("dividerExamples", "Dividers"),
4242
BadgeExamples("badgeExamples", "Badges"),
43+
PartialBottomSheet("partialBottomSheets", "Partial Bottom Sheet"),
4344
}

0 commit comments

Comments
 (0)