簡體   English   中英

Jetpack Compose 從修改器中提取填充信息

[英]Jetpack Compose extract padding info from Modifier

我想從修飾符中提取填充(或其他參數,如果需要)信息。 我需要提取填充以使用Modiifer.drawBehindLayout在我的可組合列表后面繪制一個氣泡形狀。 從應用第二個氣泡填充中可以看出,但由於我無法獲得填充,因此在應用填充后它不會覆蓋整個布局僅可用區域

在此處輸入圖像描述 在此處輸入圖像描述

  @Composable
    fun BubbleColumn(
        modifier: Modifier = Modifier,
        bubbleState: BubbleState,
        content: @Composable () -> Unit
    ) {
        val contentRect = remember { BubbleRect() }
        val bubbleRect = remember { BubbleRect() }
        val path = remember { Path() }
    
        val newModifier = modifier
            .materialShadow(bubbleState, path)
            .drawBehind {
                drawPath(path = path, color = bubbleState.backgroundColor)
    
                drawRect(
                    color = Color.Red,
                    topLeft = Offset(bubbleRect.left, bubbleRect.top),
                    size = Size(bubbleRect.width, bubbleRect.height),
                    style = Stroke(2f)
                )
    
                drawRect(
                    color = Color.Blue,
                    topLeft = Offset(contentRect.left, contentRect.top),
                    size = Size(contentRect.width, contentRect.height),
                    style = Stroke(2f)
                )
            }
    
        Layout(
            content = content,
            modifier = newModifier
    
        ) { measurables, constraints ->
    
            measureBubbleColumnResult(
                bubbleState = bubbleState,
                measurables = measurables,
                constraints = constraints,
                rectContent = contentRect,
                rect = bubbleRect,
                path = path
            )
        }
    }

用途如下

    BubbleColumn(
        modifier = Modifier.background(Color.Yellow),
        bubbleState = bubbleStateShadow1
    ) {
        Text(text = "Composable1")
        Text(text = "Composable12")
    }

    Spacer(modifier = Modifier.width(10.dp))
    BubbleColumn(
        modifier = Modifier.background(Color.Green).padding(10.dp),
        bubbleState = bubbleStateShadow2
    ) {
        Text(text = "Composable1")
        Text(text = "Composable12")
    }

當我為BubbleColumn內部布局設置填充時,無法使用placeable寬度或Constraint獲得填充。 接受的這個問題的答案表明沒有辦法提取它,但我從谷歌開發人員那里讀到,找不到源,有可能使用Modifier.anyModifier.foldInModifier.oldOut但找不到答案,它也沒有指定如何這樣做,或者如何使用這些修飾符。

我可以為我的問題創建ParentDataModifier或直接傳遞填充值,但我的問題不僅針對此問題,我想知道如何從修飾符本身檢索數據,因為 toString 方法顯示了Modifier的一些可用參數。

嘗試改變

val newModifier = modifier
    .materialShadow(bubbleState, path)
    .drawBehind {
        drawPath(path = path, color = bubbleState.backgroundColor)
    
        drawRect(
            color = Color.Red,
            topLeft = Offset(bubbleRect.left, bubbleRect.top),
            size = Size(bubbleRect.width, bubbleRect.height),
            style = Stroke(2f)
        )

        drawRect(
            color = Color.Blue,
            topLeft = Offset(contentRect.left, contentRect.top),
            size = Size(contentRect.width, contentRect.height),
            style = Stroke(2f)
        )
    }

val newModifier = Modifier
    .materialShadow(bubbleState, path)
    .drawBehind {
        drawPath(path = path, color = bubbleState.backgroundColor)
    
        drawRect(
            color = Color.Red,
            topLeft = Offset(bubbleRect.left, bubbleRect.top),
            size = Size(bubbleRect.width, bubbleRect.height),
            style = Stroke(2f)
        )

        drawRect(
            color = Color.Blue,
            topLeft = Offset(contentRect.left, contentRect.top),
            size = Size(contentRect.width, contentRect.height),
            style = Stroke(2f)
        )
    }
    .then(modifier)

或在適當的位置設置一個參數,如contentPadding並設置修飾符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM