官方文档 修饰符列表
Modifier 是有序的, 例如 clickable padding background 设置不同的顺序 效果是不一样的
aspectRatio() 设置宽高比例
align(): 设置组件在父容器中的对齐方式。
paddingFromBaseline(): 在组件的基线周围添加内边距。
requiredHeight(): 强制组件具有指定的高度。
clip(): 将组件裁剪为指定形状。例如 Modifier.clip(RoundedCornerShape(8.dp)) // 圆角
draggable(): 将组件设置为可拖动,并在拖动时执行指定的操作。
alpha(): 设置组件的透明度。
indication(): 设置组件的触摸反馈效果。
keyboardActions(): 在键盘事件发生时执行指定的操作。
shadow(): 添加阴影效果。
testTag(): 给组件设置测试标签,用于自动化测试。
@Composable
fun DraggableExample() {
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
Box(
modifier = Modifier
.size(100.dp)
.background(Color.Blue)
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
offsetX += delta
}
)
.draggable(
orientation = Orientation.Vertical,
state = rememberDraggableState { delta ->
offsetY += delta
}
)
)
}
@Composable
fun ScalableExample() {
var scale by remember { mutableStateOf(1f) }
val scaleGestureModifier = Modifier.pointerInput(Unit) {
detectTransformGestures { _, pan, zoom, _ ->
scale *= zoom
}
}
Box(
modifier = scaleGestureModifier
.size(100.dp)
.background(Color.Red)
.graphicsLayer(scaleX = scale, scaleY = scale)
)
}