提取Scaffold,部分核心页面才有底部导航栏【但是性能变差了】
This commit is contained in:
@@ -12,6 +12,7 @@ import androidx.compose.foundation.background
|
|||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
import androidx.compose.foundation.layout.WindowInsets
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
@@ -63,6 +64,9 @@ val LocalNavController = compositionLocalOf<NavHostController> {
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun NavigationController(navController: NavHostController) {
|
fun NavigationController(navController: NavHostController) {
|
||||||
|
val navigationBarHeight = with(LocalDensity.current) {
|
||||||
|
WindowInsets.navigationBars.getBottom(this).toDp()
|
||||||
|
}
|
||||||
NavHost(
|
NavHost(
|
||||||
navController = navController,
|
navController = navController,
|
||||||
startDestination = NavigationItem.Home.route,
|
startDestination = NavigationItem.Home.route,
|
||||||
@@ -72,60 +76,117 @@ fun NavigationController(navController: NavHostController) {
|
|||||||
exitTransition = {
|
exitTransition = {
|
||||||
fadeOut(animationSpec = tween(0))
|
fadeOut(animationSpec = tween(0))
|
||||||
}) {
|
}) {
|
||||||
composable(route = NavigationItem.Home.route) {
|
// 带底部导航栏的路由:
|
||||||
Home()
|
listOf(NavigationItem.Home,
|
||||||
|
NavigationItem.Street,
|
||||||
|
NavigationItem.Add,
|
||||||
|
NavigationItem.Message,
|
||||||
|
NavigationItem.Profile).forEach { item ->
|
||||||
|
composable(route = item.route) {
|
||||||
|
ScaffoldWithNavigationBar(navController) {
|
||||||
|
when (item) {
|
||||||
|
NavigationItem.Home -> Home()
|
||||||
|
NavigationItem.Street -> Street()
|
||||||
|
NavigationItem.Add -> Add()
|
||||||
|
NavigationItem.Message -> Video()
|
||||||
|
NavigationItem.Profile -> Profile()
|
||||||
|
else -> {} // 由于过滤,这里不会发生
|
||||||
}
|
}
|
||||||
composable(route = NavigationItem.Street.route) {
|
|
||||||
Street()
|
|
||||||
}
|
}
|
||||||
composable(route = NavigationItem.Add.route) {
|
|
||||||
Add()
|
|
||||||
}
|
}
|
||||||
composable(route = NavigationItem.Message.route) {
|
|
||||||
Video()
|
|
||||||
}
|
|
||||||
composable(route = NavigationItem.Profile.route) {
|
|
||||||
Profile()
|
|
||||||
}
|
}
|
||||||
composable(route = "ProfileTimeline") {
|
composable(route = "ProfileTimeline") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
GalleryPage()
|
GalleryPage()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="LocationDetail") {
|
composable(route="LocationDetail") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
LocationDetail()
|
LocationDetail()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="OfficialPhoto") {
|
composable(route="OfficialPhoto") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
OfficialGalleryPage()
|
OfficialGalleryPage()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="OfficialPhotographer") {
|
composable(route="OfficialPhotographer") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
OfficialPhotographer()
|
OfficialPhotographer()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="Post") {
|
composable(route="Post") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
PostPage()
|
PostPage()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="ModificationList") {
|
composable(route="ModificationList") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
ModificationListScreen()
|
ModificationListScreen()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="MyMessage") {
|
composable(route="MyMessage") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
NotificationsScreen()
|
NotificationsScreen()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="Comments") {
|
composable(route="Comments") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
CommentsScreen()
|
CommentsScreen()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="Likes") {
|
composable(route="Likes") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
LikePage()
|
LikePage()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
composable(route="Followers") {
|
composable(route="Followers") {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
FollowerPage()
|
FollowerPage()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Navigation() {
|
fun Navigation() {
|
||||||
|
val navController = rememberNavController()
|
||||||
|
CompositionLocalProvider(LocalNavController provides navController) {
|
||||||
|
Box {
|
||||||
|
NavigationController(navController = navController)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 用于带导航栏的路由的可复用 composable
|
||||||
|
@Composable
|
||||||
|
fun ScaffoldWithNavigationBar(
|
||||||
|
navController: NavHostController,
|
||||||
|
content: @Composable () -> Unit
|
||||||
|
) {
|
||||||
val navigationBarHeight = with(LocalDensity.current) {
|
val navigationBarHeight = with(LocalDensity.current) {
|
||||||
WindowInsets.navigationBars.getBottom(this).toDp()
|
WindowInsets.navigationBars.getBottom(this).toDp()
|
||||||
}
|
}
|
||||||
val navController = rememberNavController()
|
|
||||||
val item = listOf(
|
val item = listOf(
|
||||||
NavigationItem.Home,
|
NavigationItem.Home,
|
||||||
NavigationItem.Street,
|
NavigationItem.Street,
|
||||||
@@ -133,13 +194,9 @@ fun Navigation() {
|
|||||||
NavigationItem.Message,
|
NavigationItem.Message,
|
||||||
NavigationItem.Profile
|
NavigationItem.Profile
|
||||||
)
|
)
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
modifier = Modifier.statusBarsPadding(),
|
modifier = Modifier.statusBarsPadding(),
|
||||||
topBar = {},
|
bottomBar = { NavigationBar(
|
||||||
|
|
||||||
bottomBar = {
|
|
||||||
NavigationBar(
|
|
||||||
modifier = Modifier.height(56.dp + navigationBarHeight),
|
modifier = Modifier.height(56.dp + navigationBarHeight),
|
||||||
containerColor = Color.Black
|
containerColor = Color.Black
|
||||||
) {
|
) {
|
||||||
@@ -192,17 +249,15 @@ fun Navigation() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
) { it
|
|
||||||
Box(
|
|
||||||
modifier = Modifier.padding(it)
|
|
||||||
) {
|
|
||||||
NavigationController(navController = navController)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
} }
|
||||||
|
) { innerPadding ->
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.padding(innerPadding)
|
||||||
|
) {
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|||||||
Reference in New Issue
Block a user