mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-08 11:34:18 +08:00
refactor: merge text field style
This commit is contained in:
@@ -47,6 +47,7 @@ import androidx.compose.runtime.Immutable
|
|||||||
import androidx.compose.runtime.ReadOnlyComposable
|
import androidx.compose.runtime.ReadOnlyComposable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.alpha
|
import androidx.compose.ui.draw.alpha
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
@@ -55,7 +56,6 @@ import androidx.compose.ui.graphics.Shape
|
|||||||
import androidx.compose.ui.graphics.SolidColor
|
import androidx.compose.ui.graphics.SolidColor
|
||||||
import androidx.compose.ui.text.TextLayoutResult
|
import androidx.compose.ui.text.TextLayoutResult
|
||||||
import androidx.compose.ui.text.TextStyle
|
import androidx.compose.ui.text.TextStyle
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
|
||||||
import androidx.compose.ui.text.input.VisualTransformation
|
import androidx.compose.ui.text.input.VisualTransformation
|
||||||
import androidx.compose.ui.unit.Dp
|
import androidx.compose.ui.unit.Dp
|
||||||
import com.highcapable.flexiui.LocalColors
|
import com.highcapable.flexiui.LocalColors
|
||||||
@@ -106,126 +106,13 @@ fun TextField(
|
|||||||
visualTransformation: VisualTransformation = VisualTransformation.None,
|
visualTransformation: VisualTransformation = VisualTransformation.None,
|
||||||
onTextLayout: (TextLayoutResult) -> Unit = {},
|
onTextLayout: (TextLayoutResult) -> Unit = {},
|
||||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||||
header: @Composable () -> Unit = {},
|
header: @Composable (() -> Unit)? = null,
|
||||||
placeholder: @Composable () -> Unit = {},
|
placeholder: @Composable () -> Unit = {},
|
||||||
footer: @Composable () -> Unit = {},
|
footer: @Composable (() -> Unit)? = null,
|
||||||
textStyle: TextStyle = TextField.textStyle
|
textStyle: TextStyle = TextField.textStyle
|
||||||
) {
|
|
||||||
TextFieldStyle(colors) {
|
|
||||||
BasicTextField(
|
|
||||||
value = value,
|
|
||||||
onValueChange = onValueChange,
|
|
||||||
modifier = modifier,
|
|
||||||
enabled = enabled,
|
|
||||||
readOnly = readOnly,
|
|
||||||
textStyle = textStyle,
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
singleLine = singleLine,
|
|
||||||
maxLines = maxLines,
|
|
||||||
minLines = minLines,
|
|
||||||
visualTransformation = visualTransformation,
|
|
||||||
onTextLayout = onTextLayout,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
cursorBrush = SolidColor(colors.cursorColor),
|
|
||||||
decorationBox = {
|
|
||||||
TextFieldDecorationBox(
|
|
||||||
value = value,
|
|
||||||
modifier = modifier,
|
|
||||||
colors = colors,
|
|
||||||
style = style,
|
|
||||||
enabled = enabled,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
header = header,
|
|
||||||
placeholder = placeholder,
|
|
||||||
footer = footer,
|
|
||||||
innerTextField = it
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun TextField(
|
|
||||||
value: TextFieldValue,
|
|
||||||
onValueChange: (TextFieldValue) -> Unit,
|
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
colors: TextFieldColors = TextField.colors,
|
|
||||||
style: TextFieldStyle = TextField.style,
|
|
||||||
enabled: Boolean = true,
|
|
||||||
readOnly: Boolean = false,
|
|
||||||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
|
|
||||||
keyboardActions: KeyboardActions = KeyboardActions.Default,
|
|
||||||
singleLine: Boolean = false,
|
|
||||||
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
|
|
||||||
minLines: Int = 1,
|
|
||||||
visualTransformation: VisualTransformation = VisualTransformation.None,
|
|
||||||
onTextLayout: (TextLayoutResult) -> Unit = {},
|
|
||||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
|
||||||
header: @Composable () -> Unit = {},
|
|
||||||
placeholder: @Composable () -> Unit = {},
|
|
||||||
footer: @Composable () -> Unit = {},
|
|
||||||
textStyle: TextStyle = TextField.textStyle
|
|
||||||
) {
|
|
||||||
TextFieldStyle(colors) {
|
|
||||||
BasicTextField(
|
|
||||||
value = value,
|
|
||||||
onValueChange = onValueChange,
|
|
||||||
modifier = modifier,
|
|
||||||
enabled = enabled,
|
|
||||||
readOnly = readOnly,
|
|
||||||
textStyle = textStyle,
|
|
||||||
keyboardOptions = keyboardOptions,
|
|
||||||
keyboardActions = keyboardActions,
|
|
||||||
singleLine = singleLine,
|
|
||||||
maxLines = maxLines,
|
|
||||||
minLines = minLines,
|
|
||||||
visualTransformation = visualTransformation,
|
|
||||||
onTextLayout = onTextLayout,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
cursorBrush = SolidColor(colors.cursorColor),
|
|
||||||
decorationBox = {
|
|
||||||
TextFieldDecorationBox(
|
|
||||||
value = value.text,
|
|
||||||
modifier = modifier,
|
|
||||||
colors = colors,
|
|
||||||
style = style,
|
|
||||||
enabled = enabled,
|
|
||||||
interactionSource = interactionSource,
|
|
||||||
header = header,
|
|
||||||
placeholder = placeholder,
|
|
||||||
footer = footer,
|
|
||||||
innerTextField = it
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
private fun TextFieldStyle(colors: TextFieldColors, content: @Composable () -> Unit) {
|
|
||||||
CompositionLocalProvider(LocalTextSelectionColors provides colors.selectionColors) {
|
|
||||||
content()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
private fun TextFieldDecorationBox(
|
|
||||||
value: String,
|
|
||||||
modifier: Modifier,
|
|
||||||
colors: TextFieldColors,
|
|
||||||
style: TextFieldStyle,
|
|
||||||
enabled: Boolean,
|
|
||||||
interactionSource: MutableInteractionSource,
|
|
||||||
header: @Composable () -> Unit,
|
|
||||||
placeholder: @Composable () -> Unit,
|
|
||||||
footer: @Composable () -> Unit,
|
|
||||||
innerTextField: @Composable () -> Unit
|
|
||||||
) {
|
) {
|
||||||
val focused by interactionSource.collectIsFocusedAsState()
|
val focused by interactionSource.collectIsFocusedAsState()
|
||||||
val hovered by interactionSource.collectIsHoveredAsState()
|
val hovered by interactionSource.collectIsHoveredAsState()
|
||||||
val animatedPlaceholder by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f)
|
|
||||||
val animatedBorderColor by animateColorAsState(when {
|
val animatedBorderColor by animateColorAsState(when {
|
||||||
focused || hovered -> style.borderActive.solidColor
|
focused || hovered -> style.borderActive.solidColor
|
||||||
else -> style.borderInactive.solidColor
|
else -> style.borderInactive.solidColor
|
||||||
@@ -238,7 +125,7 @@ private fun TextFieldDecorationBox(
|
|||||||
focused || hovered -> style.borderInactive
|
focused || hovered -> style.borderInactive
|
||||||
else -> style.borderInactive
|
else -> style.borderInactive
|
||||||
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
|
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
|
||||||
Box(
|
Row(
|
||||||
modifier = Modifier.textField(
|
modifier = Modifier.textField(
|
||||||
colors = colors,
|
colors = colors,
|
||||||
style = style,
|
style = style,
|
||||||
@@ -246,20 +133,61 @@ private fun TextFieldDecorationBox(
|
|||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
)
|
),
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
) {
|
) {
|
||||||
Row {
|
header?.invoke()
|
||||||
header()
|
Box(modifier = Modifier.weight(1f, fill = false)) {
|
||||||
Box {
|
TextFieldStyle(colors) {
|
||||||
Box(modifier = Modifier.alpha(animatedPlaceholder)) {
|
BasicTextField(
|
||||||
CompositionLocalProvider(
|
value = value,
|
||||||
LocalTextStyle provides LocalTextStyle.current.default(LocalColors.current.textSecondary)
|
onValueChange = onValueChange,
|
||||||
) { placeholder() }
|
modifier = modifier,
|
||||||
}
|
enabled = enabled,
|
||||||
innerTextField()
|
readOnly = readOnly,
|
||||||
|
textStyle = textStyle,
|
||||||
|
keyboardOptions = keyboardOptions,
|
||||||
|
keyboardActions = keyboardActions,
|
||||||
|
singleLine = singleLine,
|
||||||
|
maxLines = maxLines,
|
||||||
|
minLines = minLines,
|
||||||
|
visualTransformation = visualTransformation,
|
||||||
|
onTextLayout = onTextLayout,
|
||||||
|
interactionSource = interactionSource,
|
||||||
|
cursorBrush = SolidColor(colors.cursorColor),
|
||||||
|
decorationBox = {
|
||||||
|
TextFieldDecorationBox(
|
||||||
|
value = value,
|
||||||
|
placeholder = placeholder,
|
||||||
|
innerTextField = it
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
footer()
|
|
||||||
}
|
}
|
||||||
|
footer?.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun TextFieldDecorationBox(
|
||||||
|
value: String,
|
||||||
|
placeholder: @Composable () -> Unit,
|
||||||
|
innerTextField: @Composable () -> Unit
|
||||||
|
) {
|
||||||
|
val animatedPlaceholder by animateFloatAsState(if (value.isNotEmpty()) 0f else 1f)
|
||||||
|
Box(modifier = Modifier.alpha(animatedPlaceholder)) {
|
||||||
|
CompositionLocalProvider(
|
||||||
|
LocalTextStyle provides LocalTextStyle.current.default(LocalColors.current.textSecondary)
|
||||||
|
) { placeholder() }
|
||||||
|
}
|
||||||
|
innerTextField()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun TextFieldStyle(colors: TextFieldColors, content: @Composable () -> Unit) {
|
||||||
|
CompositionLocalProvider(LocalTextSelectionColors provides colors.selectionColors) {
|
||||||
|
content()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user