Flutter---Container

概念

一个多功能容器,可以设置背景、边距、大小、装饰等

核心属性

Dart 复制代码
child	Widget?	容器内的子组件	
width	double?	容器宽度	
height	double?	容器高度	
color	Color?	背景色	
padding	EdgeInsetsGeometry?	内边距(内容到边框)	
margin	EdgeInsetsGeometry?	外边距(容器到外部)	
decoration	BoxDecoration?	装饰(圆角、阴影、渐变等)	
alignment	AlignmentGeometry?	子组件对齐方式	
constraints	BoxConstraints?	额外约束限制	
transform	Matrix4?	变换(旋转、缩放)	
clipBehavior	Clip?	裁剪行为	

基本用法

Dart 复制代码
        Container(
            width:100, //宽
            height: 100,//高
            //外边距:有三个参数all(全部),only(单边),symmetric(水平或垂直),
            margin: EdgeInsets.symmetric(horizontal: 8),
            padding: EdgeInsets.all(8),//内边距的用法和外边距一样
            alignment: Alignment(0, 0),//对齐方式
            decoration: BoxDecoration(
              //颜色,withOpacity是用来设置透明度的,值的范围(0.0~1.0)
              color: Color(0xFF5091C9).withOpacity(0.3),
              //圆角效果
              borderRadius: BorderRadius.circular(20),
              //边框
              border: Border.all(color: Colors.black,width: 5),
              boxShadow: [                    // 阴影
                  BoxShadow(
                    color: Colors.black26, //颜色
                    blurRadius: 8, //模糊程度
                    offset: Offset(2, 2),//偏移
                    spreadRadius:2,//扩散范围
                  ),
                ],
              //设置形状
             // shape: BoxShape.circle, //形状和圆角不能同时设置
            ),
            child: Text(
               "内容"
            ),
          )

不同方式的圆角

Dart 复制代码
// 不同角的圆角
Container(
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(20),
      topRight: Radius.circular(10),
      bottomLeft: Radius.circular(5),
      bottomRight: Radius.circular(15),
    ),
  ),
  child: Text('四个角不同圆角'),
)

线性渐变LinearGradient

Dart 复制代码
// 线性渐变
Container(
  width: 200,
  height: 100,
  decoration: BoxDecoration(
    gradient: LinearGradient(
      begin: Alignment.topLeft,
      end: Alignment.bottomRight,
      colors: [
        Colors.blue,
        Colors.purple,
      ],
    ),
    borderRadius: BorderRadius.circular(12),
  ),
  child: Center(child: Text('渐变色', style: TextStyle(color: Colors.white))),
)

径向渐变(从中心向外)RadialGradient

Dart 复制代码
// 径向渐变(从中心向外)
Container(
  decoration: BoxDecoration(
    gradient: RadialGradient(
      center: Alignment.center,
      radius: 0.8,
      colors: [
        Colors.yellow,
        Colors.orange,
        Colors.red,
      ],
    ),
  ),
  child: Text('径向渐变'),
)

设置图片背景

Dart 复制代码
Container(
  width: 200,
  height: 150,
  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage('assets/images/background.png'),
      fit: BoxFit.cover,  // 覆盖
    ),
    borderRadius: BorderRadius.circular(12),
  ),
  child: Center(
    child: Text(
      '背景图',
      style: TextStyle(color: Colors.white, fontSize: 20),
    ),
  ),
)

对齐方式

Dart 复制代码
Container(
  width: 200,
  height: 200,
  color: Colors.grey[200],
  alignment: Alignment.center,  // 居中
  child: Container(
    width: 50,
    height: 50,
    color: Colors.blue,
  ),
)


// 常用的对齐常量
alignment: Alignment.topLeft      // 左上
alignment: Alignment.topCenter    // 上中
alignment: Alignment.topRight     // 右上
alignment: Alignment.centerLeft   // 左中
alignment: Alignment.center       // 正中心(默认)
alignment: Alignment.centerRight  // 右中
alignment: Alignment.bottomLeft   // 左下
alignment: Alignment.bottomCenter // 下中
alignment: Alignment.bottomRight  // 右下

自定义偏移

Dart 复制代码
// 坐标范围:-1.0 ~ 1.0
Container(
  alignment: Alignment(-0.5, 0.3),  // 左偏50%,下偏30%
  child: Container(width: 50, height: 50, color: Colors.blue),
)

约束控制

Dart 复制代码
Container(
  constraints: BoxConstraints(
    minWidth: 100,
    maxWidth: 300,
    minHeight: 50,
    maxHeight: 150,
  ),
  color: Colors.blue,
  child: Text('尺寸限制'),
)
相关推荐
落叶飘飘s13 小时前
餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
flutter·华为·harmonyos
起司喵喵16 小时前
Flutter-MacOS桌面OS系统|flutter.+window_manager客户端OS模板
flutter·macos·策略模式
Wuxiaoming1351 天前
flutter app的logo和splash logo尺寸
flutter
GitLqr1 天前
深入理解 Flutter 架构:从 Widget 到 GPU 像素的全链路解析
flutter·面试·dart
SoaringHeart1 天前
Flutter进阶|最佳实践:组件内阴影实现
前端·flutter
玛艾露贝1 天前
Supabase云同步架构:Flutter应用的数据同步策略
flutter·架构
魔力女仆2 天前
Flutter 复杂拖拽排序实战:同源排序 + 跨容器拖拽完整落地
flutter
杉氧2 天前
状态管理大对决:在 Flutter 实战中如何优雅地管理共享状态?
android·前端·flutter
恋猫de小郭2 天前
Flutter Starling : 用 Swift和 Flutter 写了一个 Linux 桌面系统,震惊我一整年
android·前端·flutter