Created
April 14, 2023 17:02
-
-
Save MohamedGouaouri/7dc0446d31cdb14d851945729a7cd614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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