Scaffold
ใน Material Design โครงสร้างพื้นฐานคือโครงสร้างพื้นฐานที่ให้แพลตฟอร์มมาตรฐานสำหรับอินเทอร์เฟซผู้ใช้ที่ซับซ้อน โดยจะรวมส่วนต่างๆ ของ UI เข้าด้วยกัน เช่น แถบแอปและปุ่มการทำงานแบบลอย เพื่อให้แอปมีรูปลักษณ์และลักษณะการทำงานที่สอดคล้องกัน
ตัวอย่าง
Composable Scaffold มี API ที่ตรงไปตรงมาซึ่งคุณสามารถใช้เพื่อประกอบโครงสร้างของแอปได้อย่างรวดเร็วตามหลักเกณฑ์ของ Material Design
Scaffold ยอมรับ Composable หลายรายการเป็นพารามิเตอร์ ซึ่งรวมถึงสิ่งต่อไปนี้
topBar: แถบแอปที่ด้านบนของหน้าจอbottomBar: แถบแอปที่ด้านล่างของหน้าจอfloatingActionButton: ปุ่มที่วางเหนือมุมขวาล่างของหน้าจอซึ่งคุณใช้เพื่อแสดงการดำเนินการหลักได้
นอกจากนี้ คุณยังส่งเนื้อหา Scaffold ได้เหมือนกับที่ส่งไปยังคอนเทนเนอร์อื่นๆ โดยจะส่ง PaddingValues ไปยังแลมบ์ดา content ที่คุณควรใช้กับ 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(), ) } } }
การใช้งานนี้จะปรากฏดังนี้