ui: ecommerce-style 2-column waterfall product grid
This commit is contained in:
1 parent
5e2eb56658
commit
5196d9da10
1 file changed
+26
-13
@@ -1,5 +1,6 @@
|
||||
package com.example.minishop
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
@@ -11,8 +12,9 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
|
||||
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
|
||||
import androidx.compose.foundation.lazy.staggeredgrid.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
@@ -246,24 +248,35 @@ private fun ProductListScreen(
|
||||
}
|
||||
products.isEmpty() -> EmptyState("没有匹配的商品")
|
||||
else -> {
|
||||
LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
items(products) { p ->
|
||||
LazyVerticalStaggeredGrid(
|
||||
columns = StaggeredGridCells.Fixed(2),
|
||||
verticalItemSpacing = 10.dp,
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
items(products, key = { it.id }) { p ->
|
||||
ElevatedCard(onClick = { onClick(p) }, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Column(Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
ProductCover(p.imageUrl)
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(p.title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
Text(
|
||||
p.title,
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
PriceTag(p.price)
|
||||
}
|
||||
Text(p.description, style = MaterialTheme.typography.bodySmall)
|
||||
Text(
|
||||
p.description,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
maxLines = 3
|
||||
)
|
||||
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Button(onClick = { onAdd(p) }) { Text("加入购物车") }
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Button(onClick = { onAdd(p) }) { Text("加购") }
|
||||
Spacer(Modifier.width(8.dp))
|
||||
val q = quantityOf(p.id)
|
||||
if (q > 0) {
|
||||
Text(
|
||||
text = "已加购 x$q",
|
||||
text = "x$q",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
@@ -402,7 +415,7 @@ private fun ProductCover(url: String) {
|
||||
fallback = painterResource(id = R.drawable.course_cover_default)
|
||||
)
|
||||
} else {
|
||||
androidx.compose.foundation.Image(
|
||||
Image(
|
||||
painter = painterResource(id = R.drawable.course_cover_default),
|
||||
contentDescription = null,
|
||||
modifier = Modifier
|
||||
|
||||
Reference in new issue
Block a user