Skip to content

Jetpack Compose Search AppBar based on Material AppBar

Notifications You must be signed in to change notification settings

gogoadl/compose-search-appbar

Repository files navigation

compose-search-appbar

Jetpack Compose Search AppBar based on Material AppBar

Overview

compose-search-appbar is search Appbar with animation.

녹화_2023_07_10_20_33_00_634

Usage

A simple usage example

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposesearchappbarTheme {
                val focusManager = LocalFocusManager.current
                val title = "title of your app"
                var query by remember { mutableStateOf("") }
                var targetState by remember { mutableStateOf(false) }
                val context = LocalContext.current
                Scaffold(topBar = {
                    SearchAppBar(
                        modifier = Modifier,
                        title = title,
                        query = query,
                        placeholder = "search anything!",
                        targetState = targetState,
                        onSearch = {
                            if (targetState && query.isNotBlank() && query.isNotEmpty()) {
                                Toast.makeText(context, "search start", Toast.LENGTH_SHORT).show()
                                query = ""
                            }
                            targetState = targetState.not()
                        },
                        onQueryChange = { query = it },
                        onQueryDone = {
                            if (targetState && query.isNotBlank() && query.isNotEmpty()) {
                                Toast.makeText(context, "search start", Toast.LENGTH_SHORT).show()
                                query = ""
                            }
                            targetState = targetState.not()
                        },
                        onClose = { query = "" }
                    )
                }
                ) {
                    Column(
                        modifier = Modifier
                            .padding(it)
                            .addFocusCleaner(focusManager = focusManager)
                    ) {
                        Column(modifier = Modifier.fillMaxSize()) {
                            Text(text = "Search App Bar")
                        }
                    }
                }
            }
        }
    }
}

About

Jetpack Compose Search AppBar based on Material AppBar

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages