411 lines
11 KiB
Markdown
411 lines
11 KiB
Markdown
# KMP Mini Shopping UI 实现指南(Android|逐步照写版)
|
||
|
||
> 目标:这份文档按“最小可运行 -> 完整效果”拆到每一步。学员照着写即可完成 UI。
|
||
> 前置:`ShopViewModel` 已存在(第7节已完成)。
|
||
|
||
---
|
||
|
||
## 0. 准备(先不写业务)
|
||
|
||
### 0.1 打开文件
|
||
|
||
- `composeApp/src/androidMain/kotlin/com/example/minishop/App.kt`
|
||
|
||
### 0.2 先放最小 App 骨架(确认可编译)
|
||
|
||
```kotlin
|
||
package com.example.minishop
|
||
|
||
import androidx.compose.runtime.Composable
|
||
import androidx.compose.material3.Text
|
||
|
||
@Composable
|
||
fun App() {
|
||
Text("Mini Shopping")
|
||
}
|
||
```
|
||
|
||
先 Run 一次,确认 App 能起来。
|
||
|
||
---
|
||
|
||
## 1. 第一步:搭 Scaffold 壳子(顶栏 + 底栏 + 内容区)
|
||
|
||
把 `App()` 改成下面:
|
||
|
||
```kotlin
|
||
@Composable
|
||
fun App() {
|
||
val vm: ShopViewModel = androidx.lifecycle.viewmodel.compose.viewModel()
|
||
val ui = vm.uiState
|
||
|
||
androidx.compose.material3.MaterialTheme {
|
||
androidx.compose.material3.Scaffold(
|
||
topBar = {
|
||
androidx.compose.material3.TopAppBar(
|
||
title = {
|
||
androidx.compose.material3.Text(
|
||
when {
|
||
ui.inDetail -> "商品详情"
|
||
ui.currentTab == Tab.Home -> "KMP Mini Shopping"
|
||
else -> "购物车"
|
||
}
|
||
)
|
||
}
|
||
)
|
||
},
|
||
bottomBar = {
|
||
if (!ui.inDetail) {
|
||
androidx.compose.material3.NavigationBar {
|
||
androidx.compose.material3.NavigationBarItem(
|
||
selected = ui.currentTab == Tab.Home,
|
||
onClick = { vm.selectTab(Tab.Home) },
|
||
icon = { androidx.compose.material3.Text("🏠") },
|
||
label = { androidx.compose.material3.Text("商品") }
|
||
)
|
||
androidx.compose.material3.NavigationBarItem(
|
||
selected = ui.currentTab == Tab.Cart,
|
||
onClick = { vm.selectTab(Tab.Cart) },
|
||
icon = { androidx.compose.material3.Text("🛒") },
|
||
label = { androidx.compose.material3.Text("购物车") }
|
||
)
|
||
}
|
||
}
|
||
}
|
||
) { padding ->
|
||
androidx.compose.foundation.layout.Box(
|
||
androidx.compose.ui.Modifier
|
||
.padding(padding)
|
||
.fillMaxSize()
|
||
) {
|
||
androidx.compose.material3.Text("内容区")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 顶部标题显示
|
||
- 底部两个 tab 显示
|
||
- 中间出现“内容区”
|
||
|
||
---
|
||
|
||
## 2. 第二步:建三个页面函数(先空实现)
|
||
|
||
在 `App.kt` 底部新增:
|
||
|
||
```kotlin
|
||
@Composable
|
||
private fun ProductListScreen() {
|
||
androidx.compose.material3.Text("商品列表页")
|
||
}
|
||
|
||
@Composable
|
||
private fun ProductDetailScreen() {
|
||
androidx.compose.material3.Text("详情页")
|
||
}
|
||
|
||
@Composable
|
||
private fun CartScreen() {
|
||
androidx.compose.material3.Text("购物车页")
|
||
}
|
||
```
|
||
|
||
然后把内容区替换成路由切换:
|
||
|
||
```kotlin
|
||
when {
|
||
ui.inDetail -> ProductDetailScreen()
|
||
ui.currentTab == Tab.Home -> ProductListScreen()
|
||
else -> CartScreen()
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 点底栏可切换“商品列表页 / 购物车页”
|
||
|
||
---
|
||
|
||
## 3. 第三步:把商品列表接上数据(先不用瀑布流)
|
||
|
||
先把列表页改成 `LazyColumn` 版本,确保数据逻辑通:
|
||
|
||
```kotlin
|
||
@Composable
|
||
private fun ProductListScreen(
|
||
products: List<com.example.minishop.model.Product>,
|
||
onClick: (com.example.minishop.model.Product) -> Unit,
|
||
onAdd: (com.example.minishop.model.Product) -> Unit
|
||
) {
|
||
androidx.compose.foundation.lazy.LazyColumn {
|
||
items(products, key = { it.id }) { p ->
|
||
androidx.compose.material3.Card(
|
||
modifier = androidx.compose.ui.Modifier
|
||
.fillMaxWidth()
|
||
.padding(8.dp),
|
||
onClick = { onClick(p) }
|
||
) {
|
||
androidx.compose.foundation.layout.Column(
|
||
modifier = androidx.compose.ui.Modifier.padding(12.dp)
|
||
) {
|
||
androidx.compose.material3.Text(p.title)
|
||
androidx.compose.material3.Text("¥${p.price}")
|
||
androidx.compose.material3.Text(p.description)
|
||
androidx.compose.material3.Button(onClick = { onAdd(p) }) {
|
||
androidx.compose.material3.Text("加购")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
在 `App()` 里调用:
|
||
|
||
```kotlin
|
||
ui.currentTab == Tab.Home -> ProductListScreen(
|
||
products = vm.filteredProducts,
|
||
onClick = vm::openDetail,
|
||
onAdd = { vm.addToCart(it.id) }
|
||
)
|
||
```
|
||
|
||
### 本步验收
|
||
- 能看到后端商品列表
|
||
- 点“加购”不崩
|
||
|
||
---
|
||
|
||
## 4. 第四步:升级为两列瀑布流
|
||
|
||
把 `ProductListScreen` 里的 `LazyColumn` 换成:
|
||
|
||
```kotlin
|
||
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
|
||
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
|
||
import androidx.compose.foundation.lazy.staggeredgrid.items as staggeredItems
|
||
|
||
LazyVerticalStaggeredGrid(
|
||
columns = StaggeredGridCells.Fixed(2),
|
||
verticalItemSpacing = 10.dp,
|
||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||
modifier = Modifier.fillMaxSize()
|
||
) {
|
||
staggeredItems(products, key = { it.id }) { p ->
|
||
// 卡片内容沿用上一步
|
||
}
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 列表变成两列
|
||
- 滚动正常
|
||
|
||
---
|
||
|
||
## 5. 第五步:实现详情页
|
||
|
||
改 `ProductDetailScreen` 为:
|
||
|
||
```kotlin
|
||
@Composable
|
||
private fun ProductDetailScreen(
|
||
product: com.example.minishop.model.Product?,
|
||
quantity: Int,
|
||
onAdd: (com.example.minishop.model.Product) -> Unit
|
||
) {
|
||
if (product == null) {
|
||
androidx.compose.material3.Text("商品不存在")
|
||
return
|
||
}
|
||
|
||
androidx.compose.foundation.layout.Column(
|
||
modifier = Modifier
|
||
.fillMaxSize()
|
||
.padding(16.dp),
|
||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||
) {
|
||
ProductCover(product.imageUrl)
|
||
androidx.compose.material3.Text(product.title)
|
||
androidx.compose.material3.Text("¥${product.price}")
|
||
androidx.compose.material3.Text(product.description)
|
||
if (quantity > 0) {
|
||
androidx.compose.material3.Text("当前已加购:$quantity")
|
||
}
|
||
androidx.compose.material3.Button(onClick = { onAdd(product) }) {
|
||
androidx.compose.material3.Text("加入购物车")
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
`App()` 里调用:
|
||
|
||
```kotlin
|
||
ui.inDetail -> ProductDetailScreen(
|
||
product = vm.selectedProduct,
|
||
quantity = vm.selectedProduct?.let { vm.quantityOf(it.id) } ?: 0,
|
||
onAdd = { vm.addToCart(it.id) }
|
||
)
|
||
```
|
||
|
||
### 本步验收
|
||
- 点商品进入详情
|
||
- 详情页可加购
|
||
|
||
---
|
||
|
||
## 6. 第六步:实现购物车页(先行项目)
|
||
|
||
```kotlin
|
||
import androidx.compose.foundation.lazy.LazyColumn
|
||
import androidx.compose.foundation.lazy.items as lazyListItems
|
||
|
||
@Composable
|
||
private fun CartScreen(
|
||
items: List<com.example.minishop.cart.CartItem>,
|
||
productMap: Map<String, com.example.minishop.model.Product>,
|
||
onInc: (String) -> Unit,
|
||
onDec: (String) -> Unit,
|
||
onRemove: (String) -> Unit
|
||
) {
|
||
LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||
lazyListItems(items) { item ->
|
||
val p = productMap[item.productId] ?: return@lazyListItems
|
||
androidx.compose.material3.Card(Modifier.fillMaxWidth()) {
|
||
androidx.compose.foundation.layout.Column(Modifier.padding(12.dp)) {
|
||
androidx.compose.material3.Text(p.title)
|
||
androidx.compose.material3.Text("数量:${item.quantity}")
|
||
androidx.compose.foundation.layout.Row {
|
||
androidx.compose.material3.OutlinedButton(onClick = { onDec(p.id) }) { androidx.compose.material3.Text("-") }
|
||
androidx.compose.material3.OutlinedButton(onClick = { onInc(p.id) }) { androidx.compose.material3.Text("+") }
|
||
androidx.compose.material3.OutlinedButton(onClick = { onRemove(p.id) }) { androidx.compose.material3.Text("移除") }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 购物车可增减删
|
||
|
||
---
|
||
|
||
## 7. 第七步:加“底部固定结算栏”
|
||
|
||
把 `CartScreen` 外层改成 `Box`:
|
||
|
||
```kotlin
|
||
Box(Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp)) {
|
||
LazyColumn(
|
||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||
contentPadding = PaddingValues(bottom = 96.dp)
|
||
) {
|
||
// 购物车项
|
||
}
|
||
|
||
Card(
|
||
modifier = Modifier
|
||
.align(Alignment.BottomCenter)
|
||
.fillMaxWidth()
|
||
) {
|
||
// 总件数、总价、清空、结算
|
||
}
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 结算栏固定在底部
|
||
- 列表最后一项不会被遮挡
|
||
|
||
---
|
||
|
||
## 8. 第八步:补图片组件(失败兜底)
|
||
|
||
```kotlin
|
||
@Composable
|
||
private fun ProductCover(url: String) {
|
||
val coverModifier = Modifier
|
||
.fillMaxWidth()
|
||
.height(150.dp)
|
||
.clip(RoundedCornerShape(12.dp))
|
||
|
||
if (url.isNotBlank()) {
|
||
coil3.compose.SubcomposeAsyncImage(
|
||
model = url,
|
||
contentDescription = null,
|
||
modifier = coverModifier,
|
||
contentScale = ContentScale.Crop,
|
||
loading = {
|
||
androidx.compose.foundation.Image(
|
||
painter = painterResource(id = R.drawable.course_cover_default),
|
||
contentDescription = null,
|
||
modifier = coverModifier,
|
||
contentScale = ContentScale.Crop
|
||
)
|
||
},
|
||
error = {
|
||
androidx.compose.foundation.Image(
|
||
painter = painterResource(id = R.drawable.course_cover_default),
|
||
contentDescription = null,
|
||
modifier = coverModifier,
|
||
contentScale = ContentScale.Crop
|
||
)
|
||
},
|
||
success = { coil3.compose.SubcomposeAsyncImageContent() }
|
||
)
|
||
} else {
|
||
androidx.compose.foundation.Image(
|
||
painter = painterResource(id = R.drawable.course_cover_default),
|
||
contentDescription = null,
|
||
modifier = coverModifier,
|
||
contentScale = ContentScale.Crop
|
||
)
|
||
}
|
||
}
|
||
```
|
||
|
||
### 本步验收
|
||
- 有网显示网络图
|
||
- 慢网/失败显示本地图
|
||
|
||
---
|
||
|
||
## 9. 第九步:把搜索框加上
|
||
|
||
在列表页顶部加:
|
||
|
||
```kotlin
|
||
OutlinedTextField(
|
||
value = query,
|
||
onValueChange = onQueryChange,
|
||
modifier = Modifier.fillMaxWidth(),
|
||
label = { Text("搜索课程") },
|
||
singleLine = true
|
||
)
|
||
```
|
||
|
||
ViewModel 内做 `trim()` 过滤。
|
||
|
||
---
|
||
|
||
## 10. 最终对照(和当前项目一致)
|
||
|
||
最终应达到:
|
||
- 首页两列瀑布流
|
||
- 详情页
|
||
- 购物车增减删清
|
||
- 底部固定结算栏
|
||
- 图片兜底
|
||
- 搜索过滤
|
||
- 事件全部走 `ShopViewModel`
|
||
|
||
如果学员跟到第10步还有问题,直接对照:
|
||
- `App.kt`
|
||
- `ShopViewModel.kt`
|