|
| 1 | +package com.example.compose.snippets.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.clickable |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.PaddingValues |
| 8 | +import androidx.compose.foundation.layout.fillMaxSize |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.padding |
| 11 | +import androidx.compose.foundation.lazy.LazyColumn |
| 12 | +import androidx.compose.foundation.verticalScroll |
| 13 | +import androidx.compose.material.icons.Icons |
| 14 | +import androidx.compose.material.icons.filled.MoreVert |
| 15 | +import androidx.compose.material.icons.filled.Search |
| 16 | +import androidx.compose.material.icons.filled.Star |
| 17 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 18 | +import androidx.compose.material3.Icon |
| 19 | +import androidx.compose.material3.ListItem |
| 20 | +import androidx.compose.material3.ListItemDefaults |
| 21 | +import androidx.compose.material3.SearchBar |
| 22 | +import androidx.compose.material3.SearchBarDefaults |
| 23 | +import androidx.compose.material3.Text |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.derivedStateOf |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.mutableStateOf |
| 28 | +import androidx.compose.runtime.remember |
| 29 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 30 | +import androidx.compose.runtime.setValue |
| 31 | +import androidx.compose.ui.Alignment |
| 32 | +import androidx.compose.ui.Modifier |
| 33 | +import androidx.compose.ui.graphics.Color |
| 34 | +import androidx.compose.ui.semantics.isTraversalGroup |
| 35 | +import androidx.compose.ui.semantics.semantics |
| 36 | +import androidx.compose.ui.semantics.traversalIndex |
| 37 | +import androidx.compose.ui.tooling.preview.Preview |
| 38 | +import androidx.compose.ui.unit.dp |
| 39 | +import androidx.compose.foundation.rememberScrollState |
| 40 | + |
| 41 | +@OptIn(ExperimentalMaterial3Api::class) |
| 42 | +// [START android_compose_components_searchbarbasicfilterlist] |
| 43 | +@Composable |
| 44 | +fun SearchBarBasicFilterList(modifier: Modifier = Modifier) { |
| 45 | + var text by rememberSaveable { mutableStateOf("") } |
| 46 | + var expanded by rememberSaveable { mutableStateOf(false) } |
| 47 | + Box( |
| 48 | + modifier |
| 49 | + .fillMaxSize() |
| 50 | + .semantics { isTraversalGroup = true }) |
| 51 | + { |
| 52 | + SearchBar( |
| 53 | + modifier = Modifier |
| 54 | + .align(Alignment.TopCenter) |
| 55 | + .semantics { traversalIndex = 0f }, |
| 56 | + inputField = { |
| 57 | + SearchBarDefaults.InputField( |
| 58 | + query = text, |
| 59 | + onQueryChange = { text = it }, |
| 60 | + onSearch = { expanded = false }, |
| 61 | + expanded = expanded, |
| 62 | + onExpandedChange = { expanded = it }, |
| 63 | + placeholder = { Text("Hinted search text") } |
| 64 | + ) |
| 65 | + }, |
| 66 | + expanded = expanded, |
| 67 | + onExpandedChange = { expanded = it }, |
| 68 | + ) { |
| 69 | + Column(Modifier.verticalScroll(rememberScrollState())) { |
| 70 | + repeat(4) { index -> |
| 71 | + val resultText = "Suggestion $index" |
| 72 | + ListItem( |
| 73 | + headlineContent = { Text(resultText) }, |
| 74 | + supportingContent = { Text("Additional info") }, |
| 75 | + modifier = Modifier |
| 76 | + .clickable { |
| 77 | + text = resultText |
| 78 | + expanded = false |
| 79 | + } |
| 80 | + .fillMaxWidth() |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +// [END android_compose_components_searchbarbasicfilterlist] |
| 88 | + |
| 89 | +@Preview(showBackground = true) |
| 90 | +@Composable |
| 91 | +private fun SearchBarBasicFilterListPreview() { |
| 92 | + SearchBarBasicFilterList() |
| 93 | +} |
| 94 | + |
| 95 | +// [START android_compose_components_searchbarfilterlist] |
| 96 | +@OptIn(ExperimentalMaterial3Api::class) |
| 97 | +@Composable |
| 98 | +fun SearchBarFilterList( |
| 99 | + list: List<String>, |
| 100 | + modifier: Modifier = Modifier |
| 101 | +) { |
| 102 | + var text by rememberSaveable { mutableStateOf("") } |
| 103 | + val filteredList by remember { |
| 104 | + derivedStateOf { |
| 105 | + list.filter { it.lowercase().contains(text.lowercase()) } |
| 106 | + } |
| 107 | + } |
| 108 | + var expanded by rememberSaveable { mutableStateOf(false) } |
| 109 | + |
| 110 | + Box( |
| 111 | + modifier |
| 112 | + .fillMaxSize() |
| 113 | + .semantics { isTraversalGroup = true }) { |
| 114 | + SearchBar( |
| 115 | + modifier = Modifier |
| 116 | + .align(Alignment.TopCenter) |
| 117 | + .semantics { traversalIndex = 0f }, |
| 118 | + inputField = { |
| 119 | + SearchBarDefaults.InputField( |
| 120 | + query = text, |
| 121 | + onQueryChange = { text = it }, |
| 122 | + onSearch = { expanded = false }, |
| 123 | + expanded = expanded, |
| 124 | + onExpandedChange = { expanded = it }, |
| 125 | + placeholder = { Text("Hinted search text") }, |
| 126 | + leadingIcon = { Icon(Icons.Default.Search, contentDescription = null) }, |
| 127 | + trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = null) }, |
| 128 | + ) |
| 129 | + }, |
| 130 | + expanded = expanded, |
| 131 | + onExpandedChange = { expanded = it }, |
| 132 | + ) { |
| 133 | + LazyColumn { |
| 134 | + items(count = filteredList.size) { index -> |
| 135 | + val resultText = filteredList[index] |
| 136 | + ListItem( |
| 137 | + headlineContent = { Text(resultText) }, |
| 138 | + supportingContent = { Text("Additional info") }, |
| 139 | + leadingContent = { |
| 140 | + Icon( |
| 141 | + Icons.Filled.Star, |
| 142 | + contentDescription = null |
| 143 | + ) |
| 144 | + }, |
| 145 | + colors = ListItemDefaults.colors(containerColor = Color.Transparent), |
| 146 | + modifier = Modifier |
| 147 | + .clickable { |
| 148 | + text = resultText |
| 149 | + expanded = false |
| 150 | + } |
| 151 | + .fillMaxWidth() |
| 152 | + .padding(horizontal = 16.dp, vertical = 4.dp) |
| 153 | + ) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + LazyColumn( |
| 158 | + contentPadding = PaddingValues( |
| 159 | + start = 16.dp, |
| 160 | + top = 72.dp, |
| 161 | + end = 16.dp, |
| 162 | + bottom = 16.dp |
| 163 | + ), |
| 164 | + verticalArrangement = Arrangement.spacedBy(8.dp), |
| 165 | + modifier = Modifier.semantics { |
| 166 | + traversalIndex = 1f |
| 167 | + }, |
| 168 | + ) { |
| 169 | + items(count = filteredList.size) { |
| 170 | + Text(text = filteredList[it]) |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | +// [END android_compose_components_searchbarfilterlist] |
| 176 | + |
| 177 | +@Preview(showBackground = true) |
| 178 | +@Composable |
| 179 | +fun AppSearchBar(modifier: Modifier = Modifier) { |
| 180 | + SearchBarFilterList( |
| 181 | + list = listOf( |
| 182 | + "Cupcake", |
| 183 | + "Donut", |
| 184 | + "Eclair", |
| 185 | + "Froyo", |
| 186 | + "Gingerbread", |
| 187 | + "Honeycomb", |
| 188 | + "Ice Cream Sandwich", |
| 189 | + "Jelly Bean", |
| 190 | + "KitKat", |
| 191 | + "Lollipop", |
| 192 | + "Marshmallow", |
| 193 | + "Nougat", |
| 194 | + "Oreo", |
| 195 | + "Pie" |
| 196 | + ), |
| 197 | + modifier |
| 198 | + ) |
| 199 | +} |
| 200 | + |
0 commit comments