Flutter 布局類組件:流式布局(Wrap和Flow)


前言

把超出屏幕顯示范圍會自動折行的布局稱為流式布局。Flutter中通過Wrap和Flow來支持流式布局,將Row換成Wrap后溢出部分則會自動折行。

Wrap

接口描述

Wrap({
    Key key,
    this.direction = Axis.horizontal,
    this.alignment = WrapAlignment.start,
    // 主軸方向子widget的間距
    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>[],
  })

代碼示例

class WrapTest extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('流式布局-Wrap'),
      ),
      body: Container(
        child: Wrap(
          // 主軸(水平)方向間距
          spacing: 8.0,
          // 縱軸(垂直)方向間距
          runSpacing: 4.0,
          // 沿主軸方向居中
          alignment: WrapAlignment.center,
          children: <Widget>[
            Chip(
              avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('A'),),
              label: Text('HelloWorld'),
            ),

            Chip(
              avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('B'),),
              label: Text('WorldHello'),
            ),

            Chip(
              avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('T'),),
              label: Text('FlowWorld'),
            ),

            Chip(
              avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('J'),),
              label: Text('WarpHello'),
            ),

            Chip(
              avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('F'),),
              label: Text('Yes i do'),
            ),

          ],
        ),
      ),
    );
  }
}

FLow

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

  1. 性能好;Flow是一個對子組件尺寸以及位置調整非常高效的控件,Flow用轉換矩陣在對子組件進行位置調整的時候進行了優化:在Flow定位過后,如果子組件的尺寸或者位置發生了變化,在FlowDelegate中的paintChildren()方法中調用context.paintChild 進行重繪,而context.paintChild在重繪時使用了轉換矩陣,並沒有實際調整組件位置。
  2. 靈活;由於我們需要自己實現FlowDelegate的paintChildren()方法,所以我們需要自己計算每一個組件的位置,因此,可以自定義布局策略。

缺點

  1. 使用復雜。
  2. 不能自適應子組件大小,必須通過指定父容器大小或實現TestFlowDelegate的getSize返回固定大小。

接口描述

 Flow({
    Key key,
    @required this.delegate,
    List<Widget> children = const <Widget>[],
  })

代碼示例

class FlowTest extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('流式布局-Flow'),
      ),
      body: Container(
        child: Flow(
          delegate: FlowDelegateTest(margin: EdgeInsets.all(10.0)),
          children: <Widget>[
            Container(width: 80.0, height: 80.0, color: Colors.red,),
            Container(width: 80.0, height: 80.0, color: Colors.green,),
            Container(width: 80.0, height: 80.0, color: Colors.blue,),
            Container(width: 80.0, height: 80.0, color: Colors.yellow,),
            Container(width: 80.0, height: 80.0, color: Colors.brown,),
            Container(width: 80.0, height: 80.0, color: Colors.purple,),
          ],

        ),
      ),
    );

  }
}

class FlowDelegateTest extends FlowDelegate {
  EdgeInsets margin = EdgeInsets.zero;
  FlowDelegateTest({this.margin});

  // Flow主要要重載這個函數,它的主要任務是確定每個子widget位置。
  // 由於Flow不能自適應子widget的大小,通過在getSize返回一個固定大小來指定Flow的大小。
  @override
  void paintChildren(FlowPaintingContext context) {
    var x = margin.left;
    var y = margin.right;
    // 計算每一個子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: 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: Matrix4.translationValues(x, y, 0.0)
        );
        x += context.getChildSize(i).width + margin.left + margin.right;
      }
    }
  }

  @override
  Size getSize(BoxConstraints constraints) {
    // 指定Flow的大小
    return Size(double.infinity, 200.0);
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM