Forgive if I ask something really stupid now but I'm quite new to compose. I'm trying to create a BasicTextField which looks like I want it to but for some reason there is no cursor blinking when I've selected the TextField and the keyboard is showing. I saw it with a normal TextField but I didn't like the looks of the default things. Am I missing a line or something? Thanks
fun CustomSearchField(state: MutableState<TextFieldValue>) {
val focusManager = LocalFocusManager.current
val keyboardController = LocalSoftwareKeyboardController.current
BasicTextField(
value = state.value,
onValueChange = { value ->
state.value = value
},
singleLine = true,
cursorBrush = SolidColor(Color.White),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
focusManager.clearFocus()
}
),
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
Text(
text = state.value.text,
style = poppinsRegular(14),
color = colorResource(id = R.color.primaryTextColor),
modifier = Modifier
.weight(1f)
)
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
There are two issues:
cursorBrush = SolidColor(Color.Black),
decorationBox takes an argument with the text box and text controls. At the decorationBox function, you need to add innerTextField -> to make that the argument name. Then, inside the row, you need to call the composable.
decorationBox = {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
.heightIn(48.dp)
.clip(shape = RoundedCornerShape(8.dp))
.border(
BorderStroke(
1.dp, colorResource(R.color.cardBorder)
), shape = RoundedCornerShape(8.dp)
)
) {
Image(
painter = painterResource(id = R.drawable.ic_search_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(if (state.value == TextFieldValue("")) R.color.mainGray else R.color.primaryTextColor)),
modifier = Modifier
.padding(start = 16.dp)
.size(24.dp)
)
if (state.value == TextFieldValue("")) {
/*Text(
text = "Search",
style = poppinsRegular(14),
color = colorResource(id = R.color.mainGray),
modifier = Modifier
.weight(1f)
)*/
} else {
innerTextField() // <--- HERE
}
if (state.value != TextFieldValue("")) {
IconButton(
onClick = {
state.value =
TextFieldValue("")
}
) {
Image(
painter = painterResource(id = R.drawable.clear_icon),
contentDescription = null,
contentScale = ContentScale.Crop,
colorFilter = ColorFilter.tint(colorResource(R.color.primaryTextColor)),
modifier = Modifier
.size(24.dp)
.padding(2.dp)
)
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With