{"id":245,"date":"2023-02-28T22:47:10","date_gmt":"2023-02-28T14:47:10","guid":{"rendered":"https:\/\/www.jinhaihan.top\/WordPress\/?p=245"},"modified":"2023-02-28T22:47:15","modified_gmt":"2023-02-28T14:47:15","slug":"jetpack-compose%e5%ba%95%e9%83%a8%e7%8a%b6%e6%80%81%e6%a0%8ffab%e5%88%b6%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.jinhaihan.top\/WordPress\/jetpack-compose%e5%ba%95%e9%83%a8%e7%8a%b6%e6%80%81%e6%a0%8ffab%e5%88%b6%e4%bd%9c\/","title":{"rendered":"<strong>Jetpack Compose\u5e95\u90e8\u72b6\u6001\u680f+Fab\u5236\u4f5c<\/strong>"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"244\" src=\"https:\/\/i0.wp.com\/www.jinhaihan.top\/WordPress\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-28-at-10.32.53-PM.png?resize=696%2C244&#038;ssl=1\" alt=\"\" class=\"wp-image-246\"\/><\/figure>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><p>\u6548\u679c\u56fe\u5982\u4e0a\uff1a<\/p>\n<p>\u63a7\u4ef6\u9ed8\u8ba4\u652f\u6301\u4e00\u4e9b\u5c5e\u6027\u7684\u914d\u7f6e\uff0c\u4e0b\u9762\u76f4\u63a5\u4e0a\u4ee3\u7801\uff0c\u5728\u4ee3\u7801\u4e2d\u52a0\u6ce8\u91ca\u89e3\u91ca\uff1a<\/p>\n<p><strong>\u6ce8\u610f\uff1a\u9700\u8981\u5bfc\u5165<code>androidx.compose.material<\/code>\u5305\uff0c\u5982\u679c\u5bfc\u5165material3\u7684\u5305\u4f1a\u5bfc\u81f4\u989c\u8272\u8bbe\u7f6e\u3001\u67d0\u4e9b\u8bbe\u7f6e\u9879\u5f02\u5e38<\/strong><\/p>\n<pre><code class=\"language-kotlin\">@Composable  \nfun Greeting2() {  \n\t\/\/ \u521b\u5efa\u5bfc\u822a\n    val navController = rememberNavController()  \n    val scaffoldState = rememberScaffoldState()  \n    Scaffold(  \n\t    \/\/ \u521b\u5efa\u5e95\u90e8\u5bfc\u822a\u680f\uff0c\u548cNavHost\u7ed3\u5408\u5bfc\u822a\u3002\u4f20\u5165navController\u8fdb\u884c\u70b9\u51fb\u63a7\u5236\u3002\n        bottomBar = { MyBottomBar(navController) },  \n        \/\/ \u65b0\u5efa\u4e00\u4e2a\u60ac\u6d6eFab\uff0c\u7528\u4e8e\u5d4c\u5165\n        floatingActionButton = {  \n            FloatingActionButton(  \n                modifier = Modifier.size(156.dp),  \n                onClick = {  \n                \/\/ FAB onClick  \n            },  \n                shape = CircleShape) {  \n                Icon(imageVector = Icons.Default.Add, contentDescription = &quot;Add&quot;, modifier = Modifier.size(100.dp))  \n            }  \n        },  \n  \n        scaffoldState = scaffoldState,  \n        \/\/ Fab\u662f\u5426\u5d4c\u5165\u5e95\u90e8\u680f\n        isFloatingActionButtonDocked = true,  \n        \/\/ Fab\u5d4c\u5165\u5e95\u90e8\u680f\u7684\u4f4d\u7f6e\n        floatingActionButtonPosition = FabPosition.Center  \n        \n    ) { innerPadding -&gt;  \n\t    \/\/ \u5bfc\u822a\uff0c\u4e00\u5171\u6709\u56db\u4e2a\u9875\u9762\uff0c\u6bcf\u4e2a\u9875\u9762\u90fd\u662f\u4e00\u4e2aComposable\n        NavHost(navController, startDestination = MainActivityPages.Profile.route, Modifier.padding(innerPadding)) {  \n            composable(MainActivityPages.Profile.route) { Text(text = &quot;aaa&quot;) }  \n            composable(MainActivityPages.Info.route) { Text(text = &quot;bbb&quot;) }  \n            composable(MainActivityPages.Message.route) { Text(text = &quot;bbb&quot;) }  \n            composable(MainActivityPages.My.route) { Text(text = &quot;bbb&quot;) }  \n        }    }}  \n  \n@Composable  \nfun MyBottomBar(navController: NavHostController) {  \n\t\/\/ \u51c6\u5907\u5bfc\u822a\u6309\u94ae\n    val bottomMenuItemsList = prepareBottomMenu()  \n\t\/\/ \u8bbe\u5b9a\u5bfc\u822a\u7684\u6837\u5f0f\u3001\u5d4c\u5165\u7684\u5f62\u72b6\u7b49\n    BottomAppBar(backgroundColor = Color.White,  \n        cutoutShape = CircleShape,  \n        modifier = Modifier.height(180.dp)  \n    ) {  \n\t    \/\/ \u83b7\u53d6\u5f53\u524d\u5bfc\u822a\u7684\u5185\u5bb9\n        val navBackStackEntry by navController.currentBackStackEntryAsState()  \n        val currentDestination = navBackStackEntry?.destination  \n        bottomMenuItemsList.forEachIndexed { index, menuItem -&gt;\n\t        \/\/ \u4e00\u5171\u6709\u56db\u4e2a\u6309\u94ae\uff0cFab\u5c45\u4e2d\uff0c\u6240\u4ee5\u8981\u5728\u4e2d\u95f4\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6309\u94ae\u5360\u4f4d  \n            if (index == 2) {  \n                \/\/ add an empty space for FAB  \n                BottomNavigationItem(  \n                    modifier = Modifier.size(156.dp),  \n                    selected = false,  \n                    onClick = {},  \n                    icon = {},  \n                    enabled = false  \n                )  \n            }  \n\t\t\t\/\/ \u5176\u4f59\u6309\u94ae\u6b63\u5e38\u5904\u7406\n            BottomNavigationItem(  \n\t            \/\/ \u9009\u62e9\u7684\u989c\u8272\n                selectedContentColor = Color.Red,  \n                \/\/ \u672a\u9009\u62e9\u7684\u989c\u8272\n                unselectedContentColor = Color.Gray,  \n                \/\/ \u662f\u5426\u4e00\u76f4\u663e\u793a\u6807\u9898\uff0cfalse\u4e3a\u9009\u4e2d\u624d\u663e\u793a\u6807\u9898\n                alwaysShowLabel = false,  \n                modifier = Modifier.size(100.dp),  \n                onClick = {  \n\t                \/\/ \u70b9\u51fb\u4e8b\u4ef6\uff0c\u5229\u7528\u5bfc\u822a\n                    navController.navigate(menuItem.route) {  \n                        \/\/ Pop up to the start destination of the graph to  \n                        \/\/ avoid building up a large stack of destinations                        \/\/ on the back stack as users select items                        popUpTo(navController.graph.findStartDestination().id) {  \n                            saveState = true  \n                        }  \n                        \/\/ Avoid multiple copies of the same destination when  \n                        \/\/ reselecting the same item                        launchSingleTop = true  \n                        \/\/ Restore state when reselecting a previously selected item  \n                        restoreState = true  \n                    }  \n                },  \n                label = { Text(menuItem.label, fontSize = 35.sp) },  \n                icon = {  \n                    Icon(  \n                        imageVector = menuItem.icon,  \n                        contentDescription = menuItem.label,  \n                        modifier = Modifier.size(100.dp),  \n                    )  \n                },  \n                \/\/ \u5229\u7528\u5bfc\u822a\u5224\u65ad\u5f53\u524d\u6309\u94ae\u662f\u5426\u9009\u62e9\n                selected = currentDestination?.hierarchy?.any { it.route == menuItem.route } == true,\/\/(selectedItem == menuItem.route),  \n                enabled = true  \n            )  \n        }  \n    }}  \n  \nprivate fun prepareBottomMenu(): List&lt;MainActivityPages&gt; {  \n    val bottomMenuItemsList = arrayListOf&lt;MainActivityPages&gt;()  \n  \n    \/\/ add menu items  \n    bottomMenuItemsList.add(MainActivityPages.Profile)  \n    bottomMenuItemsList.add(MainActivityPages.Info)  \n    bottomMenuItemsList.add(MainActivityPages.Message)  \n    bottomMenuItemsList.add(MainActivityPages.My)  \n  \n    return bottomMenuItemsList  \n}  \n  \ndata class BottomMenuItem(val label: String, val icon: ImageVector, val route: String)  \n  \n@Preview(showBackground = true)  \n@Composable  \nfun GreetingPreview2() {  \n    HomeOfCarTheme {  \n        Greeting2()  \n    }  \n}\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7,8],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bolg","category-android"],"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jinhaihan.top\/WordPress\/wp-content\/uploads\/2023\/02\/49830289321_83c90a1dd4_b.jpg?fit=1024%2C683&ssl=1","_links":{"self":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":1,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/posts\/245\/revisions\/248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/media\/247"}],"wp:attachment":[{"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jinhaihan.top\/WordPress\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}