有資料之後,我們需要把這些寶可夢紀錄漂漂亮亮地展示出來。我們的系統會用手機查,也會用電腦查,因此我們將使用 MUI 的 Grid 響應式排版系統,並將每一筆紀錄包裝在精美的 Card 元件裡。
MUI 的 Grid 系統使用了 12 欄 (12-column) 的網格系統,我們可以透過斷點設定,決定在不同螢幕寬度下,一張卡片要佔據幾個網格:
| 斷點屬性 | 代表螢幕寬度 | 我們設定的佔比 | 實際排版效果 |
|---|---|---|---|
xs |
極小螢幕 (手機) | xs={12} (佔滿 12 格) |
一排顯示 1 張卡片 |
sm |
小螢幕 (大手機/直立平板) | sm={6} (佔據一半) |
一排顯示 2 張卡片 |
md |
中等螢幕 (橫向平板/小筆電) | md={4} (佔據 1/3) |
一排顯示 3 張卡片 |
lg |
大螢幕 (桌上型螢幕) | lg={3} (佔據 1/4) |
一排顯示 4 張卡片 |
在列表元件中,我們把昨天的純文字升級成卡片,並透過 Grid 系統自動換行:
import { Grid, Card, CardContent, Typography, Chip, Box } from '@mui/material';
// 假設這是在 map 迴圈中
<Grid container spacing={3}>
{holdings.map((pokemon) => (
// 套用上面分析的 RWD 斷點魔法!
<Grid item xs={12} sm={6} md={4} lg={3} key={pokemon.holding_id}>
<Card sx={{ borderRadius: 3, boxShadow: '0 4px 12px rgba(0,0,0,0.1)' }}>
<CardContent>
<Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 1 }}>
<Typography variant="h6" fontWeight="bold">
{pokemon.name_zh}
</Typography>
{/* 標示是否為亮晶晶狀態 */}
{pokemon.is_lucky && <Chip label="亮晶晶" color="warning" size="small" />}
</Box>
<Typography variant="body2" color="text.secondary">
性別: {pokemon.gender || '無'}
</Typography>
<Typography variant="body2" sx={{ mt: 1 }}>
備註: {pokemon.notes || '無'}
</Typography>
</CardContent>
</Card>
</Grid>
))}
</Grid>

電腦版的庫存管理網頁畫面。

手機版的單欄排版畫面,完美展示 RWD 效果。
圖鑑的展示介面完成了,看著一張張漂漂亮亮的卡片排好,非常有成就感!接下來我們要補上重要的拼圖:提供一個表單對話框,讓玩家自己新增捕獲紀錄!