Created
August 7, 2024 13:35
-
-
Save y2k/e7f969e8906c0bdc2908a387b3c4ffc5 to your computer and use it in GitHub Desktop.
SampleModalBottomSheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.testmbs | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.WindowInsets | |
import androidx.compose.foundation.layout.asPaddingValues | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.navigationBars | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.wrapContentHeight | |
import androidx.compose.foundation.rememberScrollState | |
import androidx.compose.foundation.verticalScroll | |
import androidx.compose.material3.BottomSheetDefaults | |
import androidx.compose.material3.ExperimentalMaterial3Api | |
import androidx.compose.material3.ModalBottomSheet | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.rememberModalBottomSheetState | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.dp | |
import com.example.testmbs.ui.theme.MyTestTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
MyTestTheme { | |
Box { | |
var opened by remember { mutableStateOf(true) } | |
Column( | |
modifier = Modifier.verticalScroll(rememberScrollState()) | |
) { | |
for (i in 1..40) { | |
Text( | |
modifier = Modifier | |
.clickable { opened = true } | |
.background(if (i % 2 == 0) Color.Gray else Color.LightGray) | |
.fillMaxWidth() | |
.padding(16.dp), | |
text = "Item $i" | |
) | |
} | |
} | |
if (opened) | |
SampleModalBottomSheet( | |
onDismissRequest = { opened = false }, | |
content = { | |
Column { | |
for (i in 1..4) { | |
Text( | |
modifier = Modifier | |
.background(if (i % 2 == 0) Color.Gray else Color.LightGray) | |
.fillMaxWidth() | |
.padding(16.dp), | |
text = "Item $i" | |
) | |
} | |
} | |
} | |
) | |
} | |
} | |
} | |
} | |
} | |
@OptIn(ExperimentalMaterial3Api::class) | |
@Composable | |
fun SampleModalBottomSheet( | |
modifier: Modifier = Modifier, | |
onDismissRequest: () -> Unit, | |
content: @Composable () -> Unit | |
) { | |
val bottomPadding = WindowInsets.navigationBars.asPaddingValues() | |
ModalBottomSheet( | |
onDismissRequest = onDismissRequest, | |
windowInsets = WindowInsets(bottom = 0.dp), | |
sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true), | |
dragHandle = { BottomSheetDefaults.DragHandle() }, | |
modifier = modifier.wrapContentHeight(), | |
) { | |
Surface( | |
color = Color.Transparent, | |
modifier = Modifier | |
.padding(bottomPadding) | |
.padding(horizontal = 24.dp) | |
) { | |
content() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment