本文主要介紹Flutter布局中的SizedOverflowBox、Transform、CustomSingleChildLayout三種控件,詳細介紹了其布局行為以及使用場景,並對源碼進行了分析。
1. SizedOverflowBox
A widget that is a specific size but passes its original constraints through to its child, which will probably overflow.
1.1 簡介
光看名稱,就可以猜出,SizedOverflowBox是SizedBox與OverflowBox的結合體。
1.2 布局行為
SizedOverflowBox主要的布局行為有兩點:
- 尺寸部分。通過將自身的固定尺寸,傳遞給child,來達到控制child尺寸的目的;
- 超出部分。可以突破父節點尺寸的限制,超出部分也可以被渲染顯示,與OverflowBox類似。
1.3 繼承關系
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > SizedOverflowBox
1.4 示例代碼
Container(
color: Colors.green,
alignment: Alignment.topRight,
width: 200.0,
height: 200.0,
padding: EdgeInsets.all(5.0),
child: SizedOverflowBox(
size: Size(100.0, 200.0),
child: Container(color: Colors.red, width: 200.0, height: 100.0,),
),
);
代碼運行的時候報出了下面的異常,很神奇。但是同學們可以自己看看代碼運行的效果,可以超出,但是不太好用。
══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
1.5 源碼解析
const SizedOverflowBox({
Key key,
@required this.size,
this.alignment = Alignment.center,
Widget child,
})
1.5.1 屬性解析
size:固定的尺寸。
alignment:對齊方式。
1.5.2 源碼
直接上布局相關的代碼:
size = constraints.constrain(_requestedSize);
if (child != null) {
child.layout(constraints);
alignChild();
}
如果child存在的話,就將child設為對應的尺寸,然后按照對齊方式進行對齊。但是在實際寫sample的時候,感覺跟我預想中的表現不太一致,而且經常報出異常。不知道是我理解錯了,還是樣例寫錯了,如果有了解的同學,麻煩告知,在此感謝。
1.6 使用場景
代碼的表現跟我預想中的不太一致,更推薦使用OverflowBox,場景也跟其比較一致。
2. Transform
A widget that applies a transformation before painting its child.
2.1 簡介
Transform在介紹Container的時候有提到過,就是做矩陣變換的。Container中矩陣變換就是使用的Transform。
2.2 布局行為
有過其他平台經驗的,對Transform應該不會陌生。可以對child做平移、旋轉、縮放等操作。
2.3 繼承關系
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Transform
2.4 示例代碼
Center(
child: Transform(
transform: Matrix4.rotationZ(0.3),
child: Container(
color: Colors.blue,
width: 100.0,
height: 100.0,
),
),
)
示例中將Container繞z軸旋轉了,代碼很簡單。Matrix4也提供了很多便捷的構造函數供大家使用,因此寫起來,並不會有太大的難度。
2.5 源碼解析
const Transform({
Key key,
@required this.transform,
this.origin,
this.alignment,
this.transformHitTests = true,
Widget child,
})
上面是其默認的構造函數,Transform也提供下面三種構造函數:
Transform.rotate
Transform.translate
Transform.scale
2.5.1 屬性解析
transform:一個4x4的矩陣。不難發現,其他平台的變換矩陣也都是四階的。一些復合操作,僅靠三維是不夠的,必須采用額外的一維來補充,感興趣的同學可以自行搜索了解。
origin:旋轉點,相對於左上角頂點的偏移。默認旋轉點事左上角頂點。
alignment:對齊方式。
transformHitTests:點擊區域是否也做相應的改變。
2.5.2 源碼
我們來看看它的繪制代碼:
if (child != null) {
final Matrix4 transform = _effectiveTransform;
final Offset childOffset = MatrixUtils.getAsTranslation(transform);
if (childOffset == null)
context.pushTransform(needsCompositing, offset, transform, super.paint);
else
super.paint(context, offset + childOffset);
}
整個繪制代碼不復雜,如果child有偏移的話,則將兩個偏移相加,進行繪制。如果child沒有偏移的話,則按照設置的offset、transform進行繪制。
2.6 使用場景
這個控件算是較常見的控件,很多平移、旋轉、縮放都可以使用的到。如果只是單純的進行變換的話,用Transform比用Container效率會更高。
3. CustomSingleChildLayout
A widget that defers the layout of its single child to a delegate.
3.1 簡介
一個通過外部傳入的布局行為,來進行布局的控件,不同於其他固定布局的控件,我們自定義一些單節點布局控件的時候,可以考慮使用它。
3.2 布局行為
CustomSingleChildLayout提供了一個控制child布局的delegate,這個delegate可以控制這些因素:
- 可以控制child的布局constraints;
- 可以控制child的位置;
- 在parent的尺寸不依賴於child的情況下,可以決定parent的尺寸。
3.3 繼承關系
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > CustomSingleChildLayout
3.4 示例代碼
class FixedSizeLayoutDelegate extends SingleChildLayoutDelegate {
FixedSizeLayoutDelegate(this.size);
final Size size;
@override
Size getSize(BoxConstraints constraints) => size;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return new BoxConstraints.tight(size);
}
@override
bool shouldRelayout(FixedSizeLayoutDelegate oldDelegate) {
return size != oldDelegate.size;
}
}
Container(
color: Colors.blue,
padding: const EdgeInsets.all(5.0),
child: CustomSingleChildLayout(
delegate: FixedSizeLayoutDelegate(Size(200.0, 200.0)),
child: Container(
color: Colors.red,
width: 100.0,
height: 300.0,
),
),
)
由於SingleChildLayoutDelegate是一個抽象類,我們需要單獨寫一個繼承自它的delegate,來進行相關的布局操作。上面例子中是一個固定尺寸的布局。
3.5 源碼解析
構造函數如下:
const CustomSingleChildLayout({
Key key,
@required this.delegate,
Widget child
})
3.5.1 屬性解析
alignment:一個抽象類,我們需要自行實現布局的類。
3.5.2 源碼
我們直接來看其布局函數:
size = _getSize(constraints);
if (child != null) {
final BoxConstraints childConstraints = delegate.getConstraintsForChild(constraints);
assert(childConstraints.debugAssertIsValid(isAppliedConstraint: true));
child.layout(childConstraints, parentUsesSize: !childConstraints.isTight);
final BoxParentData childParentData = child.parentData;
childParentData.offset = delegate.getPositionForChild(size, childConstraints.isTight ? childConstraints.smallest : child.size);
}
其child的constraints是通過delegate傳入的,而這個delegate則是我們通過外部繼承自SingleChildLayoutDelegate實現的。
我們接下來看一下SingleChildLayoutDelegate提供了哪些接口:
Size getSize(BoxConstraints constraints) => constraints.biggest;
BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints;
Offset getPositionForChild(Size size, Size childSize) => Offset.zero;
bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate);
其中前三個都是布局相關的,包括尺寸、constraints、位置。最后一個函數是判斷是否需要重新布局的。我們在編寫delegate的時候,可以選擇需要進行修改的屬性進行重寫,並給予相應的布局屬性即可。
3.6 使用場景
這種控件用起來可能會繁瑣一些,但是我們可以通過這個控件去封裝一些基礎的控件,供他人使用。
4. 階段性小結
到目前為止,Flutter中單子節點布局控件,大致上都簡單的梳理了一遍。如果一直在關注這個系列文章的同學,應該可以發現我經常吐槽其控件設計。
Flutter中總共有18個單子節點布局控件,18個啊,還沒有算多子節點布局控件,也沒有算可能會新增的。這樣的學習成本非常高。雖然常見的就那么幾種,平時一直用那些也都沒有問題,但是布局的時候總是會遇到那么幾種解決不了的問題。而且梳理的時候,會發現,幾種控件都能解決的問題,並不是說把效果實現出了就完事了,這中間肯定會涉及到哪種控件效率更高。如果要對Flutter項目做較深入的性能優化,這些控件肯定都得掌握。
Flutter的這種設計,把一些原本應該由它們承擔的成本,轉移到了開發者身上。很多控件在日常使用中幾乎都用不上,並沒有考慮太多實際的使用場景。當然了,也還是得安慰自己,這畢竟只是初期,亂點就亂點,日子肯定會越來越好的。
后面還會將多子節點控件全部梳理一遍,然后來個大總結,對什么場景該使用哪種控件,如何進行控件級別的優化,做一個總結。
5. 后話
筆者建了一個Flutter學習相關的項目,Github地址,里面包含了筆者寫的關於Flutter學習相關的一些文章,會定期更新,也會上傳一些學習Demo,歡迎大家關注。