Flutter之Align
生活随笔
收集整理的這篇文章主要介紹了
Flutter之Align
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、Align介紹
Align的作為一個參數,設置子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)對于左上角,FractionalOffset(1, 1)對于右下角,FractionalOffset(0.5, 0.5)對于中間坐標
FractionalOffset(0.5, 0.5) == Alignment(0.0,0.0) == Alignment.center?
?
?
?
?
?
?
?
3、測試代碼
測試1
@overrideWidget 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
@overrideWidget 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結果
?
?
?
總結
以上是生活随笔為你收集整理的Flutter之Align的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android之解决底部4个TextVi
- 下一篇: Flutter之Center