Here's my code for a form text field composable:
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FormTextFieldComponent(
topText: String,
initialText: String=""
) = Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
// Small Text at the top (dynamic content)
var textState by remember { mutableStateOf(TextFieldValue(initialText)) }
OutlinedTextField(
value = textState,
onValueChange = {
textState = it
},
label = { Text(topText) }
)
}
However, it shows up only once with this code (only shows the first textfield):
FormTextFieldComponent("First Name", "Van")
Spacer(modifier = Modifier.height(1.dp))
FormTextFieldComponent("Last Name", "Chen")
Spacer(modifier = Modifier.height(1.dp))
What is wrong?
The quick answer is: You are using
fillMaxSize()
, so when the first TextField is render, it fills all the screen, then the second one is rendering outside the screen. So you should usefillMaxWidth()
instead offillMaxSize()
You can prove this using this code, wrapping one of the TextFields and scaling the 0.5 of the full size
Also as recommendation to have more control in the sizes, try to always put a
modifier
parameter to composable functions like this:Is a good practice in order to modify this from the parent.