Files
kmp-mini-shopping/docs/UI_参考资料_Android.md
T
xiaobai ec1bcfa47e
Build Android app / Build (push) Has been cancelled
Build iOS app / Build (push) Has been cancelled
Template Cleanup / Template Cleanup (push) Has been cancelled
docs(ui): make UI material actionable for students with build sequence
2026-03-04 21:26:12 +08:00

172 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# KMP Mini Shopping UI 实现指南(Android)
> 这份文档解决“知道架构但不会落 UI”的问题。按步骤写,可从 0 到 1 完成页面。
---
## 1. 先做哪些文件
- `composeApp/src/androidMain/kotlin/com/example/minishop/App.kt`
- `composeApp/src/androidMain/kotlin/com/example/minishop/ShopViewModel.kt`(已在第7节完成)
- `composeApp/src/androidMain/res/drawable/course_cover_default.xml`
---
## 2. 第一步:先搭页面骨架(Scaffold)
先实现三块:
- `TopAppBar`
- `BottomBar(Home/Cart)`
- `Content` 按状态切换页面
```kotlin
@Composable
fun App() {
val vm: ShopViewModel = viewModel()
val ui = vm.uiState
Scaffold(
topBar = { /* TopAppBar */ },
bottomBar = { /* Home / Cart tab */ }
) { padding ->
Box(Modifier.padding(padding).fillMaxSize()) {
when {
ui.inDetail -> ProductDetailScreen(...)
ui.currentTab == Tab.Home -> ProductListScreen(...)
else -> CartScreen(...)
}
}
}
}
```
---
## 3. 第二步:实现首页商品列表(两列瀑布流)
### 3.1 列表组件
```kotlin
LazyVerticalStaggeredGrid(
columns = StaggeredGridCells.Fixed(2),
verticalItemSpacing = 10.dp,
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
staggeredItems(products, key = { it.id }) { p ->
ElevatedCard(onClick = { onClick(p) }) {
Column(Modifier.padding(10.dp)) {
ProductCover(p.imageUrl)
Text(p.title)
Text("¥${p.price}")
Text(p.description, maxLines = 3)
Button(onClick = { onAdd(p) }) { Text("加购") }
}
}
}
}
```
### 3.2 关键导入(避免冲突)
```kotlin
import androidx.compose.foundation.lazy.items as lazyListItems
import androidx.compose.foundation.lazy.staggeredgrid.items as staggeredItems
```
> 瀑布流用 `staggeredItems`,购物车 `LazyColumn` 用 `lazyListItems`。
---
## 4. 第三步:实现详情页
```kotlin
@Composable
private fun ProductDetailScreen(
product: Product?,
quantity: Int,
onAdd: (Product) -> Unit
) {
if (product == null) return
Column(Modifier.fillMaxSize().padding(16.dp)) {
ProductCover(product.imageUrl)
Text(product.title)
Text("¥${product.price}")
Text(product.description)
if (quantity > 0) Text("当前已加购:$quantity")
Button(onClick = { onAdd(product) }) { Text("加入购物车") }
}
}
```
---
## 5. 第四步:实现购物车页(重点:底部固定结算栏)
```kotlin
Box(Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp)) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(8.dp),
contentPadding = PaddingValues(bottom = 96.dp)
) {
lazyListItems(items) { item ->
// 购物车项:标题、数量+/-、移除
}
}
Card(
modifier = Modifier
.align(Alignment.BottomCenter)
.fillMaxWidth()
) {
// 总件数 + 合计 + 清空 + 结算
}
}
```
---
## 6. 第五步:图片加载与兜底
```kotlin
SubcomposeAsyncImage(
model = url,
loading = { LocalFallbackImage() },
error = { LocalFallbackImage() },
success = { SubcomposeAsyncImageContent() }
)
```
本地兜底图:
- `R.drawable.course_cover_default`
---
## 7. 第六步:把 UI 事件接到 ViewModel
- 搜索框:`vm.onQueryChange(...)`
- 重试:`vm.refreshProducts()`
- 加购:`vm.addToCart(productId)`
- 加减删清:`vm.increase/decrease/remove/clear`
- 结算:`vm.checkout()`
> 原则:UI 不直接写业务规则,只触发事件。
---
## 8. 最终验收
- [ ] 首页两列列表可滚动
- [ ] 点击卡片可进详情
- [ ] 购物车数量增减正常
- [ ] 底部结算栏固定不遮挡内容
- [ ] 图片加载失败显示本地兜底图
---
## 9. 说明
- 第6节主线:shared 网络与存储
- 第7节主线:MVVM 与业务整合
- 本文档:补齐“UI 怎么写”的落地路径