当try 在Jetpack Compose中从MapsScreen导航到SignupScreen时,虽然我在Logcat中看到注册被点击,但我无法导航到SignupScreen.我已经设置好了导航图和合成图.遗漏了什么?

package com.example.bettehomes.presentation.mapscreens

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.example.bettehomes.presentation.composables.GeoMarkerTopBar
import com.example.bettehomes.navigation.Screens

@ExperimentalMaterial3Api
@Composable
fun MapsScreen(
    snackbarHostState: SnackbarHostState,
    navController: NavController,
    onNavigateToSignupScreen: () -> Unit,
    fetchLocationUpdates: () -> Unit
) {
    Scaffold(
        topBar = { GeoMarkerTopBar() },
        content = { innerPadding ->
            Box(modifier = Modifier.padding(innerPadding)) {
                MapScreenContent(snackbarHostState, fetchLocationUpdates)
                SnackbarHost(
                    hostState = snackbarHostState,
                    modifier = Modifier
                        .wrapContentHeight(Alignment.Bottom)
                        .align(Alignment.BottomCenter)
                )

                Box(modifier=Modifier.fillMaxSize(),contentAlignment = Alignment.TopCenter) {
                    AnnotatedClickableText(onNavigateToSignupScreen)
                }

            }
        },
        floatingActionButton = {
            ExtendedFloatingActionButton(
                modifier = Modifier
                    .padding(16.dp),
                onClick = {
                    navController.navigate(Screens.GeoMarkerScreen.route)
                },
                icon = {
                    Icon(
                        Icons.Filled.Add,
                        contentDescription = "Create"
                    )
                },
                text = { Text("Mark Area") }
            )
        }
    )
}

@Composable
fun AnnotatedClickableText(onNavigateToSignupScreen:() -> Unit) {
    val annotatedText = buildAnnotatedString {
        //append your initial text
        withStyle(
            style = SpanStyle(
                color = Color.Gray,
            )
        ) {
            append("Don't have an account? ")

        }

        //Start of the pushing annotation which you want to color and make them clickable later
        pushStringAnnotation(
            tag = "SignUp",// provide tag which will then be provided when you click the text
            annotation = "SignUp"
        )
        //add text with your different color/style
        withStyle(
            style = SpanStyle(
                color = Color.Red,
            )
        ) {
            append("Sign Up")
        }
        // when pop is called it means the end of annotation with current tag
        pop()
    }
    ClickableText(
        text = annotatedText,
        onClick = { run { onNavigateToSignupScreen }
            annotatedText.getStringAnnotations(
                tag = "SignUp",// tag which you used in the buildAnnotatedString
                start = it,
                end = it
            )[0].let { annotation ->
                //do your stuff when it gets clicked
                Log.d("Clicked", annotation.item)
            }
        },
    )
}

NavHost.kt

package com.example.bettehomes.navigation

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.example.bettehomes.data.home.HomeViewModel
import com.example.bettehomes.data.login.LoginViewModel
import com.example.bettehomes.data.signup.SignupViewModel
import com.example.bettehomes.presentation.GeoMarkerViewModel
import com.example.bettehomes.presentation.authscreens.HomeScreen
import com.example.bettehomes.presentation.authscreens.LoginScreen
import com.example.bettehomes.presentation.authscreens.SignUpScreen
import com.example.bettehomes.presentation.authscreens.TermsAndConditionsScreen
import com.example.bettehomes.presentation.mapscreens.GeoMarkerScreen
import com.example.bettehomes.presentation.mapscreens.MapsScreen

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppNavigation(
    navController: NavHostController,
    snackbarHostState: SnackbarHostState,
    homeViewModel: HomeViewModel,
    loginViewModel: LoginViewModel,
    signUpViewModel:SignupViewModel,
    geoMarkerViewModel: GeoMarkerViewModel,
    fetchLocationUpdates: () -> Unit
) {
    NavHost(navController=navController,startDestination = Screens.MapScreen.route) {
        composable(Screens.MapScreen.route) {
            MapsScreen(
                snackbarHostState = snackbarHostState,
                navController = navController,
                onNavigateToSignupScreen = { navController.navigate(Screens.SignUpScreen.route)},
                fetchLocationUpdates)
            }

        composable(Screens.SignUpScreen.route){
            SignUpScreen(signUpViewModel)
        }
        composable(Screens.LoginScreen.route){
            LoginScreen(loginViewModel)
        }
        composable(Screens.TermsAndConditionsScreen.route){
            TermsAndConditionsScreen()
        }
        composable(Screens.HomeScreen.route){
            HomeScreen(homeViewModel)
        }
        composable(Screens.GeoMarkerScreen.route){
            GeoMarkerScreen(geoMarkerViewModel)
        }


    }
}

推荐答案

当您说onNavigateToSignupScreen时,您只是引用一个变量-它实际上并不做任何事情.

相反,如果您想要调用lambda,则需要使用onNavigateToSignupScreen()(这实际上只是对实际发生的事情的速记-onNavigateToSignupScreen.invoke()).

Android相关问答推荐

如何完全隐藏的元素堆叠在CardView?

当我在Android上运行应用程序时,组件会随机调整大小和移动

如何处理谷歌未经证实的应用程序拒绝?

在Android中使用Room从SQlite数据库中获取实体列表的正确方式是什么?

如何在喷气背包中绕过集装箱

如何制作安卓';s FileProvider在Android 11上使用外部存储+

如何在Jetpack Compose中创建这个圆形?

如何在使用 PointerInput 修改器时添加点击时的波纹效果

弹出导航堆栈后,Compose 无法访问 Hilt View Model

如何在Android Studio中禁用文件中的Github用户名引用?

请求访问小部件中的位置权限

Android AGP 8 + Gradle 8 + Kotlin 1.8 导致 Kapt 出错

通知使用默认语言,屏幕显示多种语言,同时通过 AppCompatDelegate 设置应用程序语言

在 Google Play 中将用户从开放测试转移到生产的过程是怎样的?

在 Kotlin 中循环遍历字符串并用新行替换句号

插入查询室 OnConflictStrategy.REPLACE

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

如何在包含 Jetpack Compose 内容的布局中使用权重

Jetpack Compose Tapjacking:过滤对模糊 UI 的touch

如何将私有 mutableStateOf 分配给 Android Jetpack 中的 State 变量?