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