ui: improve android shopping UI presentation
This commit is contained in:
1 parent
823080245d
commit
035d396eaf
1 file changed
+127
-32
@@ -1,16 +1,22 @@
|
||||
package com.example.minishop
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.ElevatedCard
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
@@ -21,6 +27,7 @@ import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
@@ -31,7 +38,10 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.minishop.cart.CartItem
|
||||
import com.example.minishop.cart.CartRepository
|
||||
@@ -52,10 +62,7 @@ fun App() {
|
||||
|
||||
val productRepository = remember {
|
||||
ProductRepository(
|
||||
api = ProductApi(
|
||||
baseUrl = "https://example.com",
|
||||
useMock = true
|
||||
)
|
||||
api = ProductApi(baseUrl = "https://example.com", useMock = true)
|
||||
)
|
||||
}
|
||||
val cartRepository = remember { CartRepository() }
|
||||
@@ -69,7 +76,6 @@ fun App() {
|
||||
var products by remember { mutableStateOf<List<Product>>(emptyList()) }
|
||||
var loading by remember { mutableStateOf(false) }
|
||||
var loadError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
var tick by remember { mutableStateOf(0) }
|
||||
|
||||
fun loadProducts() {
|
||||
@@ -106,7 +112,7 @@ fun App() {
|
||||
Text(
|
||||
when {
|
||||
inDetail -> "商品详情"
|
||||
currentTab == Tab.Home -> "Mini Shopping"
|
||||
currentTab == Tab.Home -> "KMP Mini Shopping"
|
||||
else -> "购物车"
|
||||
}
|
||||
)
|
||||
@@ -114,6 +120,12 @@ fun App() {
|
||||
actions = {
|
||||
if (inDetail) {
|
||||
TextButton(onClick = { inDetail = false }) { Text("返回") }
|
||||
} else {
|
||||
Text(
|
||||
text = "共 $totalCount 件",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
modifier = Modifier.padding(end = 12.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -137,7 +149,12 @@ fun App() {
|
||||
}
|
||||
}
|
||||
) { padding ->
|
||||
Box(Modifier.padding(padding).fillMaxSize()) {
|
||||
Box(
|
||||
Modifier
|
||||
.padding(padding)
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.surface)
|
||||
) {
|
||||
when {
|
||||
inDetail -> ProductDetailScreen(
|
||||
product = selected,
|
||||
@@ -199,7 +216,12 @@ private fun ProductListScreen(
|
||||
onAdd: (Product) -> Unit,
|
||||
onClick: (Product) -> Unit
|
||||
) {
|
||||
Column(Modifier.fillMaxSize().padding(12.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Column(
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(12.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp)
|
||||
) {
|
||||
OutlinedTextField(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
value = query,
|
||||
@@ -210,29 +232,38 @@ private fun ProductListScreen(
|
||||
)
|
||||
|
||||
when {
|
||||
loading -> EmptyState("加载中...")
|
||||
loading -> EmptyState("⏳ 加载中...")
|
||||
error != null -> {
|
||||
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("加载失败:$error")
|
||||
Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer)) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("加载失败:$error", color = MaterialTheme.colorScheme.onErrorContainer)
|
||||
Button(onClick = onRetry) { Text("重试") }
|
||||
}
|
||||
}
|
||||
}
|
||||
products.isEmpty() -> EmptyState("没有匹配的商品")
|
||||
else -> {
|
||||
LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
items(products) { p ->
|
||||
ElevatedCard(onClick = { onClick(p) }, modifier = Modifier.fillMaxWidth()) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(p.title, style = MaterialTheme.typography.titleMedium)
|
||||
Text("¥${formatPrice(p.price)}")
|
||||
Text(p.title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
PriceTag(p.price)
|
||||
}
|
||||
Text(p.description, style = MaterialTheme.typography.bodySmall)
|
||||
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Button(onClick = { onAdd(p) }) { Text("加入购物车") }
|
||||
Spacer(Modifier.width(10.dp))
|
||||
val q = quantityOf(p.id)
|
||||
if (q > 0) Text("已加购 x$q", style = MaterialTheme.typography.bodySmall)
|
||||
if (q > 0) {
|
||||
Text(
|
||||
text = "已加购 x$q",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -254,13 +285,30 @@ private fun ProductDetailScreen(
|
||||
return
|
||||
}
|
||||
|
||||
Column(Modifier.fillMaxSize().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
Text(product.title, style = MaterialTheme.typography.headlineSmall)
|
||||
Text("价格:¥${formatPrice(product.price)}")
|
||||
Column(
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
tonalElevation = 2.dp,
|
||||
shape = RoundedCornerShape(14.dp)
|
||||
) {
|
||||
Column(Modifier.padding(14.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
Text(product.title, style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold)
|
||||
PriceTag(product.price)
|
||||
Text(product.description)
|
||||
if (quantity > 0) Text("当前已加购:$quantity")
|
||||
if (quantity > 0) {
|
||||
Text("当前已加购:$quantity", color = MaterialTheme.colorScheme.primary)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button(onClick = { onAdd(product) }) { Text("加入购物车") }
|
||||
Button(onClick = { onAdd(product) }, modifier = Modifier.fillMaxWidth()) {
|
||||
Text("加入购物车")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,31 +323,54 @@ private fun CartScreen(
|
||||
onClear: () -> Unit,
|
||||
onCheckout: () -> Unit
|
||||
) {
|
||||
Column(Modifier.fillMaxSize().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
Column(
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
if (items.isNotEmpty()) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.secondaryContainer)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(12.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column {
|
||||
Text("总件数:${items.sumOf { it.quantity }}")
|
||||
Text("合计:¥${formatPrice(totalPrice)}", fontWeight = FontWeight.Bold)
|
||||
}
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
OutlinedButton(onClick = onClear) { Text("清空购物车") }
|
||||
OutlinedButton(onClick = onClear) { Text("清空") }
|
||||
Button(onClick = onCheckout) { Text("结算") }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (items.isEmpty()) {
|
||||
EmptyState("购物车为空")
|
||||
} else {
|
||||
LazyColumn(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
items(items) { item ->
|
||||
val product = productMap[item.productId]
|
||||
if (product != null) {
|
||||
Card(Modifier.fillMaxWidth()) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text(product.title)
|
||||
Text(product.title, fontWeight = FontWeight.Medium)
|
||||
Text("¥${formatPrice(product.price * item.quantity)}")
|
||||
}
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
OutlinedButton(onClick = { onDec(product.id) }) { Text("-") }
|
||||
Text("数量 ${item.quantity}")
|
||||
OutlinedButton(onClick = { onInc(product.id) }) { Text("+") }
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
SmallOpButton("-") { onDec(product.id) }
|
||||
Text(" ${item.quantity} ")
|
||||
SmallOpButton("+") { onInc(product.id) }
|
||||
Spacer(Modifier.width(8.dp))
|
||||
OutlinedButton(onClick = { onRemove(product.id) }) { Text("移除") }
|
||||
}
|
||||
}
|
||||
@@ -307,17 +378,41 @@ private fun CartScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
Text("总件数:${items.sumOf { it.quantity }}")
|
||||
Text("合计:¥${formatPrice(totalPrice)}", style = MaterialTheme.typography.titleMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SmallOpButton(text: String, onClick: () -> Unit) {
|
||||
OutlinedButton(onClick = onClick) {
|
||||
Text(text)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun PriceTag(price: Double) {
|
||||
Surface(
|
||||
color = MaterialTheme.colorScheme.primaryContainer,
|
||||
shape = RoundedCornerShape(10.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "¥${formatPrice(price)}",
|
||||
modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp),
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EmptyState(text: String) {
|
||||
Box(Modifier.fillMaxSize().padding(24.dp)) {
|
||||
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Text("🧺", style = MaterialTheme.typography.headlineLarge)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Text(text, style = MaterialTheme.typography.bodyLarge)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatPrice(price: Double): String = "%.2f".format(price)
|
||||
Reference in new issue
Block a user