|
| 1 | +package com.example.gridsample |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.ExperimentalGridApi |
| 7 | +import androidx.compose.foundation.layout.Grid |
| 8 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 9 | +import androidx.compose.foundation.layout.fillMaxSize |
| 10 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 11 | +import androidx.compose.foundation.layout.height |
| 12 | +import androidx.compose.foundation.layout.padding |
| 13 | +import androidx.compose.foundation.layout.safeDrawingPadding |
| 14 | +import androidx.compose.foundation.rememberScrollState |
| 15 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 16 | +import androidx.compose.foundation.verticalScroll |
| 17 | +import androidx.compose.material3.MaterialTheme |
| 18 | +import androidx.compose.material3.Text |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.ui.Alignment |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.graphics.Color |
| 23 | +import androidx.compose.ui.graphics.luminance |
| 24 | +import androidx.compose.ui.text.font.FontWeight |
| 25 | +import androidx.compose.ui.tooling.preview.Preview |
| 26 | +import androidx.compose.ui.unit.dp |
| 27 | +import androidx.compose.ui.unit.sp |
| 28 | +import com.example.gridsample.ui.theme.MyApplicationTheme |
| 29 | + |
| 30 | +@OptIn(ExperimentalGridApi::class) |
| 31 | +@Composable |
| 32 | +fun GridSample() { |
| 33 | + val colors = listOf( |
| 34 | + Color(0xFF4285F4), // Google Blue |
| 35 | + Color(0xFFEA4335), // Google Red |
| 36 | + Color(0xFFFBBC05), // Google Yellow |
| 37 | + Color(0xFF34A853), // Google Green |
| 38 | + Color(0xFF000000) // Black |
| 39 | + ) |
| 40 | + |
| 41 | + Column(modifier = Modifier.safeDrawingPadding().padding(16.dp).fillMaxSize().verticalScroll(rememberScrollState())) { |
| 42 | + Text( |
| 43 | + text = "Grid sample", |
| 44 | + fontSize = 30.sp, |
| 45 | + fontWeight = FontWeight.Bold, |
| 46 | + color = MaterialTheme.colorScheme.onBackground, |
| 47 | + modifier = Modifier.padding(bottom = 8.dp) |
| 48 | + ) |
| 49 | + |
| 50 | + Text( |
| 51 | + text = "Display Grid layout variations on this device.", |
| 52 | + fontSize = 14.sp, |
| 53 | + color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f), |
| 54 | + modifier = Modifier.padding(bottom = 16.dp) |
| 55 | + ) |
| 56 | + |
| 57 | + Grid( |
| 58 | + config = { |
| 59 | + repeat(3) { column(0.333f) } |
| 60 | + row(112.dp) |
| 61 | + row(88.dp) |
| 62 | + row(100.dp) |
| 63 | + gap(8.dp) |
| 64 | + }, |
| 65 | + modifier = Modifier.fillMaxWidth() |
| 66 | + ) { |
| 67 | + Box( |
| 68 | + modifier = Modifier |
| 69 | + .gridItem(rowSpan = 2) |
| 70 | + .background(colors[0], shape = RoundedCornerShape(16.dp)) |
| 71 | + .fillMaxSize() |
| 72 | + ) { |
| 73 | + Text( |
| 74 | + text = "1", |
| 75 | + color = if (colors[0].luminance() > 0.5f) Color.Black else Color.White, |
| 76 | + modifier = Modifier.align(Alignment.Center), |
| 77 | + fontSize = 40.sp |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + Box( |
| 82 | + modifier = Modifier |
| 83 | + .background(colors[1], shape = RoundedCornerShape(16.dp)) |
| 84 | + .fillMaxSize() |
| 85 | + ) { |
| 86 | + Text( |
| 87 | + text = "2", |
| 88 | + color = if (colors[1].luminance() > 0.5f) Color.Black else Color.White, |
| 89 | + modifier = Modifier.align(Alignment.Center), |
| 90 | + fontSize = 40.sp |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + Box( |
| 95 | + modifier = Modifier |
| 96 | + .background(colors[2], shape = RoundedCornerShape(16.dp)) |
| 97 | + .fillMaxSize() |
| 98 | + ) { |
| 99 | + Text( |
| 100 | + text = "3", |
| 101 | + color = if (colors[2].luminance() > 0.5f) Color.Black else Color.White, |
| 102 | + modifier = Modifier.align(Alignment.Center), |
| 103 | + fontSize = 40.sp |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + Box( |
| 108 | + modifier = Modifier |
| 109 | + .gridItem(columnSpan = 2) |
| 110 | + .background(colors[3], shape = RoundedCornerShape(16.dp)) |
| 111 | + .fillMaxSize() |
| 112 | + ) { |
| 113 | + Text( |
| 114 | + text = "4", |
| 115 | + color = if (colors[3].luminance() > 0.5f) Color.Black else Color.White, |
| 116 | + modifier = Modifier.align(Alignment.Center), |
| 117 | + fontSize = 40.sp |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + Box( |
| 122 | + modifier = Modifier |
| 123 | + .gridItem(columnSpan = 3) |
| 124 | + .background(colors[4], shape = RoundedCornerShape(16.dp)) |
| 125 | + .fillMaxSize() |
| 126 | + ) { |
| 127 | + Text( |
| 128 | + text = "5", |
| 129 | + color = if (colors[4].luminance() > 0.5f) Color.Black else Color.White, |
| 130 | + modifier = Modifier.align(Alignment.Center), |
| 131 | + fontSize = 40.sp |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +@Preview(showBackground = true) |
| 139 | +@Composable |
| 140 | +fun GridSamplePreview() { |
| 141 | + MyApplicationTheme { |
| 142 | + GridSample() |
| 143 | + } |
| 144 | +} |
0 commit comments