@@ -26,12 +26,15 @@ import androidx.compose.foundation.layout.fillMaxWidth
26
26
import androidx.compose.foundation.layout.padding
27
27
import androidx.compose.foundation.lazy.LazyColumn
28
28
import androidx.compose.foundation.rememberScrollState
29
+ import androidx.compose.foundation.text.input.rememberTextFieldState
30
+ import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
29
31
import androidx.compose.foundation.verticalScroll
30
32
import androidx.compose.material.icons.Icons
31
33
import androidx.compose.material.icons.filled.MoreVert
32
34
import androidx.compose.material.icons.filled.Search
33
35
import androidx.compose.material.icons.filled.Star
34
36
import androidx.compose.material3.Button
37
+ import androidx.compose.material3.DockedSearchBar
35
38
import androidx.compose.material3.ExperimentalMaterial3Api
36
39
import androidx.compose.material3.Icon
37
40
import androidx.compose.material3.ListItem
@@ -68,12 +71,16 @@ fun SearchBarExamples() {
68
71
var currentExample by remember { mutableStateOf<String ?>(null ) }
69
72
70
73
when (currentExample) {
71
- " basic" -> SearchBarBasicFilterList ()
72
- " advanced" -> AppSearchBar ()
74
+ " basic" -> SearchBarBasicExample ()
75
+ " docked" -> DockedSearchBarExample ()
76
+ " advanced" -> SearchBarFilterListPreview ()
73
77
else -> {
74
78
Button (onClick = { currentExample = " basic" }) {
75
79
Text (" Basic search bar with filter" )
76
80
}
81
+ Button (onClick = { currentExample = " docked" }) {
82
+ Text (" Basic docked search bar with filter" )
83
+ }
77
84
Button (onClick = { currentExample = " advanced" }) {
78
85
Text (" Advanced search bar with filter" )
79
86
}
@@ -83,11 +90,12 @@ fun SearchBarExamples() {
83
90
}
84
91
85
92
@OptIn(ExperimentalMaterial3Api ::class )
86
- // [START android_compose_components_searchbarbasicfilterlist ]
93
+ // [START android_compose_components_searchbarbasicexample ]
87
94
@Composable
88
- fun SearchBarBasicFilterList (modifier : Modifier = Modifier ) {
95
+ fun SearchBarBasicExample (modifier : Modifier = Modifier ) {
89
96
var text by rememberSaveable { mutableStateOf(" " ) }
90
97
var expanded by rememberSaveable { mutableStateOf(false ) }
98
+
91
99
Box (
92
100
modifier
93
101
.fillMaxSize()
@@ -104,12 +112,15 @@ fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
104
112
onSearch = { expanded = false },
105
113
expanded = expanded,
106
114
onExpandedChange = { expanded = it },
107
- placeholder = { Text (" Hinted search text" ) }
115
+ placeholder = { Text (" Hinted search text" ) },
116
+ leadingIcon = { Icon (Icons .Default .Search , contentDescription = null ) },
117
+ trailingIcon = { Icon (Icons .Default .MoreVert , contentDescription = null ) },
108
118
)
109
119
},
110
120
expanded = expanded,
111
121
onExpandedChange = { expanded = it },
112
122
) {
123
+ // Dummy suggestions
113
124
Column (Modifier .verticalScroll(rememberScrollState())) {
114
125
repeat(4 ) { index ->
115
126
val resultText = " Suggestion $index "
@@ -128,18 +139,18 @@ fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
128
139
}
129
140
}
130
141
}
131
- // [END android_compose_components_searchbarbasicfilterlist ]
142
+ // [END android_compose_components_searchbarbasicexample ]
132
143
133
144
@Preview(showBackground = true )
134
145
@Composable
135
- private fun SearchBarBasicFilterListPreview () {
136
- SearchBarBasicFilterList ()
146
+ private fun SearchBarBasicExamplePreview () {
147
+ SearchBarBasicExample ()
137
148
}
138
149
139
- // [START android_compose_components_searchbarfilterlist ]
150
+ // [START android_compose_components_searchbarfilterlistexample ]
140
151
@OptIn(ExperimentalMaterial3Api ::class )
141
152
@Composable
142
- fun SearchBarFilterList (
153
+ fun SearchBarFilterListExample (
143
154
list : List <String >,
144
155
modifier : Modifier = Modifier
145
156
) {
@@ -217,12 +228,72 @@ fun SearchBarFilterList(
217
228
}
218
229
}
219
230
}
220
- // [END android_compose_components_searchbarfilterlist]
231
+ // [END android_compose_components_searchbarfilterlistexample]
232
+
233
+ @OptIn(ExperimentalMaterial3Api ::class )
234
+ // This example is the same as SearchBarExample, but with DockedSearchBar.
235
+ // [START android_compose_components_dockedsearchbarexample]
236
+ @Composable
237
+ fun DockedSearchBarExample (modifier : Modifier = Modifier ){
238
+ var text by rememberSaveable { mutableStateOf(" " ) }
239
+ var expanded by rememberSaveable { mutableStateOf(false ) }
240
+
241
+ Box (
242
+ modifier
243
+ .fillMaxSize()
244
+ .semantics { isTraversalGroup = true }
245
+ ) {
246
+ DockedSearchBar (
247
+ modifier = Modifier
248
+ .align(Alignment .TopCenter )
249
+ .semantics { traversalIndex = 0f },
250
+ inputField = {
251
+ SearchBarDefaults .InputField (
252
+ query = text,
253
+ onQueryChange = { text = it },
254
+ onSearch = { expanded = false },
255
+ expanded = expanded,
256
+ onExpandedChange = { expanded = it },
257
+ placeholder = { Text (" Hinted search text" ) },
258
+ leadingIcon = { Icon (Icons .Default .Search , contentDescription = null ) },
259
+ trailingIcon = { Icon (Icons .Default .MoreVert , contentDescription = null ) },
260
+ )
261
+ },
262
+ expanded = expanded,
263
+ onExpandedChange = { expanded = it },
264
+ ) {
265
+ // Dummy suggestions
266
+ Column (Modifier .verticalScroll(rememberScrollState())) {
267
+ repeat(4 ) { index ->
268
+ val resultText = " Suggestion $index "
269
+ ListItem (
270
+ headlineContent = { Text (resultText) },
271
+ supportingContent = { Text (" Additional info" ) },
272
+ modifier = Modifier
273
+ .clickable {
274
+ text = resultText
275
+ expanded = false
276
+ }
277
+ .fillMaxWidth()
278
+ )
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
284
+ // [END android_compose_components_dockedsearchbarexample]
285
+
286
+ @Preview(showBackground = true )
287
+ @Composable
288
+ fun DockedSearchBarExamplePreview (modifier : Modifier = Modifier ) {
289
+ DockedSearchBarExample ()
290
+ }
291
+
221
292
222
293
@Preview(showBackground = true )
223
294
@Composable
224
- fun AppSearchBar (modifier : Modifier = Modifier ) {
225
- SearchBarFilterList (
295
+ fun SearchBarFilterListPreview (modifier : Modifier = Modifier ) {
296
+ SearchBarFilterListExample (
226
297
list = listOf (
227
298
" Cupcake" ,
228
299
" Donut" ,
0 commit comments