@@ -9,7 +9,7 @@ import androidx.compose.foundation.gestures.awaitEachGesture
9
9
import androidx.compose.foundation.gestures.awaitFirstDown
10
10
import androidx.compose.foundation.gestures.waitForUpOrCancellation
11
11
import androidx.compose.foundation.interaction.MutableInteractionSource
12
- import androidx.compose.foundation.layout.Box
12
+ import androidx.compose.foundation.interaction.collectIsFocusedAsState
13
13
import androidx.compose.foundation.layout.BoxWithConstraints
14
14
import androidx.compose.foundation.layout.Column
15
15
import androidx.compose.foundation.layout.ColumnScope
@@ -20,7 +20,6 @@ import androidx.compose.foundation.layout.WindowInsets
20
20
import androidx.compose.foundation.layout.asPaddingValues
21
21
import androidx.compose.foundation.layout.calculateEndPadding
22
22
import androidx.compose.foundation.layout.calculateStartPadding
23
- import androidx.compose.foundation.layout.fillMaxHeight
24
23
import androidx.compose.foundation.layout.fillMaxWidth
25
24
import androidx.compose.foundation.layout.height
26
25
import androidx.compose.foundation.layout.offset
@@ -33,19 +32,19 @@ import androidx.compose.foundation.shape.RoundedCornerShape
33
32
import androidx.compose.foundation.text.BasicTextField
34
33
import androidx.compose.foundation.text.KeyboardActions
35
34
import androidx.compose.foundation.text.KeyboardOptions
36
- import androidx.compose.material3.Decoration
37
35
import androidx.compose.material3.Divider
38
36
import androidx.compose.material3.ExperimentalMaterial3Api
39
37
import androidx.compose.material3.LocalTextStyle
40
- import androidx.compose.material3.MaterialTheme
41
38
import androidx.compose.material3.SearchBarColors
42
39
import androidx.compose.material3.SearchBarDefaults
43
- import androidx.compose.material3.Strings
40
+ import androidx.compose.material3.SearchBarDefaults.TonalElevation
44
41
import androidx.compose.material3.Surface
45
42
import androidx.compose.material3.TextFieldColors
43
+ import androidx.compose.material3.TextFieldDefaults
46
44
import androidx.compose.material3.TopAppBarScrollBehavior
47
45
import androidx.compose.material3.contentColorFor
48
- import androidx.compose.material3.getString
46
+ import androidx.compose.material3.internal.Strings
47
+ import androidx.compose.material3.internal.getString
49
48
import androidx.compose.material3.tokens.MotionTokens
50
49
import androidx.compose.runtime.Composable
51
50
import androidx.compose.runtime.SideEffect
@@ -74,6 +73,7 @@ import androidx.compose.ui.semantics.stateDescription
74
73
import androidx.compose.ui.text.TextStyle
75
74
import androidx.compose.ui.text.input.ImeAction
76
75
import androidx.compose.ui.text.input.TextFieldValue
76
+ import androidx.compose.ui.text.input.VisualTransformation
77
77
import androidx.compose.ui.unit.Dp
78
78
import androidx.compose.ui.unit.IntOffset
79
79
import androidx.compose.ui.unit.dp
@@ -99,7 +99,7 @@ fun SearchBar(
99
99
trailingIcon : @Composable (() -> Unit )? = null,
100
100
shape : Shape = SearchBarDefaults .inputFieldShape,
101
101
colors : SearchBarColors = SearchBarDefaults .colors(),
102
- tonalElevation : Dp = SearchBarDefaults . Elevation ,
102
+ tonalElevation : Dp = TonalElevation ,
103
103
windowInsets : WindowInsets = WindowInsets .systemBars,
104
104
interactionSource : MutableInteractionSource = remember { MutableInteractionSource () },
105
105
focusRequester : FocusRequester = remember { FocusRequester () },
@@ -244,10 +244,12 @@ private fun SearchBarInputField(
244
244
interactionSource : MutableInteractionSource = remember { MutableInteractionSource () },
245
245
focusRequester : FocusRequester = remember { FocusRequester () },
246
246
) {
247
+ val focused = interactionSource.collectIsFocusedAsState().value
248
+
247
249
val searchSemantics = getString(Strings .SearchBarSearch )
248
250
val suggestionsAvailableSemantics = getString(Strings .SuggestionsAvailable )
249
251
val textColor = LocalTextStyle .current.color.takeOrElse {
250
- colors.textColor(enabled).value
252
+ colors.textColor(enabled, isError = false , focused = focused)
251
253
}
252
254
253
255
Row (
@@ -294,26 +296,24 @@ private fun SearchBarInputField(
294
296
enabled = enabled,
295
297
singleLine = true ,
296
298
textStyle = LocalTextStyle .current.merge(TextStyle (color = textColor)),
297
- cursorBrush = SolidColor (colors.cursorColor(isError = false ).value ),
299
+ cursorBrush = SolidColor (colors.cursorColor(isError = false )),
298
300
keyboardOptions = KeyboardOptions (imeAction = ImeAction .Search ),
299
301
keyboardActions = KeyboardActions (onSearch = { onSearch(query.text) }),
300
302
interactionSource = interactionSource,
301
- decorationBox = { innerTextField ->
302
- Box (
303
- modifier = Modifier .fillMaxHeight(),
304
- contentAlignment = Alignment .CenterStart
305
- ) {
306
- if (placeholder != null && query.text.isEmpty()) {
307
- Box (Modifier .alpha(0.8f )) {
308
- Decoration (
309
- contentColor = colors.placeholderColor(enabled).value,
310
- typography = MaterialTheme .typography.bodyLarge,
311
- content = placeholder
312
- )
313
- }
314
- }
315
- innerTextField()
316
- }
303
+ decorationBox = @Composable { innerTextField ->
304
+ TextFieldDefaults .DecorationBox (
305
+ value = query.text,
306
+ innerTextField = innerTextField,
307
+ enabled = enabled,
308
+ singleLine = true ,
309
+ visualTransformation = VisualTransformation .None ,
310
+ interactionSource = interactionSource,
311
+ placeholder = placeholder,
312
+ shape = SearchBarDefaults .inputFieldShape,
313
+ colors = colors,
314
+ contentPadding = PaddingValues (),
315
+ container = {},
316
+ )
317
317
}
318
318
)
319
319
0 commit comments