While navigating to other screens, Jetpack compose navigation in SignUpScreen.kt
fails to navigateTo LoginScreen.kt
. I used sealed class to define the screens and data object to define sub-screens. What seems to be the problem?
package com.example.bettehomes.presentation.authscreens
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.bettehomes.R
import com.example.bettehomes.components.*
import com.example.bettehomes.data.signup.SignupViewModel
import com.example.bettehomes.data.signup.SignupUIEvent
import com.example.bettehomes.navigation.BetteHomesAppRouter
import com.example.bettehomes.navigation.Screens
import kotlinx.coroutines.launch
@Composable
fun SignUpScreen(signupViewModel: SignupViewModel = viewModel()) {
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState()}
Scaffold( snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
){it
//Screen content
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Surface(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
.padding(28.dp)
) {
Column(modifier = Modifier.fillMaxSize()) {
NormalTextComponent(value = stringResource(id = R.string.hello))
HeadingTextComponent(value = stringResource(id = R.string.create_account))
Spacer(modifier = Modifier.height(20.dp))
MyTextFieldComponent(
labelValue = stringResource(id = R.string.first_name),
painterResource(id = R.drawable.profile),
onTextChanged = {
signupViewModel.onEvent(SignupUIEvent.FirstNameChanged(it))
},
errorStatus = signupViewModel.registrationUIState.value.firstNameError
)
MyTextFieldComponent(
labelValue = stringResource(id = R.string.last_name),
painterResource = painterResource(id = R.drawable.profile),
onTextChanged = {
signupViewModel.onEvent(SignupUIEvent.LastNameChanged(it))
},
errorStatus = signupViewModel.registrationUIState.value.lastNameError
)
MyTextFieldComponent(
labelValue = stringResource(id = R.string.email),
painterResource = painterResource(id = R.drawable.message),
onTextChanged = {
signupViewModel.onEvent(SignupUIEvent.EmailChanged(it))
},
errorStatus = signupViewModel.registrationUIState.value.emailError
)
PasswordTextFieldComponent(
labelValue = stringResource(id = R.string.password),
painterResource = painterResource(id = R.drawable.ic_lock),
onTextSelected = {
signupViewModel.onEvent(SignupUIEvent.PasswordChanged(it))
},
errorStatus = signupViewModel.registrationUIState.value.passwordError
)
CheckboxComponent(value = stringResource(id = R.string.terms_and_conditions),
onTextSelected = {
BetteHomesAppRouter.navigateTo(Screens.TermsAndConditionsScreen)
},
onCheckedChange = {
signupViewModel.onEvent(SignupUIEvent.PrivacyPolicyCheckBoxClicked(it))
}
)
Spacer(modifier = Modifier.height(40.dp))
ButtonComponent(
value = stringResource(id = R.string.register),
onButtonClicked = {
signupViewModel.onEvent(SignupUIEvent.RegisterButtonClicked)
if (signupViewModel.userExists.value){
scope.launch { snackbarHostState.showSnackbar("The Email address is already in use!") }
}},
isEnabled = signupViewModel.allValidationsPassed.value
)
Spacer(modifier = Modifier.height(20.dp))
DividerTextComponent()
LoginClickableTextComponent(tryingToLogin = true,onTextSelected = {
BetteHomesAppRouter.navigateTo(Screens.LoginScreen)
})
}
}
if(signupViewModel.signUpInProgress.value) {
CircularProgressIndicator()
}
}
}
}
@Preview
@Composable
fun DefaultPreviewOfSignUpScreen() {
SignUpScreen()
}
AppRouter.kt
package com.example.bettehomes.navigation
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
sealed class Screens(val route: String) {
data object SignUpScreen : Screens("SignUpScreen")
data object TermsAndConditionsScreen : Screens("termsAndConditions")
data object LoginScreen : Screens("loginScreen")
data object HomeScreen : Screens("homeScreen")
data object MapScreen : Screens("mapScreen")
data object GeoMarkerScreen : Screens("geoMarkerScreen")
}
object BetteHomesAppRouter {
private var currentScreen: MutableState<Screens> = mutableStateOf(Screens.MapScreen)
fun navigateTo(destination : Screens){
currentScreen.value = destination
}