提取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,
|
||||||
composable(route = NavigationItem.Street.route) {
|
NavigationItem.Add,
|
||||||
Street()
|
NavigationItem.Message,
|
||||||
}
|
NavigationItem.Profile).forEach { item ->
|
||||||
composable(route = NavigationItem.Add.route) {
|
composable(route = item.route) {
|
||||||
Add()
|
ScaffoldWithNavigationBar(navController) {
|
||||||
}
|
when (item) {
|
||||||
composable(route = NavigationItem.Message.route) {
|
NavigationItem.Home -> Home()
|
||||||
Video()
|
NavigationItem.Street -> Street()
|
||||||
}
|
NavigationItem.Add -> Add()
|
||||||
composable(route = NavigationItem.Profile.route) {
|
NavigationItem.Message -> Video()
|
||||||
Profile()
|
NavigationItem.Profile -> Profile()
|
||||||
|
else -> {} // 由于过滤,这里不会发生
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route = "ProfileTimeline") {
|
composable(route = "ProfileTimeline") {
|
||||||
GalleryPage()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
GalleryPage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="LocationDetail") {
|
composable(route="LocationDetail") {
|
||||||
LocationDetail()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
LocationDetail()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="OfficialPhoto") {
|
composable(route="OfficialPhoto") {
|
||||||
OfficialGalleryPage()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
OfficialGalleryPage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="OfficialPhotographer") {
|
composable(route="OfficialPhotographer") {
|
||||||
OfficialPhotographer()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
OfficialPhotographer()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="Post") {
|
composable(route="Post") {
|
||||||
PostPage()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
PostPage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="ModificationList") {
|
composable(route="ModificationList") {
|
||||||
ModificationListScreen()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
ModificationListScreen()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="MyMessage") {
|
composable(route="MyMessage") {
|
||||||
NotificationsScreen()
|
Box(
|
||||||
|
modifier = Modifier.padding(bottom = navigationBarHeight)
|
||||||
|
) {
|
||||||
|
NotificationsScreen()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="Comments") {
|
composable(route="Comments") {
|
||||||
CommentsScreen()
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
|
CommentsScreen()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="Likes") {
|
composable(route="Likes") {
|
||||||
LikePage()
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
|
LikePage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
composable(route="Followers") {
|
composable(route="Followers") {
|
||||||
FollowerPage()
|
Box(
|
||||||
|
modifier = Modifier.padding(navigationBarHeight)
|
||||||
|
) {
|
||||||
|
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,76 +194,70 @@ fun Navigation() {
|
|||||||
NavigationItem.Message,
|
NavigationItem.Message,
|
||||||
NavigationItem.Profile
|
NavigationItem.Profile
|
||||||
)
|
)
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
Scaffold(
|
||||||
Scaffold(
|
modifier = Modifier.statusBarsPadding(),
|
||||||
modifier = Modifier.statusBarsPadding(),
|
bottomBar = { NavigationBar(
|
||||||
topBar = {},
|
modifier = Modifier.height(56.dp + navigationBarHeight),
|
||||||
|
containerColor = Color.Black
|
||||||
bottomBar = {
|
) {
|
||||||
NavigationBar(
|
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
||||||
modifier = Modifier.height(56.dp + navigationBarHeight),
|
val currentRoute = navBackStackEntry?.destination?.route
|
||||||
containerColor = Color.Black
|
val systemUiController = rememberSystemUiController()
|
||||||
) {
|
item.forEach { it ->
|
||||||
val navBackStackEntry by navController.currentBackStackEntryAsState()
|
NavigationBarItem(
|
||||||
val currentRoute = navBackStackEntry?.destination?.route
|
selected = currentRoute == it.route,
|
||||||
val systemUiController = rememberSystemUiController()
|
onClick = {
|
||||||
item.forEach { it ->
|
if (currentRoute != it.route) {
|
||||||
NavigationBarItem(
|
navController.navigate(it.route)
|
||||||
selected = currentRoute == it.route,
|
}
|
||||||
onClick = {
|
when (it.route) {
|
||||||
if (currentRoute != it.route) {
|
NavigationItem.Add.route -> {
|
||||||
navController.navigate(it.route)
|
systemUiController.setSystemBarsColor(
|
||||||
}
|
color = Color.Black
|
||||||
when (it.route) {
|
|
||||||
NavigationItem.Add.route -> {
|
|
||||||
systemUiController.setSystemBarsColor(
|
|
||||||
color = Color.Black
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
NavigationItem.Message.route -> {
|
|
||||||
systemUiController.setSystemBarsColor(
|
|
||||||
color = Color.Black
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
systemUiController.setSystemBarsColor(
|
|
||||||
color = Color.Transparent
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
colors = NavigationBarItemColors(
|
|
||||||
selectedIconColor = Color.Red,
|
|
||||||
selectedTextColor = Color.Red,
|
|
||||||
selectedIndicatorColor = Color.Black,
|
|
||||||
unselectedIconColor = Color.Red,
|
|
||||||
unselectedTextColor = Color.Red,
|
|
||||||
disabledIconColor = Color.Red,
|
|
||||||
disabledTextColor = Color.Red,
|
|
||||||
|
|
||||||
),
|
|
||||||
icon = {
|
|
||||||
Icon(
|
|
||||||
modifier = Modifier.size(24.dp),
|
|
||||||
imageVector = it.icon(), contentDescription = null,
|
|
||||||
tint = if (currentRoute == it.route) Color.Red else Color.White
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NavigationItem.Message.route -> {
|
||||||
|
systemUiController.setSystemBarsColor(
|
||||||
|
color = Color.Black
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
systemUiController.setSystemBarsColor(
|
||||||
|
color = Color.Transparent
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
colors = NavigationBarItemColors(
|
||||||
|
selectedIconColor = Color.Red,
|
||||||
|
selectedTextColor = Color.Red,
|
||||||
|
selectedIndicatorColor = Color.Black,
|
||||||
|
unselectedIconColor = Color.Red,
|
||||||
|
unselectedTextColor = Color.Red,
|
||||||
|
disabledIconColor = Color.Red,
|
||||||
|
disabledTextColor = Color.Red,
|
||||||
|
|
||||||
|
),
|
||||||
|
icon = {
|
||||||
|
Icon(
|
||||||
|
modifier = Modifier.size(24.dp),
|
||||||
|
imageVector = it.icon(), contentDescription = null,
|
||||||
|
tint = if (currentRoute == it.route) Color.Red else Color.White
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
}
|
|
||||||
) { 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