refactor: use measure and determine the needInflatable to handler the child width

This commit is contained in:
2023-11-19 02:29:03 +08:00
parent 15e3161d3f
commit 5b5cfd7629
2 changed files with 57 additions and 48 deletions

View File

@@ -152,7 +152,7 @@ data class DropdownMenuStyle(
fun DropdownList( fun DropdownList(
expanded: Boolean, expanded: Boolean,
onExpandedChange: (Boolean) -> Unit, onExpandedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier.width(DefaultDropdownListWidth), modifier: Modifier = Modifier,
colors: DropdownListColors = DropdownList.colors, colors: DropdownListColors = DropdownList.colors,
style: DropdownListStyle = DropdownList.style, style: DropdownListStyle = DropdownList.style,
menuColors: DropdownMenuColors = DropdownMenu.colors, menuColors: DropdownMenuColors = DropdownMenu.colors,
@@ -203,11 +203,14 @@ fun DropdownList(
) { ) {
val menuWidth = maxWidth + startPadding + endPadding val menuWidth = maxWidth + startPadding + endPadding
val menuHeight = with(LocalDensity.current) { menuHeightPx.toDp() } val menuHeight = with(LocalDensity.current) { menuHeightPx.toDp() }
// Note: If minWidth is not 0, a constant width is currently set.
// At this time, the child layout must be completely filled into the parent layout.
val needInflatable = minWidth > 0.dp
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween horizontalArrangement = Arrangement.SpaceBetween
) { ) {
Box(modifier = Modifier.weight(1f)) { text() } Box(modifier = Modifier.weight(1f, needInflatable)) { text() }
Icon( Icon(
modifier = Modifier.graphicsLayer { modifier = Modifier.graphicsLayer {
rotationZ = animatedDirection rotationZ = animatedDirection
@@ -570,8 +573,6 @@ private fun defaultDropdownListInactiveBorder() = BorderStroke(LocalSizes.curren
@ReadOnlyComposable @ReadOnlyComposable
private fun defaultDropdownListActiveBorder() = BorderStroke(LocalSizes.current.borderSizePrimary, LocalColors.current.themePrimary) private fun defaultDropdownListActiveBorder() = BorderStroke(LocalSizes.current.borderSizePrimary, LocalColors.current.themePrimary)
private val DefaultDropdownListWidth = 150.dp
private val DefaultDropdownListMenuOffset = DpOffset((-10).dp, 10.dp) private val DefaultDropdownListMenuOffset = DpOffset((-10).dp, 10.dp)
private val DefaultMenuContentPadding = 16.dp private val DefaultMenuContentPadding = 16.dp

View File

@@ -34,7 +34,9 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsHoveredAsState import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions import androidx.compose.foundation.text.KeyboardActions
@@ -60,6 +62,7 @@ import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
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 androidx.compose.ui.unit.dp
import com.highcapable.flexiui.LocalColors import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.LocalShapes import com.highcapable.flexiui.LocalShapes
import com.highcapable.flexiui.LocalSizes import com.highcapable.flexiui.LocalSizes
@@ -134,7 +137,7 @@ fun TextField(
focused || hovered -> style.borderInactive focused || hovered -> style.borderInactive
else -> style.borderInactive else -> style.borderInactive
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor)) }.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
Row( BoxWithConstraints(
modifier = Modifier.textField( modifier = Modifier.textField(
colors = colors, colors = colors,
style = style, style = style,
@@ -142,52 +145,57 @@ fun TextField(
enabled = enabled, enabled = enabled,
interactionSource = interactionSource, interactionSource = interactionSource,
modifier = modifier modifier = modifier
), )
verticalAlignment = Alignment.CenterVertically
) { ) {
header?.also { // Note: If minWidth is not 0, a constant width is currently set.
InnerDecorationBox( // At this time, the child layout must be completely filled into the parent layout.
decorTint = animatedDecorTint, val needInflatable = minWidth > 0.dp
style = style, fun Modifier.inflatable() = if (needInflatable) fillMaxWidth() else this
header = true, Row(verticalAlignment = Alignment.CenterVertically) {
content = it header?.also {
) InnerDecorationBox(
} decorTint = animatedDecorTint,
Box(modifier = Modifier.weight(1f, fill = false).textFieldPadding(style)) { style = style,
TextFieldStyle(colors) { header = true,
BasicTextField( content = it
value = value, )
onValueChange = onValueChange, }
modifier = Modifier.focusRequester(focusRequester).then(modifier), Box(modifier = Modifier.weight(1f, needInflatable).textFieldPadding(style)) {
enabled = enabled, TextFieldStyle(colors) {
readOnly = readOnly, BasicTextField(
textStyle = textStyle, value = value,
keyboardOptions = keyboardOptions, onValueChange = onValueChange,
keyboardActions = keyboardActions, modifier = Modifier.focusRequester(focusRequester).inflatable(),
singleLine = singleLine, enabled = enabled,
maxLines = maxLines, readOnly = readOnly,
minLines = minLines, textStyle = textStyle,
visualTransformation = visualTransformation, keyboardOptions = keyboardOptions,
onTextLayout = onTextLayout, keyboardActions = keyboardActions,
interactionSource = interactionSource, singleLine = singleLine,
cursorBrush = SolidColor(colors.cursorColor), maxLines = maxLines,
decorationBox = { minLines = minLines,
TextFieldDecorationBox( visualTransformation = visualTransformation,
value = value, onTextLayout = onTextLayout,
placeholder = placeholder, interactionSource = interactionSource,
innerTextField = it cursorBrush = SolidColor(colors.cursorColor),
) decorationBox = {
} TextFieldDecorationBox(
value = value,
placeholder = placeholder,
innerTextField = it
)
}
)
}
}
footer?.also {
InnerDecorationBox(
decorTint = animatedDecorTint,
style = style,
footer = true,
content = it
) )
} }
}
footer?.also {
InnerDecorationBox(
decorTint = animatedDecorTint,
style = style,
footer = true,
content = it
)
} }
} }
} }