CSS → Flutter 对照手册

19 阅读4分钟

CSS → Flutter 对照手册

核心思维转变:CSS 是给元素加属性,Flutter 是用 Widget 嵌套实现布局


一、盒模型

CSSFlutter说明
width / heightContainer(width, height) / SizedBox固定尺寸
max-width / min-widthConstrainedBox(constraints)尺寸约束
padding: 16pxEdgeInsets.all(16)四边相同
padding: 10px 20pxEdgeInsets.symmetric(vertical:10, horizontal:20)上下/左右
padding: 10px 20px 30px 40pxEdgeInsets.fromLTRB(40, 10, 20, 30)上右下左
marginContainer(margin: ...)外边距
background: blueContainer(color: Colors.blue)纯色背景
border-radius: 20pxBorderRadius.circular(20)圆角
border-radius: 10px 20px 30px 40pxBorderRadius.only(topLeft: ..., ...)单独圆角
border: 3px solid yellowBorder.all(width:3, color:Colors.yellow)边框
border-top: 3px solid redBorder(top: BorderSide(width:3, color:Colors.red))单边边框
box-shadowBoxDecoration(boxShadow: [BoxShadow(...)])阴影
overflow: hiddenClipRRect / ClipOval裁剪溢出
width: fit-contentIntrinsicWidth包裹内容宽度
width: 100%SizedBox(width: double.infinity) / Expanded撑满父级

⚠️ Containercolordecoration 互斥,用了 decoration 就把颜色写进 BoxDecoration(color:...)


二、Flex 布局

CSSFlutter说明
display: flex; flex-direction: rowRow横向排列
display: flex; flex-direction: columnColumn纵向排列
justify-contentmainAxisAlignment主轴对齐
align-itemscrossAxisAlignment交叉轴对齐
flex: 1Expanded占满剩余空间
flex: 2 (比例)Expanded(flex: 2)按比例分配
flex-grow 不强制填满Flexible弹性但不强制
flex-wrap: wrapWrap自动换行
gap / column-gapWrap(spacing: 8)横向间距
row-gapWrap(runSpacing: 8)纵向间距
align-selfAlign 包裹单个子项单独控制对齐

justify-content 对照

CSSFlutter
flex-startMainAxisAlignment.start
flex-endMainAxisAlignment.end
centerMainAxisAlignment.center
space-betweenMainAxisAlignment.spaceBetween
space-aroundMainAxisAlignment.spaceAround
space-evenlyMainAxisAlignment.spaceEvenly

align-items 对照

CSSFlutter
flex-startCrossAxisAlignment.start
flex-endCrossAxisAlignment.end
centerCrossAxisAlignment.center
stretchCrossAxisAlignment.stretch
baselineCrossAxisAlignment.baseline

三、定位

CSSFlutter说明
position: relative(父容器)Stack绝对定位容器
position: absolute; top/left/right/bottomPositioned(top, left, right, bottom)绝对定位子项
position: absolute; top:50%; transform:translate(-50%,-50%)Positioned.fill + Align(Alignment.center)绝对居中
z-indexStack 中越靠后的 child 层级越高层叠顺序
position: stickySliverPersistentHeader(pinned: true)滚动置顶
position: fixedScaffoldfloatingActionButton / bottomNavigationBar固定在屏幕

四、文字样式

CSSFlutter说明
font-size: 20pxTextStyle(fontSize: 20)字号
color: redTextStyle(color: Colors.red)颜色
font-weight: boldTextStyle(fontWeight: FontWeight.bold)粗体
font-weight: 300TextStyle(fontWeight: FontWeight.w300)字重
font-style: italicTextStyle(fontStyle: FontStyle.italic)斜体
letter-spacing: 2pxTextStyle(letterSpacing: 2)字间距
line-height: 1.5TextStyle(height: 1.5)行高
text-decoration: underlineTextStyle(decoration: TextDecoration.underline)下划线
text-decoration: line-throughTextStyle(decoration: TextDecoration.lineThrough)删除线
text-align: centerText(textAlign: TextAlign.center)对齐
overflow: ellipsisText(overflow: TextOverflow.ellipsis, maxLines: 1)超出省略
text-transform: uppercasetext.toUpperCase()大写
text-shadowTextStyle(shadows: [Shadow(...)])文字阴影

五、背景与装饰

CSSFlutter说明
background: blueBoxDecoration(color: Colors.blue)纯色背景
background: linear-gradient(to right, red, blue)LinearGradient(begin: Alignment.centerLeft, end: Alignment.centerRight, colors: [...])线性渐变
background: linear-gradient(135deg, red, blue)LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [...])对角渐变
background: radial-gradient(circle, red, blue)RadialGradient(colors: [...])径向渐变
background-image: url(...)DecorationImage(image: AssetImage(...))背景图
background-size: coverDecorationImage(fit: BoxFit.cover)背景图填充

六、变换

CSSFlutter说明
transform: rotate(0.05rad)Transform.rotate(angle: 0.05)旋转(弧度)
transform: rotate(45deg)Transform.rotate(angle: 45 * pi / 180)旋转(角度转弧度)
transform: scale(1.5)Transform.scale(scale: 1.5)缩放
transform: translate(20px, 10px)Transform.translate(offset: Offset(20, 10))平移
transform-originTransform(alignment: Alignment.topLeft, ...)变换原点

七、显示与隐藏

CSSFlutter说明
display: none(不占位)Visibility(visible: false) / 三元返回 SizedBox.shrink()移除占位
visibility: hidden(占位)Visibility(visible: false, maintainSize: true, maintainState: true, maintainAnimation: true)保留占位
opacity: 0.5Opacity(opacity: 0.5)透明度
pointer-events: noneIgnorePointer / AbsorbPointer禁用触摸

八、滚动

CSSFlutter说明
overflow: scroll(单个子项)SingleChildScrollView单子滚动
overflow: scroll(列表)ListView列表滚动
overflow-x: scrollSingleChildScrollView(scrollDirection: Axis.horizontal)横向滚动
scroll-snapPageView分页滑动
虚拟列表ListView.builder懒加载长列表
网格滚动GridView网格列表
混合滚动CustomScrollView + Sliver*复杂滚动

九、响应式

CSSFlutter说明
@media (max-width: 600px)MediaQuery.of(context).size.width < 600屏幕宽度判断
vw / vhMediaQuery.of(context).size.width/height视口尺寸
grid-template-columns: repeat(auto-fill, minmax(150px,1fr))LayoutBuilder + 动态 crossAxisCount自适应列数
父级约束响应LayoutBuilder(builder: (context, constraints) {...})获取父级约束

十、经典布局速查

布局名CSS 方案Flutter 方案
圣杯布局float / flexColumn + Row + Expanded
双飞翼布局marginRow + Expanded(主内容优先)
粘性底部min-height: 100vh + margin-top: autoColumn + Expanded
水平垂直居中flex + justify/align: centerCenter / Align
瀑布流column-count / JS 计算双列 Column 交叉填充
底部导航固定定位BottomNavigationBar + IndexedStack
侧边抽屉transform: translateXDrawer
折叠头部position: sticky + JSSliverAppBar
主从布局@media 切换MediaQuery + 条件渲染
固定头尾中间滚动flex-column + overflow: autoColumn + Expanded + ListView

十一、最大的思维差异

CSS 思维Flutter 思维
一个元素加多个属性多个 Widget 嵌套组合
样式可以全局继承样式靠 ThemeData 统一管理
父级影响子级布局父 Widget 向子 Widget 传递约束(constraints)
z-index 控制层级Stack 中越靠后的 child 层级越高
媒体查询响应式MediaQuery.of(context).size 获取屏幕尺寸
display: none 隐藏元素三元表达式直接移除 Widget
全局 class 复用样式封装成独立 Widget 复用
CSS 变量 var(--color)Theme.of(context).colorScheme.primary
伪元素 ::before / ::afterStack + Positioned 叠加 Widget
transition 过渡动画AnimatedContainer / AnimatedOpacity 等 Animated* 系列

十二、Widget 选择决策树

需要样式装饰(背景/圆角/边框/阴影)?
  └─ 是 → BoxDecoration(放在 Container 或 DecoratedBox 里)

需要布局子元素?
  ├─ 横向 → Row
  ├─ 纵向 → Column
  ├─ 叠加 → Stack
  └─ 网格 → GridView

需要滚动?
  ├─ 单个内容 → SingleChildScrollView
  ├─ 列表 → ListView.builder
  └─ 复杂滚动(折叠头/置顶) → CustomScrollView + Sliver*

只需固定尺寸,不需要装饰?
  └─ SizedBox(比 Container 更轻量)

只需内边距?
  └─ Padding(比 Container 更语义化)

只需对齐?
  └─ Align / Center