You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中设计带侧边黑色块的卡片/容器视图?

行程卡片布局实现思路

下面是几种实现带左侧和末端黑色色块卡片的实用方案,都是前端开发中常用的手法:

方案1:CSS伪元素实现(无额外DOM元素)

这种方法不需要新增HTML节点,利用CSS伪元素生成色块,适合追求简洁结构的场景。

HTML结构

<div class="trip-card">
  <div class="card-content">
    <h3>行程标题</h3>
    <p>行程详情描述...</p>
  </div>
</div>

CSS样式

.trip-card {
  position: relative;
  width: 300px;
  padding: 16px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden; /* 限制色块不超出卡片范围 */
}

/* 左侧黑色竖条 */
.trip-card:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 4px;
  height: 100%;
  background-color: #000;
}

/* 末端(右下角)黑色色块 */
.trip-card:after {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  width: 60px;
  height: 8px;
  background-color: #000;
}

.card-content {
  margin-left: 8px; /* 避开左侧色块,防止内容被遮挡 */
}

方案2:嵌套容器+Flex布局

如果需要给色块添加交互逻辑或更复杂的样式,嵌套容器的方式结构更直观,便于后期维护。

HTML结构

<div class="trip-card">
  <div class="left-bar"></div>
  <div class="card-content">
    <h3>行程标题</h3>
    <p>行程详情描述...</p>
  </div>
  <div class="end-bar"></div>
</div>

CSS样式

.trip-card {
  display: flex;
  align-items: stretch;
  width: 300px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  position: relative;
}

.left-bar {
  width: 4px;
  background-color: #000;
  border-radius: 8px 0 0 8px; /* 和卡片圆角对齐 */
}

.card-content {
  flex: 1;
  padding: 16px;
}

.end-bar {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 60px;
  height: 8px;
  background-color: #000;
  border-radius: 0 0 8px 0;
}

方案3:Grid布局(适合复杂布局扩展)

如果卡片后续需要新增更多区块,Grid布局的可扩展性更强,能直接划分区域定位元素。

HTML结构

<div class="trip-card">
  <div class="left-bar"></div>
  <div class="card-content">
    <h3>行程标题</h3>
    <p>行程详情描述...</p>
  </div>
  <div class="end-bar"></div>
</div>

CSS样式

.trip-card {
  display: grid;
  grid-template-columns: 4px 1fr;
  width: 300px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}

.left-bar {
  background-color: #000;
}

.card-content {
  padding: 16px;
}

.end-bar {
  grid-column: 2;
  justify-self: end;
  align-self: end;
  width: 60px;
  height: 8px;
  background-color: #000;
}

如果用React/Vue等框架的组件库(比如Ant Design、Element UI),只需要给组件库的Card外层容器添加相对定位,再套用上述伪元素或色块容器的样式即可。

内容的提问来源于stack exchange,提问作者Darshan Kachhadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 12:20:13