I’m trying to make an app with a simple profile creation screen that transitions to the next screen, here known as the ‘Welcome’ screen, but there appears to be one error that’s keeping it from doing just that.
Here’s the code for the ‘UserInputScreen’ class:
package com.example.project.ui.screens
import android.widget.Space
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.rememberNavController
import com.example.project.data.UserDataUiEvents
import com.example.project.ui.TextComponent
import com.example.project.ui.TextFieldComponent
import com.example.project.ui.TopBar
import com.example.project.ui.UserInputViewModel
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.viewModelFactory
import com.example.project.R
import com.example.project.ui.ButtonComponent
import com.example.project.ui.ProfileCard
@Composable
fun UserInputScreen(
userInputViewModel: UserInputViewModel,
showWelcomeScreen: (valuesPair: Pair<String,String>) -> Unit) {
Surface(
modifier = Modifier.fillMaxSize()
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(18.dp))
{
TopBar(value = "Hi there uD83DuDE0A")
TextComponent(
textValue = "Let's learn about You!",
textSize = 24.sp
)
Spacer(modifier = Modifier.size(20.dp))
TextComponent(
textValue = "This app will prepare a details page based on input provided by you!",
textSize = 18.sp)
Spacer(modifier = Modifier.size(60.dp))
TextComponent(textValue = "Name", textSize = 18.sp)
TextFieldComponent(onTextChanged = {
userInputViewModel.onEvent(
UserDataUiEvents.UserNameEntered(it)
)
})
Spacer(modifier = Modifier.size(20.dp))
TextComponent(textValue = "What do you like?", textSize = 18.sp)
Row(modifier = Modifier.fillMaxWidth()) {
ProfileCard(image = R.drawable.male_profile, profileSelected = {
userInputViewModel.onEvent(
UserDataUiEvents.ProfileSelected(it)
)
}, selected = userInputViewModel.uiState.value.profileSelected == "Male")
ProfileCard(image = R.drawable.female_profile, profileSelected = {
userInputViewModel.onEvent(
UserDataUiEvents.ProfileSelected(it)
)
},
selected = userInputViewModel.uiState.value.profileSelected == "Female"
)
}
Spacer(modifier = Modifier.weight(1f))
if(userInputViewModel.isValidState()) {
ButtonComponent(
goToDetailsScreen = {
println("==============ComingHere")
println("==============${userInputViewModel.uiState.value.nameEntered} and ${userInputViewModel.uiState.value.profileSelected}")
showWelcomeScreen(
Pair(
userInputViewModel.uiState.value.nameEntered,
userInputViewModel.uiState.value.profileSelected
)
)
}
)
}
}
}
}
@Preview
@Composable
fun UserInputScreenPreview(){
UserInputScreen(UserInputViewModel())
}
Unfortunately, there’s a problem with the UserInputScreenPreview() code right at the end
@Preview
@Composable
fun UserInputScreenPreview(){
UserInputScreen(UserInputViewModel())
}
The issue is with UserInputViewModel(), the error that reads is ‘No value passed for parameter ‘showWelcomeScreen’.’
What should I be writing instead to fix the issue?
Naturally, I tried creating UserInputScreen as a function as follows:
@Preview
@Composable
fun UserInputScreenPreview(){
UserInputScreen(userInputViewModel = UserInputViewModel(),
showWelcomeScreen = {})
}
I was hoping this would fix the issue and allow the app to run and move to the next screen, but instead, the app keeps stopping and disconnects immediately.
How should I go about this line of code to fix the problem?