Container

介绍:

Container是一个组合类容器,它本身不对应具体的RenderObject,它是DecoratedBox、ConstrainedBox、Transform、Padding、Align等组件组合的一个多功能容器,所以我们只需通过一个Container组件可以实现同时需要装饰、变换、限制的场景。

列表参数:
Container({
  this.alignment, //控制child的对齐方式,如果container或者container父节点尺寸大于child的尺寸,这个属性设置会起作用,有很多种对齐方式。
  this.padding, //容器内补白,属于decoration的装饰范围
  Color color, // 背景色
  Decoration decoration, // 背景装饰
  Decoration foregroundDecoration, //前景装饰,如果foregroundDecoration设置的话,可能会遮盖color效果。
  double width,//容器的宽度
  double height, //容器的高度
  BoxConstraints constraints, //容器大小的限制条件
  this.margin,//容器外补白,不属于decoration的装饰范围
  this.transform, //变换
  this.child,
})
绘制过程:
  1. transform

  2. decoration

  3. paints the child //孩子组件

  4. paints the foregroundDecoration //前景

综合练习:
import 'package:flutter/material.dart';

void main() {
  runApp(FlutterContainer());
}

class FlutterContainer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
      child: Text(
        "我是一个Container",
        textDirection: TextDirection.ltr,
        style: TextStyle(color: Colors.green),
      ),
      width: 400,
      height: 400,
      constraints: BoxConstraints(
        minHeight: 200,
        minWidth: 200
      ),
      margin: const EdgeInsets.all(10.0), 
      alignment: Alignment.center,
      color: Colors.yellow,
    )
    );
  }
}

显示效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容