Skip to content

Instantly share code, notes, and snippets.

@MohamedGouaouri
Created April 14, 2023 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedGouaouri/7dc0446d31cdb14d851945729a7cd614 to your computer and use it in GitHub Desktop.
Save MohamedGouaouri/7dc0446d31cdb14d851945729a7cd614 to your computer and use it in GitHub Desktop.
@Composable
fun RestaurantSearchBar(
modifier: Modifier = Modifier,
hint: String = "Search",
onSearch: (String) -> Unit = {}
) {
// 1. text field state
var text by remember {
mutableStateOf("")
}
// 2. debouncing
text.useDebounce{
onSearch(it)
}
var isHintDisplayed by remember {
mutableStateOf(hint != "")
}
Box(modifier = modifier) {
BasicTextField(
value = text,
onValueChange = {
text = it
},
maxLines = 1,
singleLine = true,
textStyle = TextStyle(color = Color.Black),
modifier = Modifier
.fillMaxWidth()
.shadow(5.dp, CircleShape)
.background(Color.White, CircleShape)
.padding(horizontal = 20.dp, vertical = 12.dp)
.onFocusChanged {
// This will show the hint when unfocused
isHintDisplayed = !it.isFocused
}
)
if(isHintDisplayed) {
Text(
text = hint,
color = Color.LightGray,
modifier = Modifier
.padding(horizontal = 20.dp, vertical = 12.dp),
fontSize = 12.sp
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment