Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M3 NavigationBar をマスターする

akkiee76
November 05, 2023

M3 NavigationBar をマスターする

akkiee76

November 05, 2023
Tweet

More Decks by akkiee76

Other Decks in Technology

Transcript

  1. M3 NavigationBar 概要 • 画面下部に表示し、ナビゲーションを提供するコンポーネント • M2 BottomNavigation が M3

    NavigationBar に名称変更 • NavigationBar の要素は NavigationBarItem • 「Bottom Navigation Views Activity」 は M3 Compose 非対応 https://developer.android.com/jetpack/compose/designsystems/material2-material3?hl=ja#bottom-navigation NavigationBar BottomNavigation
  2. 1. NavigationBar を設定する Scaffold( bottomBar = { NavigationBar { }

    } ) { Surface( modifier = Modifier .padding(bottom = it.calculateBottomPadding()) ) { }
  3. 2. NavigationBarItem を追加する enum class NavigationItem ( val label: String,

    @DrawableRes val resId: Int ) { HOME("Home", R.drawable.ic_home), EXPLORE("Explore", R.drawable.ic_fmd_good), MESSAGE("Message", R.drawable.ic_chat), STARRED("For You", R.drawable.ic_star); }
  4. Scaffold( bottomBar = { NavigationBar { NavigationItem.entries.forEach { item ->

    NavigationBarItem( selected = false, onClick = {}, icon = { Icon( painter = painterResource(id = item.resId), contentDescription = null ) }, label = { Text(text = item.label) } ) } } } 2. NavigationBarItem を追加する
  5. NavigationBarItem の注意点(M3 Guidelines) OK Don’t • ユーザにとって遷移が明確であること • NavigationBarItem は

    3 ~ 5 であること • Label を表示すること https://m3.material.io/components/navigation-bar/guidelines
  6. build.gradle.kts dependencies { implementation("androidx.navigation:navigation-compose:2.7.4" ) } val navController = rememberNavController

    () Scaffold(bottomBar = { // NavigationBar } ) { Surface(modifier = Modifier.padding(bottom = it.calculateBottomPadding())) { NavHost(navController = navController , startDestination = NavigationItem .HOME.route) { composable(NavigationItem .HOME.route) { HomeScreen() } composable(NavigationItem .EXPLORE.route) { ExploreScreen () } composable(NavigationItem .MESSAGE.route) { MessageScreen () } composable(NavigationItem .STARRED.route) { FavoriteScreen () } } 3. Navigation を行う
  7. val navBackStackEntry by navController.currentBackStackEntryAsState() val destination = navBackStackEntry?.destination NavigationBar {

    NavigationItem.entries.forEach { item -> val selected = destination?.hierarchy ?.any { it.route == item.route } == true NavigationBarItem( selected = selected, onClick = { navController.navigate(item.route) { launchSingleTop = true } }, icon = { }, label = { } ) } } 3. Navigation を行う https://developer.android.com/jetpack/compose/navigation?hl=ja#bottom-nav
  8. NavigationBarItem( icon = { BadgedBox( badge = { Badge() }

    ) { Icon( painter = painterResource(id = item.resId), contentDescription = null ) } 4. Badge を付ける
  9. @ExperimentalMaterial3Api @Composable fun Badge( modifier: Modifier = Modifier, containerColor: Color

    = BadgeDefaults.containerColor, contentColor: Color = contentColorFor(containerColor), content: @Composable (RowScope.() -> Unit)? = null, ) BadgedBox( badge = { Badge { Text(text = "1") } }) { Icon( painter = painterResource(id = item.resId), contentDescription = null ) } 4. Badge を付ける(カスタマイズ)
  10. NavigationBarItem( selected = selected, onClick = onClick, icon = {

    val resId = if (selected) item.filledResId else item.outlinedResId Icon( painter = painterResource(id = resId), contentDescription = null ) }, label = { val fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal Text( text = item.label, fontWeight = fontWeight ) } ) M3 Guidelines に準拠する
  11. まとめ • NavigationBar は Item を content に指定するだけで 実装可能 ◦

    NavigationBarItem の Composable に詳細設定 • M3 Guidelines のチェックも忘れずに