Flutter Alignment FractionalOffset AlignmentDirectional

Alignment
 

const Alignment(this.x, this.y)
    : assert(x != null),
      assert(y != null);

参数x:-1:最左边 1:最右边 0:中间
参数y:-1:最上边 1:最下边 0:中间

比如:Alignment(0,0)代表控件的中心
坐标系如下图:

在这里插入图片描述
 

Container(
          width: 300,
          height: 100,
          color: Color(0xFFFF0000),
          alignment: Alignment(1,0),
          child: Text('Alignment(1,0)',
          textDirection: TextDirection.rtl,),
        )

效果:

在这里插入图片描述

 Alignment定义了我们常用的位置:

  /// The top left corner.
  static const Alignment topLeft = Alignment(-1.0, -1.0);

  /// The center point along the top edge.
  static const Alignment topCenter = Alignment(0.0, -1.0);

  /// The top right corner.
  static const Alignment topRight = Alignment(1.0, -1.0);

  /// The center point along the left edge.
  static const Alignment centerLeft = Alignment(-1.0, 0.0);

  /// The center point, both horizontally and vertically.
  static const Alignment center = Alignment(0.0, 0.0);

  /// The center point along the right edge.
  static const Alignment centerRight = Alignment(1.0, 0.0);

  /// The bottom left corner.
  static const Alignment bottomLeft = Alignment(-1.0, 1.0);

  /// The center point along the bottom edge.
  static const Alignment bottomCenter = Alignment(0.0, 1.0);

  /// The bottom right corner.
  static const Alignment bottomRight = Alignment(1.0, 1.0);

FractionalOffset

FractionalOffset继承Alignment,他们2个区别就是坐标系不一样,Alignment的原点是中心,而FractionalOffset原点是左上角。

在这里插入图片描述

Container(
          width: 300,
          height: 100,
          color: Color(0xFFFF0000),
          alignment: FractionalOffset(0,0),
          child: Text('FractionalOffset(0,0)',
          textDirection: TextDirection.rtl,),
        ),

效果:

在这里插入图片描述
FractionalOffset定义了我们常用的位置:

/// The top left corner.
  static const FractionalOffset topLeft = FractionalOffset(0.0, 0.0);

  /// The center point along the top edge.
  static const FractionalOffset topCenter = FractionalOffset(0.5, 0.0);

  /// The top right corner.
  static const FractionalOffset topRight = FractionalOffset(1.0, 0.0);

  /// The center point along the left edge.
  static const FractionalOffset centerLeft = FractionalOffset(0.0, 0.5);

  /// The center point, both horizontally and vertically.
  static const FractionalOffset center = FractionalOffset(0.5, 0.5);

  /// The center point along the right edge.
  static const FractionalOffset centerRight = FractionalOffset(1.0, 0.5);

  /// The bottom left corner.
  static const FractionalOffset bottomLeft = FractionalOffset(0.0, 1.0);

  /// The center point along the bottom edge.
  static const FractionalOffset bottomCenter = FractionalOffset(0.5, 1.0);

  /// The bottom right corner.
  static const FractionalOffset bottomRight = FractionalOffset(1.0, 1.0);

AlignmentDirectional

AlignmentDirectional 的坐标系和Alignment比较像,原点在中心,不过AlignmentDirectional的起始位置和书写(TextDirection)方向有关

在这里插入图片描述

在这里插入图片描述 

 

demo:

Container(
      width: 300,
      height: 100,
      color: Color.fromARGB(255, 66, 165, 245),
      child: new Text(
        "Flutter Cheatsheet",
        textDirection: TextDirection.ltr,
        style: TextStyle(

        ),
      ),
      alignment: AlignmentDirectional.topStart,
    );

二真机上的效果:

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值