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(
expanded: Boolean,
onExpandedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier.width(DefaultDropdownListWidth),
modifier: Modifier = Modifier,
colors: DropdownListColors = DropdownList.colors,
style: DropdownListStyle = DropdownList.style,
menuColors: DropdownMenuColors = DropdownMenu.colors,
@@ -203,11 +203,14 @@ fun DropdownList(
) {
val menuWidth = maxWidth + startPadding + endPadding
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(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Box(modifier = Modifier.weight(1f)) { text() }
Box(modifier = Modifier.weight(1f, needInflatable)) { text() }
Icon(
modifier = Modifier.graphicsLayer {
rotationZ = animatedDirection
@@ -570,8 +573,6 @@ private fun defaultDropdownListInactiveBorder() = BorderStroke(LocalSizes.curren
@ReadOnlyComposable
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 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.collectIsHoveredAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
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.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.highcapable.flexiui.LocalColors
import com.highcapable.flexiui.LocalShapes
import com.highcapable.flexiui.LocalSizes
@@ -134,7 +137,7 @@ fun TextField(
focused || hovered -> style.borderInactive
else -> style.borderInactive
}.copy(animatedBorderWidth, SolidColor(animatedBorderColor))
Row(
BoxWithConstraints(
modifier = Modifier.textField(
colors = colors,
style = style,
@@ -142,52 +145,57 @@ fun TextField(
enabled = enabled,
interactionSource = interactionSource,
modifier = modifier
),
verticalAlignment = Alignment.CenterVertically
)
) {
header?.also {
InnerDecorationBox(
decorTint = animatedDecorTint,
style = style,
header = true,
content = it
)
}
Box(modifier = Modifier.weight(1f, fill = false).textFieldPadding(style)) {
TextFieldStyle(colors) {
BasicTextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier.focusRequester(focusRequester).then(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,
placeholder = placeholder,
innerTextField = it
)
}
// 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 {
InnerDecorationBox(
decorTint = animatedDecorTint,
style = style,
header = true,
content = it
)
}
Box(modifier = Modifier.weight(1f, needInflatable).textFieldPadding(style)) {
TextFieldStyle(colors) {
BasicTextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier.focusRequester(focusRequester).inflatable(),
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,
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
)
}
}
}