Flutter之Align
1、Align介紹
Align的作為一個參數(shù),設置子child的對齊方式,比如居中,左上,右下等多個對齊方向
2、部分源碼和功能
const Align({
Key key,
this.alignment = Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child,
}) : assert(alignment != null),
assert(widthFactor == null || widthFactor >= 0.0),
assert(heightFactor == null || heightFactor >= 0.0),
super(key: key, child: child);
/// 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);
我們知道最左上角的坐標是Alignment(-1.0, -1.0),中心坐標是Alignment(0.0, 0.0);然后最右下角坐標是Alignment(1.0, 1.0);
FractionalOffset對其封裝了大小范圍是(0~1),FractionalOffset(0, 0)對于左上角,F(xiàn)ractionalOffset(1, 1)對于右下角,F(xiàn)ractionalOffset(0.5, 0.5)對于中間坐標
FractionalOffset(0.5, 0.5) == Alignment(0.0,0.0) == Alignment.center
3、測試代碼
測試1
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'open url',
home: Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text('hello flutter'),
),
body: Align(
alignment: FractionalOffset(1, 0),
child: Container(
color: Colors.red,
width: 100,
height: 100,
),
),
),
);
}
測試2
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'open url',
home: Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text('hello flutter'),
),
body: Align(
alignment: FractionalOffset(0.5, 0),
child: Text("chenyu"),
),
),
);
}
}
4、運行效果
測試1結果
項目結構如下
測試2結果
作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學習,
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎入門進階人工智能(鏈接)