Flutter Align


简介

Align控件即对齐控件,能将子控件所指定方式对齐,并根据子控件的大小调整自己的大小。根据自己需求,进行控件对齐

基本用法

alignment → AlignmentGeometry

  • 要对齐右下方的框,那么对这个框对要求会比对子控件更加严肃的约束,使用:Alignment.bottomRight
  • 同理:Alignment.center,Alignment.bottomLeft,Alignment.topLeft等

widthFactor / heightFactor → double

  • 如果widthFactor / heightFactor 为空,并且外部无任何约束,child控件大小默认,那么这个控件将根据自身尺寸最大化
  • 如果widthFactor / heightFactor 不为空,并且外部无约束,align将匹配对应的child尺寸
  • ex:widthFactor / heightFactor 为2.0;那么widget的宽高为child宽高的两倍
  • 如果widthFactor / heightFactor 为空,并且外部无约束,child控件将会设置自身大小

实例演示

class AlignAlignment extends StatelessWidget {
  final Alignment status;
  final String dec;

  const AlignAlignment( this.status,  this.dec) : super();

  @override
  Widget build(BuildContext context) {
    return  Container(
      color: Color(0xffd81b60),
      width: 90.0,
      height: 50.0,
      child:  Align(
        alignment: status,
        child:  Text(
          dec,
          style: TextStyle(fontSize: 12.0, color: Color(0xffffffff)),
        ),
      ),
    );
  }
}

class AlignFactor extends StatelessWidget {
  final Alignment status;
  final double wFactor;
  final double hFactor;
  final String dec;

  const AlignFactor( this.status,  this.wFactor,
       this.hFactor,  this.dec)
      : super();

  @override
  Widget build(BuildContext context) {
    return  Container(
      margin:  EdgeInsets.only(top: 10.0, bottom: 10.0),
      color: Color(0xffd81b60),
      child:  Align(
        alignment: status,
        widthFactor: wFactor,
        heightFactor: hFactor,
        child: Container(
          color: Color(0xfff06292),
          width: 100.0,
          height: 50.0,
          child: Text(
            dec,
            style: TextStyle(color: Color(0xffffffff)),
          ),
        ),
      ),
    );
  }
}