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

如何在Flutter中实现可横向滚动的两行视频推荐网格

实现两行网格横向滚动的视频推荐区域

视频推荐区域示例

针对eLearning平台的视频推荐区域需求,这里提供一种基于CSS Grid的实现方案,能轻松实现两行网格布局+横向滚动的效果:

HTML结构

<div class="video-recommendation-container">
  <!-- 视频卡片1 -->
  <div class="video-card">
    <div class="video-thumbnail"></div>
    <div class="video-info">
      <h3>前端开发入门教程</h3>
      <p>张三 / 45分钟</p>
    </div>
  </div>
  <!-- 视频卡片2 -->
  <div class="video-card">
    <div class="video-thumbnail"></div>
    <div class="video-info">
      <h3>Python数据分析实战</h3>
      <p>李四 / 1小时20分</p>
    </div>
  </div>
  <!-- 可根据需求重复添加更多卡片 -->
</div>

CSS样式

.video-recommendation-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  grid-auto-flow: column; /* 强制元素横向排列 */
  grid-template-rows: repeat(2, auto); /* 固定两行布局 */
  gap: 16px; /* 卡片间的间距 */
  overflow-x: auto; /* 开启横向滚动 */
  padding: 16px 0;
  max-width: 100%;
}

.video-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 280px; /* 和grid的minmax值保持一致 */
}

.video-thumbnail {
  width: 100%;
  height: 150px;
  background-color: #e0e0e0;
  border-radius: 8px;
}

.video-info h3 {
  font-size: 14px;
  margin: 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.video-info p {
  font-size: 12px;
  color: #666;
  margin: 0;
}

/* 可选:美化横向滚动条 */
.video-recommendation-container::-webkit-scrollbar {
  height: 6px;
}

.video-recommendation-container::-webkit-scrollbar-thumb {
  background-color: #999;
  border-radius: 3px;
}

核心要点说明

  • grid-auto-flow: column 是实现横向排列的关键,让网格元素沿横轴方向填充,超出容器宽度后触发滚动
  • grid-template-rows: repeat(2, auto) 固定布局为两行,卡片会自动填充到第二行
  • minmax(280px, 1fr) 确保每个卡片有最小宽度,同时在空间允许时自适应拉伸
  • 给标题添加多行溢出省略,避免标题过长破坏布局

内容的提问来源于stack exchange,提问作者Pawan Bhayde

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 23:35:30