Dialog

คอมโพเนนต์ Dialog แสดงข้อความป๊อปอัปหรือขอข้อมูลจากผู้ใช้ใน เหนือเนื้อหาแอปหลักได้ ช่วยสร้างประสบการณ์การใช้งาน UI ที่รบกวนการทำงานของ ดึงดูดความสนใจของผู้ใช้

ตัวอย่างกรณีการใช้งานกล่องโต้ตอบมีดังนี้

  • ยืนยันการดำเนินการของผู้ใช้ เช่น เมื่อลบไฟล์
  • การขอข้อมูลจากผู้ใช้ เช่น ในแอปรายการสิ่งที่ต้องทำ
  • นำเสนอรายการตัวเลือกสำหรับการเลือกของผู้ใช้ เช่น การเลือกประเทศใน การตั้งค่าโปรไฟล์
กล่องโต้ตอบที่เต็มไปด้วยข้อความและไอคอน
รูปที่ 1 ตัวอย่างกล่องโต้ตอบที่เต็มไปด้วยข้อความและไอคอน

ช่องโต้ตอบการแจ้งเตือน

Composable ของ AlertDialog มี API เพื่อความสะดวกในการสร้าง กล่องโต้ตอบธีมดีไซน์ Material AlertDialog มีพารามิเตอร์เฉพาะสำหรับ จัดการองค์ประกอบเฉพาะของกล่องโต้ตอบ โดยส่วนหนึ่งมีดังต่อไปนี้

  • title: ข้อความที่ปรากฏที่ด้านบนของกล่องโต้ตอบ
  • text: ข้อความที่ปรากฏอยู่ตรงกลางภายในกล่องโต้ตอบ
  • icon: กราฟิกที่ปรากฏที่ด้านบนของกล่องโต้ตอบ
  • onDismissRequest: ฟังก์ชันที่เรียกใช้เมื่อผู้ใช้ปิดกล่องโต้ตอบ เช่น โดยแตะด้านนอก
  • dismissButton: Composable ที่ทำหน้าที่เป็นปุ่มปิด
  • confirmButton: Composable ที่ทำหน้าที่เป็นปุ่มยืนยัน

ตัวอย่างต่อไปนี้ใช้ปุ่ม 2 ปุ่มในกล่องโต้ตอบการแจ้งเตือน ปุ่มหนึ่งที่ ปิดกล่องโต้ตอบ และกล่องโต้ตอบอื่นที่ยืนยันคำขอ

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AlertDialogExample(
    onDismissRequest: () -> Unit,
    onConfirmation: () -> Unit,
    dialogTitle: String,
    dialogText: String,
    icon: ImageVector,
) {
    AlertDialog(
        icon = {
            Icon(icon, contentDescription = "Example Icon")
        },
        title = {
            Text(text = dialogTitle)
        },
        text = {
            Text(text = dialogText)
        },
        onDismissRequest = {
            onDismissRequest()
        },
        confirmButton = {
            TextButton(
                onClick = {
                    onConfirmation()
                }
            ) {
                Text("Confirm")
            }
        },
        dismissButton = {
            TextButton(
                onClick = {
                    onDismissRequest()
                }
            ) {
                Text("Dismiss")
            }
        }
    )
}

การติดตั้งใช้งานนี้บอกเป็นนัยว่า Composable หลักที่ส่งอาร์กิวเมนต์ไปยัง Composable ในลักษณะนี้

@Composable
fun DialogExamples() {
    // ...
    val openAlertDialog = remember { mutableStateOf(false) }

    // ...
        when {
            // ...
            openAlertDialog.value -> {
                AlertDialogExample(
                    onDismissRequest = { openAlertDialog.value = false },
                    onConfirmation = {
                        openAlertDialog.value = false
                        println("Confirmation registered") // Add logic here to handle confirmation.
                    },
                    dialogTitle = "Alert dialog example",
                    dialogText = "This is an example of an alert dialog with buttons.",
                    icon = Icons.Default.Info
                )
            }
        }
    }
}

การติดตั้งใช้งานนี้จะปรากฏดังนี้

วันที่ กล่องโต้ตอบการแจ้งเตือนที่เปิดอยู่ซึ่งมีทั้งปุ่มปิดและยืนยัน
รูปที่ 2 กล่องโต้ตอบการแจ้งเตือนที่มีปุ่มต่างๆ

กล่องโต้ตอบที่ประกอบกันได้

Dialog เป็น Composable พื้นฐานที่ไม่มีการจัดรูปแบบหรือ สล็อตที่กำหนดไว้ล่วงหน้าสำหรับเนื้อหา เป็นคอนเทนเนอร์ที่ค่อนข้างตรงไปตรงมา คุณควรเติมข้อมูลด้วยคอนเทนเนอร์ เช่น Card ตัวอย่างมีดังต่อไปนี้ พารามิเตอร์ที่สำคัญของกล่องโต้ตอบ ได้แก่

  • onDismissRequest: lambda จะเรียกใช้เมื่อผู้ใช้ปิดกล่องโต้ตอบ
  • properties: อินสแตนซ์ของ DialogProperties ที่มี ขอบเขตเพิ่มเติมสำหรับการปรับแต่ง

ตัวอย่างพื้นฐาน

ตัวอย่างต่อไปนี้เป็นการใช้งานพื้นฐานของ Dialog Composable หมายเหตุ โดยใช้ Card เป็นคอนเทนเนอร์รอง หากไม่มีCard Text จะปรากฏขึ้นเดี่ยวๆ เหนือเนื้อหาหลักของแอป

@Composable
fun MinimalDialog(onDismissRequest: () -> Unit) {
    Dialog(onDismissRequest = { onDismissRequest() }) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp),
        ) {
            Text(
                text = "This is a minimal dialog",
                modifier = Modifier
                    .fillMaxSize()
                    .wrapContentSize(Alignment.Center),
                textAlign = TextAlign.Center,
            )
        }
    }
}

การติดตั้งใช้งานนี้จะปรากฏดังนี้ โปรดทราบว่าเมื่อเปิดกล่องโต้ตอบ เนื้อหาแอปหลักที่อยู่ด้านล่างจะมีสีเข้มและเป็นสีเทา ดังนี้

วันที่ กล่องโต้ตอบที่ไม่มีป้ายกำกับอื่น
รูปที่ 3 กล่องโต้ตอบเรียบง่าย

ตัวอย่างขั้นสูง

วิธีการต่อไปนี้คือการใช้ Composable Dialog ในขั้นสูง ด้วยวิธีนี้ คอมโพเนนต์จะใช้อินเทอร์เฟซที่คล้ายกับ AlertDialog ด้วยตนเอง ตัวอย่างด้านบน

@Composable
fun DialogWithImage(
    onDismissRequest: () -> Unit,
    onConfirmation: () -> Unit,
    painter: Painter,
    imageDescription: String,
) {
    Dialog(onDismissRequest = { onDismissRequest() }) {
        // Draw a rectangle shape with rounded corners inside the dialog
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .height(375.dp)
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp),
        ) {
            Column(
                modifier = Modifier
                    .fillMaxSize(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally,
            ) {
                Image(
                    painter = painter,
                    contentDescription = imageDescription,
                    contentScale = ContentScale.Fit,
                    modifier = Modifier
                        .height(160.dp)
                )
                Text(
                    text = "This is a dialog with buttons and an image.",
                    modifier = Modifier.padding(16.dp),
                )
                Row(
                    modifier = Modifier
                        .fillMaxWidth(),
                    horizontalArrangement = Arrangement.Center,
                ) {
                    TextButton(
                        onClick = { onDismissRequest() },
                        modifier = Modifier.padding(8.dp),
                    ) {
                        Text("Dismiss")
                    }
                    TextButton(
                        onClick = { onConfirmation() },
                        modifier = Modifier.padding(8.dp),
                    ) {
                        Text("Confirm")
                    }
                }
            }
        }
    }
}

การติดตั้งใช้งานนี้จะปรากฏดังนี้

วันที่ กล่องโต้ตอบที่มีรูปภาพภูเขาเฟเธอร์ทอป รัฐวิกตอเรีย ด้านล่างรูปภาพคือปุ่มปิดและปุ่มยืนยัน
รูปที่ 4 กล่องโต้ตอบที่มีรูปภาพ

แหล่งข้อมูลเพิ่มเติม