蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Container 盒模型与断言校验、BoxDecoration 约束、Clip 几何判定与卡片布局离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-container.py
explore-container.py143 行
"""Offline Flutter Container, BoxDecoration, Clip, and gradient/shadow card verifier for Ch08 (container)."""
import json

VERSION = "CPython 3.12.14 offline Flutter/Dart architecture & runtime verifier (Flutter SDK absent)"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def validate_container(width=None, height=None, color=None, decoration=None, padding=0, margin=0):
    if color is not None and decoration is not None:
        raise ValueError("Cannot provide both a color and a decoration on Container")
    if decoration and decoration.get("shape") == "circle" and decoration.get("borderRadius"):
        raise ValueError("borderRadius cannot be used with BoxShape.circle")
    inner_w = (width - 2 * padding) if width is not None else None
    outer_w = (width + 2 * margin) if width is not None else None
    return {"inner_width": inner_w, "outer_width": outer_w, "valid": True}


def point_in_clip_oval(px, py, w, h):
    cx, cy = w / 2.0, h / 2.0
    rx, ry = w / 2.0, h / 2.0
    return ((px - cx) / rx) ** 2 + ((py - cy) / ry) ** 2 <= 1.0


def point_in_triangle(px, py, v1, v2, v3):
    def sign(p1, p2, p3):
        return (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1])

    d1 = sign((px, py), v1, v2)
    d2 = sign((px, py), v2, v3)
    d3 = sign((px, py), v3, v1)
    has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
    has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
    return not (has_neg and has_pos)


def main():
    cases = []

    # Case 1: Container box model & color vs decoration mutual exclusion assertion
    basic_box = validate_container(width=200, height=100, color="Colors.blue", padding=16, margin=10)
    conflict_caught = False
    try:
        validate_container(width=200, height=100, color="Colors.blue", decoration={"color": "Colors.red"})
    except ValueError:
        conflict_caught = True
    cases.append(
        check(
            "container_box_and_assertion",
            "Verify Container inner/outer box sizing and Flutter assertion forbidding simultaneous color and decoration",
            {"inner_width": 168, "outer_width": 220, "conflict_caught": True},
            {"inner_width": basic_box["inner_width"], "outer_width": basic_box["outer_width"], "conflict_caught": conflict_caught},
        )
    )

    # Case 2: BoxDecoration gradients, per-side border, borderRadius & BoxShape.circle constraint
    circle_radius_caught = False
    try:
        validate_container(width=100, height=100, decoration={"shape": "circle", "borderRadius": 10})
    except ValueError:
        circle_radius_caught = True
    asymmetric_radii = {"topLeft": 20, "topRight": 10, "bottomLeft": 10, "bottomRight": 20}
    border_sides = {"top": 2, "bottom": 2, "left": 2, "right": 2}
    cases.append(
        check(
            "box_decoration_border_radius_gradient",
            "Verify BoxDecoration asymmetric BorderRadius.only, 4-side Border, Linear/Radial gradients, and circle assertion",
            {
                "circle_radius_caught": True,
                "radii_sum": 60,
                "total_horizontal_border": 4,
                "gradient_types": ["LinearGradient", "RadialGradient"],
            },
            {
                "circle_radius_caught": circle_radius_caught,
                "radii_sum": sum(asymmetric_radii.values()),
                "total_horizontal_border": border_sides["left"] + border_sides["right"],
                "gradient_types": ["LinearGradient", "RadialGradient"],
            },
        )
    )

    # Case 3: Multi-layer BoxShadow & ClipOval / ClipRRect / ClipPath geometry
    shadows = [
        {"color": "red@0.3", "blurRadius": 5, "offset": [-3, -3]},
        {"color": "blue@0.3", "blurRadius": 5, "offset": [3, 3]},
    ]
    # 100x100 oval: center (50,50) inside, corner (2,2) clipped outside
    oval_center = point_in_clip_oval(50, 50, 100, 100)
    oval_corner = point_in_clip_oval(2, 2, 100, 100)
    # 200x200 triangle (100,0)-(0,200)-(200,200): (100,150) inside, (10,10) clipped
    tri_in = point_in_triangle(100, 150, (100, 0), (0, 200), (200, 200))
    tri_out = point_in_triangle(10, 10, (100, 0), (0, 200), (200, 200))
    cases.append(
        check(
            "box_shadow_and_clip_geometry",
            "Verify multi-layer BoxShadow specs and ClipOval/ClipPath point inclusion math",
            {
                "shadow_layers": 2,
                "oval_center_visible": True,
                "oval_corner_clipped": True,
                "triangle_center_visible": True,
                "triangle_corner_clipped": True,
            },
            {
                "shadow_layers": len(shadows),
                "oval_center_visible": oval_center,
                "oval_corner_clipped": not oval_corner,
                "triangle_center_visible": tri_in,
                "triangle_corner_clipped": not tri_out,
            },
        )
    )

    # Case 4: Composite MyCard widget layout math (width=280, padding=20 -> content width=240)
    card_box = validate_container(
        width=280,
        padding=20,
        decoration={"gradient": "LinearGradient", "borderRadius": 16, "boxShadow": [{"blurRadius": 12, "offset": [0, 6]}]},
    )
    cases.append(
        check(
            "composite_gradient_card_layout",
            "Verify composite MyCard (width=280, padding=20, radius=16, gradient+shadow) inner column width=240",
            {"content_width": 240, "valid": True},
            {"content_width": card_box["inner_width"], "valid": card_box["valid"]},
        )
    )

    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/flutter-container-lab/explore-container.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-container.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Flutter

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip

下载课程关联工程包:Flutter

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1991.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习