Scaffold

ใน Material Design โครงสร้างพื้นฐานคือโครงสร้างพื้นฐานที่ให้ แพลตฟอร์มที่ได้มาตรฐานสำหรับอินเทอร์เฟซผู้ใช้ที่ซับซ้อน โดยจะรวมส่วนต่างๆ ของ UI เข้าด้วยกัน เช่น แถบแอปและปุ่มลอย เพื่อให้แอปมีรูปลักษณ์และรูปแบบที่สอดคล้องกัน

ตัวอย่าง

Composable Scaffold มี API ที่ตรงไปตรงมาซึ่งคุณใช้เพื่อ ประกอบโครงสร้างของแอปอย่างรวดเร็วตามหลักเกณฑ์ของ Material Design ได้ Scaffold ยอมรับ Composable หลายรายการเป็นพารามิเตอร์ ซึ่งรวมถึงรายการต่อไปนี้

  • topBar: แถบแอปที่ด้านบนของหน้าจอ
  • bottomBar: แถบแอปที่ด้านล่างของหน้าจอ
  • floatingActionButton: ปุ่มที่ลอยอยู่เหนือมุมขวาล่างของหน้าจอ ซึ่งคุณใช้เพื่อแสดงการดำเนินการที่สำคัญได้

นอกจากนี้ คุณยังส่งScaffoldเนื้อหาได้เช่นเดียวกับคอนเทนเนอร์อื่นๆ โดยจะส่ง PaddingValues ไปยัง content lambda ที่คุณควรใช้กับ Composable ระดับรูทของเนื้อหาเพื่อจำกัดขนาด

ตัวอย่างต่อไปนี้แสดงScaffoldการติดตั้งใช้งานที่สมบูรณ์ ซึ่งประกอบด้วย แถบแอปด้านบน แถบแอปด้านล่าง และปุ่มการทำงานแบบลอย

@Composable
fun ScaffoldExample() {
    var presses by remember { mutableIntStateOf(0) }

    Scaffold(
        topBar = {
            TopAppBar(
                colors = topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.primary,
                ),
                title = {
                    Text("Top app bar")
                }
            )
        },
        bottomBar = {
            BottomAppBar(
                containerColor = MaterialTheme.colorScheme.primaryContainer,
                contentColor = MaterialTheme.colorScheme.primary,
            ) {
                Text(
                    modifier = Modifier
                        .fillMaxWidth(),
                    textAlign = TextAlign.Center,
                    text = "Bottom app bar",
                )
            }
        },
        floatingActionButton = {
            FloatingActionButton(onClick = { presses++ }) {
                Icon(Icons.Default.Add, contentDescription = "Add")
            }
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .padding(innerPadding),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            Text(
                modifier = Modifier.padding(8.dp),
                text =
                """
                    This is an example of a scaffold. It uses the Scaffold composable's parameters to create a screen with a simple top app bar, bottom app bar, and floating action button.

                    It also contains some basic inner content, such as this text.

                    You have pressed the floating action button $presses times.
                """.trimIndent(),
            )
        }
    }
}

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

การใช้งาน Scaffold ที่มีแถบแอปด้านบนและด้านล่างแบบง่าย รวมถึงปุ่มการทำงานแบบลอยที่วนซ้ำตัวนับ เนื้อหาด้านในของโครงสร้างเป็นข้อความธรรมดาที่อธิบายคอมโพเนนต์
รูปที่ 1 การติดตั้งใช้งานโครงร่าง

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