"""Offline Flutter BoxConstraints, Row/Column/Flex/Expanded/Stack/Padding/Scaffold verifier for Ch07 (layout)."""
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 layout_row(container_width, item_widths, alignment):
total_items = sum(item_widths)
free = container_width - total_items
n = len(item_widths)
positions = []
if alignment == "start":
x, gap = 0.0, 0.0
elif alignment == "center":
x, gap = free / 2.0, 0.0
elif alignment == "end":
x, gap = free, 0.0
elif alignment == "spaceBetween":
x, gap = 0.0, (free / (n - 1) if n > 1 else 0.0)
elif alignment == "spaceEvenly":
gap = free / (n + 1)
x = gap
elif alignment == "spaceAround":
gap = free / n
x = gap / 2.0
else:
raise ValueError(alignment)
for w in item_widths:
positions.append(round(x, 2))
x += w + gap
return positions
def layout_cross_axis(container_height, child_height, alignment):
if alignment == "start":
return {"y": 0.0, "height": child_height}
if alignment == "center":
return {"y": round((container_height - child_height) / 2.0, 2), "height": child_height}
if alignment == "end":
return {"y": round(container_height - child_height, 2), "height": child_height}
if alignment == "stretch":
return {"y": 0.0, "height": container_height}
raise ValueError(alignment)
def layout_flex_expanded(container_width, children):
# children: list of {"fixed": w} or {"flex": f}
fixed_total = sum(c["fixed"] for c in children if "fixed" in c)
flex_total = sum(c["flex"] for c in children if "flex" in c)
remaining = max(0.0, container_width - fixed_total)
widths = []
for c in children:
if "fixed" in c:
widths.append(float(c["fixed"]))
else:
widths.append(round(remaining * c["flex"] / flex_total, 2))
return widths
def main():
cases = []
# Case 1: Row MainAxisAlignment 6 modes math on 300px container with three 40px icons
modes = ["start", "center", "end", "spaceBetween", "spaceEvenly", "spaceAround"]
row_results = {m: layout_row(300.0, [40.0, 40.0, 40.0], m) for m in modes}
cases.append(
check(
"row_main_axis_alignment_math",
"Verify exact horizontal x-coordinates for all 6 MainAxisAlignment modes on 300px Row with 3x40px children",
{
"start": [0.0, 40.0, 80.0],
"center": [90.0, 130.0, 170.0],
"end": [180.0, 220.0, 260.0],
"spaceBetween": [0.0, 130.0, 260.0],
"spaceEvenly": [45.0, 130.0, 215.0],
"spaceAround": [30.0, 130.0, 230.0],
},
row_results,
)
)
# Case 2: Column CrossAxisAlignment 4 modes math
cross_modes = ["start", "center", "end", "stretch"]
cross_results = {m: layout_cross_axis(200.0, 40.0, m) for m in cross_modes}
cases.append(
check(
"column_cross_axis_alignment_math",
"Verify CrossAxisAlignment start/center/end/stretch geometry on 200px container with 40px child",
{
"start": {"y": 0.0, "height": 40.0},
"center": {"y": 80.0, "height": 40.0},
"end": {"y": 160.0, "height": 40.0},
"stretch": {"y": 0.0, "height": 200.0},
},
cross_results,
)
)
# Case 3: Expanded fixed+adaptive and 1:2:1 flex ratio distribution
fixed_plus_expanded = layout_flex_expanded(400.0, [{"fixed": 100}, {"flex": 1}, {"fixed": 50}])
ratio_1_2_1 = layout_flex_expanded(400.0, [{"flex": 1}, {"flex": 2}, {"flex": 1}])
cases.append(
check(
"expanded_flex_space_allocation",
"Verify Flex/Expanded layout distributes remaining 250px and 1:2:1 ratios (100/200/100) on 400px Row",
{"fixed_plus_expanded": [100.0, 250.0, 50.0], "ratio_1_2_1": [100.0, 200.0, 100.0]},
{"fixed_plus_expanded": fixed_plus_expanded, "ratio_1_2_1": ratio_1_2_1},
)
)
# Case 4: Stack Positioned coordinates, EdgeInsets box model & Scaffold slot structure
stack_w, stack_h = 300, 200
top_right_icon = {"x": stack_w - 10 - 24, "y": 10}
bottom_left_text = {"x": 10, "y": stack_h - 10 - 20}
# EdgeInsets.only(left=10, right=20, top=5, bottom=5) + symmetric(h=16, v=8)
only_insets = {"horizontal": 10 + 20, "vertical": 5 + 5}
sym_insets = {"horizontal": 16 * 2, "vertical": 8 * 2}
scaffold_slots = ["appBar", "body", "floatingActionButton", "bottomNavigationBar"]
cases.append(
check(
"stack_padding_and_scaffold_layout",
"Verify Stack Positioned offsets, EdgeInsets total spacing, and Material Scaffold 4-slot skeleton",
{
"top_right_icon": {"x": 266, "y": 10},
"bottom_left_text": {"x": 10, "y": 170},
"only_insets": {"horizontal": 30, "vertical": 10},
"sym_insets": {"horizontal": 32, "vertical": 16},
"scaffold_slot_count": 4,
},
{
"top_right_icon": top_right_icon,
"bottom_left_text": bottom_left_text,
"only_insets": only_insets,
"sym_insets": sym_insets,
"scaffold_slot_count": len(scaffold_slots),
},
)
)
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-layout-lab/explore-layout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-layout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1999.zip