Skip to content

Container ​

cpp
class Container : public Widget;

最主要的布局组件,与 Taitank flexbox 节点一一对应:子节点由 Taitank 布局, Container 自身只绘制圆角背景。

头文件 ​

cpp
#include <neoflux/widgets/container.h>

构造 ​

cpp
auto container = std::make_shared<Container>();

添加子节点 ​

方法签名说明
SetChildContainer& SetChild(std::shared_ptr<Widget> child)单子节点便捷方法(可链式,返回 Container&)
childContainer& child(std::shared_ptr<Widget> child)追加一个子节点(可链式,返回 Container&)
childrenContainer& children(std::initializer_list<std::shared_ptr<Widget>> children)追加多个(可链式,返回 Container&)
build[[nodiscard]] std::shared_ptr<Widget> build()继承自 Widget,链尾终止符

链式写法详见 流式 API。

布局旋钮(均可链式,返回 Container&) ​

方法签名含义
SetFlexDirectionContainer& SetFlexDirection(FlexDirection d)主轴方向:kRow / kRowReverse / kColumn / kColumnReverse,默认 kColumn。
SetJustifyContentContainer& SetJustifyContent(HAlign a)主轴对齐:HAlign::kLeft/kCenter/kRight。
SetAlignItemsContainer& SetAlignItems(VAlign a)交叉轴对齐:VAlign::kTop/kCenter/kBottom。
SetFlexGrowContainer& SetFlexGrow(float grow)本容器在父布局中撑满剩余空间的弹性系数。
SetWidthContainer& SetWidth(float w)固定宽度,0 表示自适应。
SetHeightContainer& SetHeight(float h)固定高度,0 表示自适应。

间距与外观(均可链式,返回 Container&) ​

方法签名含义
SetPaddingContainer& SetPadding(const EdgeInsets& p)内边距。
SetMarginContainer& SetMargin(const EdgeInsets& m)外边距。
SetBackgroundColorContainer& SetBackgroundColor(const Color& c)背景填充。
SetBorderRadiusContainer& SetBorderRadius(float r)圆角半径(px),0 为直角。

示例 ​

cpp
auto card = std::make_shared<Container>();
card->SetFlexDirection(FlexDirection::kColumn)
    .SetPadding({.left = 16, .top = 12, .right = 16, .bottom = 12})
    .SetBackgroundColor({.r = 255, .g = 255, .b = 255, .a = 255})
    .SetBorderRadius(8.0F);
card->AddChild(title);
card->AddChild(body);

等价的流式写法:

cpp
auto card = std::make_shared<Container>()
    ->SetFlexDirection(FlexDirection::kColumn)
    .SetPadding({.left = 16, .top = 12, .right = 16, .bottom = 12})
    .SetBackgroundColor({255, 255, 255, 255})
    .SetBorderRadius(8.0F)
    .children({title, body})
    .build();

另见 ​

讨论

Released under the GPL-3.0 License.