feat: add contentAlignment, propagateMinConstraints in Surface

This commit is contained in:
2024-01-18 09:30:57 +08:00
parent ad9c2fb17f
commit cc1c5ec0fc

View File

@@ -33,6 +33,7 @@ import androidx.compose.runtime.Immutable
import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.Stable import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@@ -62,6 +63,8 @@ data class SurfaceColors(
* @param initializer the [Modifier] initializer, earlies than [modifier]. * @param initializer the [Modifier] initializer, earlies than [modifier].
* @param colors the colors of surface, default is [SurfaceDefaults.colors]. * @param colors the colors of surface, default is [SurfaceDefaults.colors].
* @param padding the padding of surface, default is [SurfaceDefaults.padding]. * @param padding the padding of surface, default is [SurfaceDefaults.padding].
* @param contentAlignment the alignment of the content inside this surface, default is [Alignment.TopStart].
* @param propagateMinConstraints whether to propagate the min constraints from the content to this surface.
* @param content the content of the [Surface]. * @param content the content of the [Surface].
*/ */
@Composable @Composable
@@ -70,6 +73,8 @@ fun Surface(
initializer: @Composable Modifier.() -> Modifier = { Modifier }, initializer: @Composable Modifier.() -> Modifier = { Modifier },
colors: SurfaceColors = SurfaceDefaults.colors(), colors: SurfaceColors = SurfaceDefaults.colors(),
padding: ComponentPadding = SurfaceDefaults.padding, padding: ComponentPadding = SurfaceDefaults.padding,
contentAlignment: Alignment = Alignment.TopStart,
propagateMinConstraints: Boolean = false,
content: @Composable BoxScope.() -> Unit content: @Composable BoxScope.() -> Unit
) { ) {
CompositionLocalProvider( CompositionLocalProvider(
@@ -78,7 +83,14 @@ fun Surface(
backgroundPrimary = colors.backgroundColor, backgroundPrimary = colors.backgroundColor,
textPrimary = colors.contentColor textPrimary = colors.contentColor
) )
) { Box(Modifier.surface(colors, padding, modifier, initializer), content = content) } ) {
Box(
modifier = Modifier.surface(colors, padding, modifier, initializer),
contentAlignment = contentAlignment,
propagateMinConstraints = propagateMinConstraints,
content = content
)
}
} }
private fun Modifier.surface( private fun Modifier.surface(