Skip to content

Commit 0b8cf78

Browse files
committed
Add show on click to the advanced example
1 parent a7117c0 commit 0b8cf78

File tree

3 files changed

+107
-17
lines changed

3 files changed

+107
-17
lines changed

.idea/codeStyles/Project.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.kt

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ import androidx.compose.foundation.layout.fillMaxWidth
2626
import androidx.compose.foundation.layout.padding
2727
import androidx.compose.foundation.lazy.LazyColumn
2828
import androidx.compose.foundation.rememberScrollState
29+
import androidx.compose.foundation.text.input.rememberTextFieldState
30+
import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
2931
import androidx.compose.foundation.verticalScroll
3032
import androidx.compose.material.icons.Icons
3133
import androidx.compose.material.icons.filled.MoreVert
3234
import androidx.compose.material.icons.filled.Search
3335
import androidx.compose.material.icons.filled.Star
3436
import androidx.compose.material3.Button
37+
import androidx.compose.material3.DockedSearchBar
3538
import androidx.compose.material3.ExperimentalMaterial3Api
3639
import androidx.compose.material3.Icon
3740
import androidx.compose.material3.ListItem
@@ -68,12 +71,16 @@ fun SearchBarExamples() {
6871
var currentExample by remember { mutableStateOf<String?>(null) }
6972

7073
when (currentExample) {
71-
"basic" -> SearchBarBasicFilterList()
72-
"advanced" -> AppSearchBar()
74+
"basic" -> SearchBarBasicExample()
75+
"docked" -> DockedSearchBarExample()
76+
"advanced" -> SearchBarFilterListPreview()
7377
else -> {
7478
Button(onClick = { currentExample = "basic" }) {
7579
Text("Basic search bar with filter")
7680
}
81+
Button(onClick = { currentExample = "docked" }) {
82+
Text("Basic docked search bar with filter")
83+
}
7784
Button(onClick = { currentExample = "advanced" }) {
7885
Text("Advanced search bar with filter")
7986
}
@@ -83,11 +90,12 @@ fun SearchBarExamples() {
8390
}
8491

8592
@OptIn(ExperimentalMaterial3Api::class)
86-
// [START android_compose_components_searchbarbasicfilterlist]
93+
// [START android_compose_components_searchbarbasicexample]
8794
@Composable
88-
fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
95+
fun SearchBarBasicExample(modifier: Modifier = Modifier) {
8996
var text by rememberSaveable { mutableStateOf("") }
9097
var expanded by rememberSaveable { mutableStateOf(false) }
98+
9199
Box(
92100
modifier
93101
.fillMaxSize()
@@ -104,12 +112,15 @@ fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
104112
onSearch = { expanded = false },
105113
expanded = expanded,
106114
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) },
108118
)
109119
},
110120
expanded = expanded,
111121
onExpandedChange = { expanded = it },
112122
) {
123+
// Dummy suggestions
113124
Column(Modifier.verticalScroll(rememberScrollState())) {
114125
repeat(4) { index ->
115126
val resultText = "Suggestion $index"
@@ -128,18 +139,18 @@ fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
128139
}
129140
}
130141
}
131-
// [END android_compose_components_searchbarbasicfilterlist]
142+
// [END android_compose_components_searchbarbasicexample]
132143

133144
@Preview(showBackground = true)
134145
@Composable
135-
private fun SearchBarBasicFilterListPreview() {
136-
SearchBarBasicFilterList()
146+
private fun SearchBarBasicExamplePreview() {
147+
SearchBarBasicExample()
137148
}
138149

139-
// [START android_compose_components_searchbarfilterlist]
150+
// [START android_compose_components_searchbarfilterlistexample]
140151
@OptIn(ExperimentalMaterial3Api::class)
141152
@Composable
142-
fun SearchBarFilterList(
153+
fun SearchBarFilterListExample(
143154
list: List<String>,
144155
modifier: Modifier = Modifier
145156
) {
@@ -217,12 +228,72 @@ fun SearchBarFilterList(
217228
}
218229
}
219230
}
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+
221292

222293
@Preview(showBackground = true)
223294
@Composable
224-
fun AppSearchBar(modifier: Modifier = Modifier) {
225-
SearchBarFilterList(
295+
fun SearchBarFilterListPreview(modifier: Modifier = Modifier) {
296+
SearchBarFilterListExample(
226297
list = listOf(
227298
"Cupcake",
228299
"Donut",

compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ import androidx.compose.material3.TooltipBox
3636
import androidx.compose.material3.TooltipDefaults
3737
import androidx.compose.material3.rememberTooltipState
3838
import androidx.compose.runtime.Composable
39-
import androidx.compose.runtime.getValue
40-
import androidx.compose.runtime.setValue
39+
import androidx.compose.runtime.rememberCoroutineScope
4140
import androidx.compose.ui.Alignment
4241
import androidx.compose.ui.Modifier
4342
import androidx.compose.ui.text.style.TextAlign
4443
import androidx.compose.ui.tooling.preview.Preview
4544
import androidx.compose.ui.unit.DpSize
4645
import androidx.compose.ui.unit.dp
46+
import kotlinx.coroutines.launch
4747

4848
@Composable
4949
fun TooltipExamples() {
@@ -147,6 +147,7 @@ fun AdvancedRichTooltipExample(
147147
richTooltipActionText: String = "Dismiss"
148148
) {
149149
val tooltipState = rememberTooltipState()
150+
val coroutineScope = rememberCoroutineScope() // Key for launching coroutines
150151

151152
TooltipBox(
152153
modifier = modifier,
@@ -156,7 +157,11 @@ fun AdvancedRichTooltipExample(
156157
title = { Text(richTooltipSubheadText) },
157158
action = {
158159
Row {
159-
TextButton(onClick = { tooltipState.dismiss() }) {
160+
TextButton(onClick = {
161+
coroutineScope.launch { // Use a coroutine to dismiss
162+
tooltipState.dismiss()
163+
}
164+
}) {
160165
Text(richTooltipActionText)
161166
}
162167
}
@@ -168,7 +173,11 @@ fun AdvancedRichTooltipExample(
168173
},
169174
state = tooltipState
170175
) {
171-
IconButton(onClick = { tooltipState.dismiss() }) {
176+
IconButton(onClick = {
177+
coroutineScope.launch { // Launch a coroutine to show the tooltip
178+
tooltipState.show()
179+
}
180+
}) {
172181
Icon(
173182
imageVector = Icons.Filled.Camera,
174183
contentDescription = "Open camera"

0 commit comments

Comments
 (0)