docs(ui): make UI guide precise enough for line-by-line classroom follow
This commit is contained in:
1 parent
ec1bcfa47e
commit
b98af4ed17
2 files changed
+328
-89
No files matched your search
+1
-1
@@ -12,7 +12,7 @@
|
||||
- 内容:第7节实操讲稿(MVVM重构+全流程整合)
|
||||
|
||||
4. `UI_参考资料_Android.md`
|
||||
- 内容:Android UI 实现指南(从页面骨架到列表/详情/购物车逐步实现)
|
||||
- 内容:Android UI 逐步照写指南(从 0 到完整页面)
|
||||
|
||||
5. `第6_7节课程大纲_KMP商城_网络存储与整合.md`
|
||||
- 内容:第6-7节目标、节奏、产出、SkillMap映射
|
||||
|
||||
+327
-88
@@ -1,107 +1,304 @@
|
||||
# KMP Mini Shopping UI 实现指南(Android)
|
||||
# KMP Mini Shopping UI 实现指南(Android|逐步照写版)
|
||||
|
||||
> 这份文档解决“知道架构但不会落 UI”的问题。按步骤写,可从 0 到 1 完成页面。
|
||||
> 目标:这份文档按“最小可运行 -> 完整效果”拆到每一步。学员照着写即可完成 UI。
|
||||
> 前置:`ShopViewModel` 已存在(第7节已完成)。
|
||||
|
||||
---
|
||||
|
||||
## 1. 先做哪些文件
|
||||
## 0. 准备(先不写业务)
|
||||
|
||||
### 0.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`
|
||||
|
||||
### 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 能起来。
|
||||
|
||||
---
|
||||
|
||||
## 2. 第一步:先搭页面骨架(Scaffold)
|
||||
## 1. 第一步:搭 Scaffold 壳子(顶栏 + 底栏 + 内容区)
|
||||
|
||||
先实现三块:
|
||||
- `TopAppBar`
|
||||
- `BottomBar(Home/Cart)`
|
||||
- `Content` 按状态切换页面
|
||||
把 `App()` 改成下面:
|
||||
|
||||
```kotlin
|
||||
@Composable
|
||||
fun App() {
|
||||
val vm: ShopViewModel = viewModel()
|
||||
val vm: ShopViewModel = androidx.lifecycle.viewmodel.compose.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(...)
|
||||
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 显示
|
||||
- 中间出现“内容区”
|
||||
|
||||
---
|
||||
|
||||
## 3. 第二步:实现首页商品列表(两列瀑布流)
|
||||
## 2. 第二步:建三个页面函数(先空实现)
|
||||
|
||||
### 3.1 列表组件
|
||||
在 `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)
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
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. 第三步:实现详情页
|
||||
## 5. 第五步:实现详情页
|
||||
|
||||
改 `ProductDetailScreen` 为:
|
||||
|
||||
```kotlin
|
||||
@Composable
|
||||
private fun ProductDetailScreen(
|
||||
product: Product?,
|
||||
product: com.example.minishop.model.Product?,
|
||||
quantity: Int,
|
||||
onAdd: (Product) -> Unit
|
||||
onAdd: (com.example.minishop.model.Product) -> Unit
|
||||
) {
|
||||
if (product == null) return
|
||||
if (product == null) {
|
||||
androidx.compose.material3.Text("商品不存在")
|
||||
return
|
||||
}
|
||||
|
||||
Column(Modifier.fillMaxSize().padding(16.dp)) {
|
||||
androidx.compose.foundation.layout.Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
ProductCover(product.imageUrl)
|
||||
Text(product.title)
|
||||
Text("¥${product.price}")
|
||||
Text(product.description)
|
||||
if (quantity > 0) Text("当前已加购:$quantity")
|
||||
Button(onClick = { onAdd(product) }) { Text("加入购物车") }
|
||||
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) }
|
||||
)
|
||||
```
|
||||
|
||||
### 本步验收
|
||||
- 点商品进入详情
|
||||
- 详情页可加购
|
||||
|
||||
---
|
||||
|
||||
## 5. 第四步:实现购物车页(重点:底部固定结算栏)
|
||||
## 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)) {
|
||||
@@ -109,9 +306,7 @@ Box(Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp)) {
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
contentPadding = PaddingValues(bottom = 96.dp)
|
||||
) {
|
||||
lazyListItems(items) { item ->
|
||||
// 购物车项:标题、数量+/-、移除
|
||||
}
|
||||
// 购物车项
|
||||
}
|
||||
|
||||
Card(
|
||||
@@ -119,53 +314,97 @@ Box(Modifier.fillMaxSize().padding(horizontal = 16.dp, vertical = 12.dp)) {
|
||||
.align(Alignment.BottomCenter)
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
// 总件数 + 合计 + 清空 + 结算
|
||||
// 总件数、总价、清空、结算
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 本步验收
|
||||
- 结算栏固定在底部
|
||||
- 列表最后一项不会被遮挡
|
||||
|
||||
---
|
||||
|
||||
## 6. 第五步:图片加载与兜底
|
||||
## 8. 第八步:补图片组件(失败兜底)
|
||||
|
||||
```kotlin
|
||||
SubcomposeAsyncImage(
|
||||
model = url,
|
||||
loading = { LocalFallbackImage() },
|
||||
error = { LocalFallbackImage() },
|
||||
success = { SubcomposeAsyncImageContent() }
|
||||
@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
|
||||
)
|
||||
```
|
||||
|
||||
本地兜底图:
|
||||
- `R.drawable.course_cover_default`
|
||||
ViewModel 内做 `trim()` 过滤。
|
||||
|
||||
---
|
||||
|
||||
## 7. 第六步:把 UI 事件接到 ViewModel
|
||||
## 10. 最终对照(和当前项目一致)
|
||||
|
||||
- 搜索框:`vm.onQueryChange(...)`
|
||||
- 重试:`vm.refreshProducts()`
|
||||
- 加购:`vm.addToCart(productId)`
|
||||
- 加减删清:`vm.increase/decrease/remove/clear`
|
||||
- 结算:`vm.checkout()`
|
||||
最终应达到:
|
||||
- 首页两列瀑布流
|
||||
- 详情页
|
||||
- 购物车增减删清
|
||||
- 底部固定结算栏
|
||||
- 图片兜底
|
||||
- 搜索过滤
|
||||
- 事件全部走 `ShopViewModel`
|
||||
|
||||
> 原则:UI 不直接写业务规则,只触发事件。
|
||||
|
||||
---
|
||||
|
||||
## 8. 最终验收
|
||||
|
||||
- [ ] 首页两列列表可滚动
|
||||
- [ ] 点击卡片可进详情
|
||||
- [ ] 购物车数量增减正常
|
||||
- [ ] 底部结算栏固定不遮挡内容
|
||||
- [ ] 图片加载失败显示本地兜底图
|
||||
|
||||
---
|
||||
|
||||
## 9. 说明
|
||||
|
||||
- 第6节主线:shared 网络与存储
|
||||
- 第7节主线:MVVM 与业务整合
|
||||
- 本文档:补齐“UI 怎么写”的落地路径
|
||||
如果学员跟到第10步还有问题,直接对照:
|
||||
- `App.kt`
|
||||
- `ShopViewModel.kt`
|
||||
Reference in new issue
Block a user