Jetpack Compose组件-对话框(Dialog)


Jetpack Compose组件-对话框(Dialog)

警告对话框(AlertDialog)

@Composable
fun AlertDialog(
    // 关闭请求回调,点击对话框外部或者按下返回按钮关闭对话框时调用
    onDismissRequest: () -> Unit,
    // 对话框按钮
    buttons: @Composable () -> Unit,
    // 修饰符
    modifier: Modifier = Modifier,
    // 标题
    title: (@Composable () -> Unit)? = null,
    // 文本
    text: (@Composable () -> Unit)? = null,
    // 形状
    shape: Shape = MaterialTheme.shapes.medium,
    // 背景颜色
    backgroundColor: Color = MaterialTheme.colors.surface,
    // 内容颜色
    contentColor: Color = contentColorFor(backgroundColor),
    // 对话框属性
    properties: DialogProperties = DialogProperties()
): Unit

@Composable
fun AlertDialog(
    // 关闭请求回调,点击对话框外部或者按下返回按钮关闭对话框时调用
    onDismissRequest: () -> Unit,
    // 确认按钮
    confirmButton: @Composable () -> Unit,
    // 修饰符
    modifier: Modifier = Modifier,
    // 关闭按钮
    dismissButton: (@Composable () -> Unit)? = null,
    // 标题
    title: (@Composable () -> Unit)? = null,
    // 文本
    text: (@Composable () -> Unit)? = null,
    // 形状
    shape: Shape = MaterialTheme.shapes.medium,
    // 背景颜色
    backgroundColor: Color = MaterialTheme.colors.surface,
    // 内容颜色
    contentColor: Color = contentColorFor(backgroundColor),
    // 对话框属性
    properties: DialogProperties = DialogProperties()
): Unit
val openDialog = remember { mutableStateOf(false)  }

Button(onClick = {
    openDialog.value = true
}) {
    Text("Click me")
}

if (openDialog.value) {
    AlertDialog(
        onDismissRequest = {
            openDialog.value = false
        },
        title = {
            Text(text = "Dialog Title")
        },
        text = {
            Text("Here is a text ")
        },
        confirmButton = {
            Button(
                onClick = {
                    openDialog.value = false
                }) {
                Text("This is the Confirm Button")
            }
        },
        dismissButton = {
            Button(
                onClick = {
                    openDialog.value = false
                }) {
                Text("This is the dismiss Button")
            }
        }
    )
}

对话框(Dialog)

AlertDialog 在一些情况下有可能还是无法满足我们的业务要求,这时候我们就可以使用更底层的一个 @Composable 函数 —— Dialog

@Composable
fun Dialog(
	// 试图关闭对话框时执行
    onDismissRequest: () -> Unit,
    // 对话框属性
    properties: DialogProperties = DialogProperties(),
    content: @Composable () -> Unit
): Unit
var flag by remember{ mutableStateOf(false) }
Box(
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Alignment.Center
) {
    Button(
        onClick = { flag = true }
    ) {
        Text("弹窗")
    }
}
if(flag) {
    Dialog(
        onDismissRequest = { flag = false }
    ) {
        Box(
            modifier = Modifier
                .size(300.dp)
                .background(Color.White),
            contentAlignment = Alignment.Center
        ) {
            Column {
                LinearProgressIndicator()
                Text("加载中 ing...")
            }
        }
    }
}

弹出窗口(Popup)

@Composable
fun Popup(
	// 对其方式
    alignment: Alignment = Alignment.TopStart,
    // 偏移
    offset: IntOffset = IntOffset(0, 0),
	// 试图关闭窗口时执行
    onDismissRequest: (() -> Unit)? = null,
    // 弹出窗口属性
    properties: PopupProperties = PopupProperties(),
    content: @Composable () -> Unit
): Unit

@Composable
fun Popup(
	// 弹出窗口的屏幕位置
    popupPositionProvider: PopupPositionProvider,
	// 试图关闭窗口时执行
    onDismissRequest: (() -> Unit)? = null,
    // 弹出窗口属性
    properties: PopupProperties = PopupProperties(),
    content: @Composable () -> Unit
): Unit
Box {
    val popupWidth = 200.dp
    val popupHeight = 50.dp
    val cornerSize = 16.dp

    Popup(alignment = Alignment.Center) {
        // Draw a rectangle shape with rounded corners inside the popup
        Box(
            Modifier
                .size(popupWidth, popupHeight)
                .background(Color.White, RoundedCornerShape(cornerSize))
        )
    }
}

文章作者: Vinx
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Vinx !
 上一篇
Jetpack Compose组件-左右滑动删除(SwipeToDismiss) Jetpack Compose组件-左右滑动删除(SwipeToDismiss)
Jetpack Compose组件-左右滑动删除(SwipeToDismiss) 左右滑动删除(SwipeToDismiss) Divider是将列表和布局中的内容分组的细线。 @Composable @ExperimentalMa
下一篇 
Jetpack Compose组件-Snackbar Jetpack Compose组件-Snackbar
Jetpack Compose组件-Snackbar Snackbar 将很多的组件摆放在这个平面之上,我们可以设置这个平面的边框,圆角,颜色等等。 Surface实际上是Box的重新包装。 @Composable fun Snac
  目录