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,9 +145,13 @@ fun TextField(
enabled = enabled, enabled = enabled,
interactionSource = interactionSource, interactionSource = interactionSource,
modifier = modifier modifier = modifier
), )
verticalAlignment = Alignment.CenterVertically
) { ) {
// 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
fun Modifier.inflatable() = if (needInflatable) fillMaxWidth() else this
Row(verticalAlignment = Alignment.CenterVertically) {
header?.also { header?.also {
InnerDecorationBox( InnerDecorationBox(
decorTint = animatedDecorTint, decorTint = animatedDecorTint,
@@ -153,12 +160,12 @@ fun TextField(
content = it content = it
) )
} }
Box(modifier = Modifier.weight(1f, fill = false).textFieldPadding(style)) { Box(modifier = Modifier.weight(1f, needInflatable).textFieldPadding(style)) {
TextFieldStyle(colors) { TextFieldStyle(colors) {
BasicTextField( BasicTextField(
value = value, value = value,
onValueChange = onValueChange, onValueChange = onValueChange,
modifier = Modifier.focusRequester(focusRequester).then(modifier), modifier = Modifier.focusRequester(focusRequester).inflatable(),
enabled = enabled, enabled = enabled,
readOnly = readOnly, readOnly = readOnly,
textStyle = textStyle, textStyle = textStyle,
@@ -190,6 +197,7 @@ fun TextField(
) )
} }
} }
}
} }
@Composable @Composable