<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 流式布局 ### Wrap 在介紹Row和Colum時,如果子widget超出屏幕范圍,則會報溢出錯誤,如: ``` Row( children: <Widget>[ Text("xxx"*100) ], ); ``` 運行: ![](https://box.kancloud.cn/563f10174e371dd03e1efe3bbc2a891c_360x150.png) 可以看到,右邊溢出部分報錯。這是因為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:縱軸方向的對齊方式 下面看一個示例子: ``` 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'), ), ], ) ``` 運行效果: ![](https://box.kancloud.cn/632eedafae7596102598ee4ee0bf76ac_360x111.png) ### 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`返回固定大小。 示例: 我們對六個色塊進行自定義流式布局: ``` 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,), ], ) ``` 實現TestFlowDelegate: ``` 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; } } ``` 效果: ![](https://box.kancloud.cn/bca99cccce72941f8aa1a0675eb7732f_360x191.png) 可以看到我們主要的任務就是實現`paintChildren`,它的主要任務是確定每個子widget位置。由于Flow不能自適應子widget的大小,我們通過在`getSize`返回一個固定大小來指定Flow的大小。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看