-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: combine TextField with label for better usability with talkback …
…simply by using the label parameter of material TextField component
- Loading branch information
1 parent
6e17309
commit 0ae76ac
Showing
2 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...a/org/scottishtecharmy/soundscape/screens/markers_routes/components/TextFieldWithLabel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.scottishtecharmy.soundscape.screens.markers_routes.components | ||
|
||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.material3.TextFieldDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import org.scottishtecharmy.soundscape.ui.theme.SoundscapeTheme | ||
|
||
@Composable | ||
fun TextFieldWithLabel( | ||
value: String, | ||
onValueChange: (String) -> Unit, | ||
label: String, | ||
modifier: Modifier = Modifier, | ||
textStyle: TextStyle = MaterialTheme.typography.bodyMedium, | ||
shape: Shape = RoundedCornerShape(5.dp), | ||
focusedBgColor: Color = MaterialTheme.colorScheme.onPrimary, // Default colour of white for TextField | ||
unfocusedBgColor: Color = MaterialTheme.colorScheme.onPrimary, // Default colour of white for TextField | ||
focusedTextColor: Color = MaterialTheme.colorScheme.onSecondary, // Default color of black | ||
unfocusedTextColor: Color = MaterialTheme.colorScheme.onSurfaceVariant, // Default color of grey | ||
isSingleLine: Boolean = true // Optional single-line behavior | ||
) { | ||
TextField( | ||
value = value, | ||
onValueChange = onValueChange, | ||
textStyle = textStyle, | ||
shape = shape, | ||
singleLine = isSingleLine, | ||
label = { | ||
Text( | ||
text = label, | ||
style = MaterialTheme.typography.bodyMedium, | ||
) | ||
}, | ||
colors = TextFieldDefaults.colors( | ||
focusedContainerColor = focusedBgColor, | ||
unfocusedContainerColor = unfocusedBgColor, | ||
focusedTextColor = focusedTextColor, | ||
unfocusedTextColor = unfocusedTextColor | ||
), | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun TextFieldWithLabelPreview() { | ||
var textValue by remember { mutableStateOf("Dave") } | ||
SoundscapeTheme { | ||
TextFieldWithLabel( | ||
label = "Name", | ||
value = textValue, | ||
onValueChange = { textValue = it }, | ||
textStyle = TextStyle(fontSize = 18.sp), | ||
shape = RoundedCornerShape(5.dp), | ||
focusedBgColor = MaterialTheme.colorScheme.onPrimary, | ||
unfocusedBgColor = MaterialTheme.colorScheme.onPrimary, | ||
focusedTextColor = MaterialTheme.colorScheme.onSecondary, | ||
unfocusedTextColor = MaterialTheme.colorScheme.onSurfaceVariant, | ||
isSingleLine = true | ||
) | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters