ã¯ããã«
Android Jetpack Composeã§ãä¸ã¹ã¯ã¤ãã§ç»é¢ãéããæ¹æ³ã解説ãModalBottomSheet
ãç¨ããæ¹æ³ããããããã®è¨äºã§ã¯AnchoredDraggableState
ã«ããã¹ã¯ã¤ãæ¤ç¥ãã¦ç»é¢ãéããæ¹æ³ã«ã¤ãã¦è§£èª¬ã
æ¹æ³
1. ã¹ã¯ã¤ãéå§ã»çµäºã¯ã©ã¹ã®ä½æ
ã¹ã¯ã¤ãç¶æ ãå®ç¾©ããã¯ã©ã¹ä½æã
enum class AnchorPos { Start, End; }
2. ã¢ã¼ãã«ç»é¢ã®ä½æ
ã¢ã¼ãã«ç»é¢ãä½æ
@OptIn(ExperimentalFoundationApi::class) @Composable fun ModalScreen(navController: NavHostController) { val screenHeight = with(LocalDensity.current) { LocalConfiguration.current.screenHeightDp.dp.toPx() } val anchors = DraggableAnchors { AnchorPos.Start at 0f AnchorPos.End at screenHeight } // ãã©ãã°å¯è½ãªè·é¢ã¨ç¶æ ãè¨å® val draggableState = remember { AnchoredDraggableState( initialValue = AnchorPos.Start, anchors = anchors, positionalThreshold = { it * 0.5f }, velocityThreshold = { screenHeight * 0.5f }, snapAnimationSpec = SpringSpec(), decayAnimationSpec = exponentialDecay(), confirmValueChange = { when (it) { AnchorPos.Start -> {} AnchorPos.End -> { //è¤æ°åpopãè¡ããããã¨ãé²ããããç¾å¨ã®ç»é¢ã§ã®ã¿popãè¡ãã if (navController.currentDestination?.route == NavItem.MODAL.route) { navController.popBackStack() } } } true } ) } Box( modifier = Modifier .offset { IntOffset( x = 0, y = draggableState .requireOffset() .roundToInt() ) } .anchoredDraggable( state = draggableState, orientation = Orientation.Vertical ) .fillMaxSize() .background(Color.DarkGray), ) { //æ»ããã¿ã³ Button( onClick = { navController.popBackStack() }, colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent), contentPadding = PaddingValues(0.dp) ) { Icon( modifier = Modifier.size(32.dp), contentDescription = "Close", imageVector = Icons.Default.Close ) } } }