flutter 流式布局Wrap、Flow

流式布局
Wrap

在介紹Row和Colum時,如果子widget超出屏幕范圍,則會報溢出錯誤,如:

Row(
  children: <Widget>[
    Text("xxx"*100)
  ],
);
超出警告

可以看到,右邊溢出部分報錯。這是因為Row默認只有一行,如果超出屏幕不會折行。我們把超出屏幕顯示范圍會自動折行的布局稱為流式布局。Flutter中通過Wrap和Flow來支持流式布局,將上例中的Row換成Wrap后溢出部分則會自動折行。下面是Wrap的定義:

Wrap({
  ...
  this.direction = Axis.horizontal,
  this.alignment = WrapAlignment.start,
  this.spacing = 0.0,
  this.runAlignment = WrapAlignment.start,
  this.runSpacing = 0.0,
  this.crossAxisAlignment = WrapCrossAlignment.start,
  this.textDirection,
  this.verticalDirection = VerticalDirection.down,
  List<Widget> children = const <Widget>[],
})

我們可以看到Wrap的很多屬性在Row(包括Flex和Column)中也有,如direction、crossAxisAlignment、textDirection、verticalDirection等,這些參數意義是相同的,我們不再重復介紹,讀者可以查閱前面介紹Row的部分。讀者可以認為Wrap和Flex(包括Row和Column)除了超出顯示范圍后Wrap會折行外,其它行為基本相同。下面我們看一下Wrap特有的幾個屬性:

  • spacing:主軸方向子widget的間距
  • runSpacing:縱軸方向的間距
  • runAlignment:縱軸方向的對齊方式
    下面看一個示例子:
Widget _body(){
  return Wrap(
    spacing: 8.0, // 主軸(水平)方向間距
    runSpacing: 4.0, // 縱軸(垂直)方向間距
    alignment: WrapAlignment.center, //沿主軸方向居中
    children: <Widget>[
      new Chip(
        avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('A')),
        label: new Text('Hamilton'),
      ),
      new Chip(
        avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('M')),
        label: new Text('Lafayette'),
      ),
      new Chip(
        avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('H')),
        label: new Text('Mulligan'),
      ),
      new Chip(
        avatar: new CircleAvatar(backgroundColor: Colors.blue, child: Text('J')),
        label: new Text('Laurens'),
      ),
    ],
  );
}
Flow

我們一般很少會使用Flow,因為其過于復雜,需要自己實現子widget的位置轉換,在很多場景下首先要考慮的是Wrap是否滿足需求。Flow主要用于一些需要自定義布局策略或性能要求較高(如動畫中)的場景。

Flow有如下優點:
  • 性能好;Flow是一個對child尺寸以及位置調整非常高效的控件,Flow用轉換矩陣(transformation matrices)在對child進行位置調整的時候進行了優化:在Flow定位過后,如果child的尺寸或者位置發生了變化,在FlowDelegate中的paintChildren()方法中調用context.paintChild 進行重繪,而context.paintChild在重繪時使用了轉換矩陣(transformation matrices),并沒有實際調整Widget位置。
  • 靈活;由于我們需要自己實現FlowDelegate的paintChildren()方法,所以我們需要自己計算每一個widget的位置,因此,可以自定義布局策略。
缺點:
  • 使用復雜.
  • 不能自適應子widget大小,必須通過指定父容器大小或實現TestFlowDelegate的getSize返回固定大小。
    示例:
    我們對六個色塊進行自定義流式布局并實現TestFlowDelegate::
class _home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _homeState();
  }
}
class _homeState extends State<_home>{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("title"),
        centerTitle: true,
      ),
      body: Flow(
        delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
        children: <Widget>[
          new Container(width: 80.0, height:80.0, color: Colors.red,),
          new Container(width: 80.0, height:80.0, color: Colors.green,),
          new Container(width: 80.0, height:80.0, color: Colors.blue,),
          new Container(width: 80.0, height:80.0,  color: Colors.yellow,),
          new Container(width: 80.0, height:80.0, color: Colors.brown,),
          new Container(width: 80.0, height:80.0,  color: Colors.purple,),
        ],
      ),
    );
  }
}

class TestFlowDelegate extends FlowDelegate {
  EdgeInsets margin = EdgeInsets.zero;
  TestFlowDelegate({this.margin});
  @override
  void paintChildren(FlowPaintingContext context) {
    var x = margin.left;
    var y = margin.top;
    //計算每一個子widget的位置
    for (int i = 0; i < context.childCount; i++) {
      var w = context.getChildSize(i).width + x + margin.right;
      if (w < context.size.width) {
        context.paintChild(i,
            transform: new Matrix4.translationValues(
                x, y, 0.0));
        x = w + margin.left;
      } else {
        x = margin.left;
        y += context.getChildSize(i).height + margin.top + margin.bottom;
        //繪制子widget(有優化)
        context.paintChild(i,
            transform: new Matrix4.translationValues(
                x, y, 0.0));
        x += context.getChildSize(i).width + margin.left + margin.right;
      }
    }
  }
  getSize(BoxConstraints constraints){
    //指定Flow的大小
    return Size(double.infinity,200.0);
  }
  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }
}

可以看到我們主要的任務就是實現paintChildren,它的主要任務是確定每個子widget位置。由于Flow不能自適應子widget的大小,我們通過在getSize返回一個固定大小來指定Flow的大小。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。