"""Offline Flutter ListView.builder, GridView, ScrollController, and Slivers verifier for Ch09 (listview)."""
import json
import math
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 visible_window(item_count, item_extent, viewport_extent, scroll_offset, cache_extent=0.0):
start = max(0, int(math.floor((scroll_offset - cache_extent) / item_extent)))
end = min(item_count - 1, int(math.ceil((scroll_offset + viewport_extent + cache_extent) / item_extent)) - 1)
return list(range(start, end + 1))
def main():
cases = []
# Case 1: ListView.builder lazy viewport windowing vs eager children & ListView.separated count
built_indices = visible_window(item_count=100, item_extent=56.0, viewport_extent=560.0, scroll_offset=280.0, cache_extent=0.0)
separated_count = 10
separator_dividers = max(0, separated_count - 1)
cases.append(
check(
"listview_lazy_builder_and_separated",
"Verify ListView.builder materializes only 10 visible items (indices 5..14) out of 100 and separated inserts N-1 dividers",
{"visible_count": 10, "first_idx": 5, "last_idx": 14, "separator_count_for_10": 9},
{
"visible_count": len(built_indices),
"first_idx": built_indices[0],
"last_idx": built_indices[-1],
"separator_count_for_10": separator_dividers,
},
)
)
# Case 2: Horizontal ListView bounding & ScrollDirection/reverse/itemExtent props
# Horizontal list: container width 348, item width 100 + horizontal margin 16 = 116 -> 3 visible items at offset 0
horiz_visible = visible_window(item_count=20, item_extent=116.0, viewport_extent=348.0, scroll_offset=0.0)
reversed_order = list(reversed(["A", "B", "C"]))
cases.append(
check(
"horizontal_listview_and_properties",
"Verify Axis.horizontal ListView inside bounded height=120 SizedBox and reverse ordering",
{"horiz_visible": [0, 1, 2], "bounded_height": 120, "reversed_order": ["C", "B", "A"]},
{"horiz_visible": horiz_visible, "bounded_height": 120, "reversed_order": reversed_order},
)
)
# Case 3: GridView.count, GridView.builder spacing/aspect math & GridView.extent adaptive columns
# 4 columns in 424px width with crossAxisSpacing=8 -> (424 - 3*8) / 4 = 100px cell width, aspect 1.0 -> 100px height
viewport_w = 424.0
cols = 4
spacing = 8.0
cell_w = (viewport_w - (cols - 1) * spacing) / cols
cell_h = cell_w / 1.0
# GridView.extent maxCrossAxisExtent=150 on 424px width -> ceil(424 / 150) = 3 columns
adaptive_cols = math.ceil(viewport_w / 150.0)
cases.append(
check(
"gridview_delegate_math",
"Verify SliverGridDelegateWithFixedCrossAxisCount (4 cols, 8px spacing -> 100x100 cell) and maxCrossAxisExtent=150 -> 3 cols",
{"cell_w": 100.0, "cell_h": 100.0, "adaptive_cols": 3, "rows_for_50_items_4_cols": 13},
{"cell_w": cell_w, "cell_h": cell_h, "adaptive_cols": adaptive_cols, "rows_for_50_items_4_cols": math.ceil(50 / 4)},
)
)
# Case 4: ScrollController offset clamping/animateTo(0) & SliverAppBar pinned collapse height
max_scroll = 100 * 56.0 - 560.0
offsets = []
current_offset = 0.0
for target in (420.0, 9999.0, 0.0):
current_offset = max(0.0, min(max_scroll, target))
offsets.append(current_offset)
# SliverAppBar expandedHeight=200, toolbarHeight=56, pinned=True
def sliver_appbar_height(scroll_y, expanded=200.0, min_h=56.0, pinned=True):
h = expanded - scroll_y
return max(min_h, h) if pinned else max(0.0, h)
cases.append(
check(
"scroll_controller_and_sliver_appbar",
"Verify ScrollController offset clamping/animateTo(0) and pinned SliverAppBar collapse from 200px to 56px",
{
"controller_offsets": [420.0, 5040.0, 0.0],
"appbar_at_0": 200.0,
"appbar_at_100": 100.0,
"appbar_at_300_pinned": 56.0,
},
{
"controller_offsets": offsets,
"appbar_at_0": sliver_appbar_height(0.0),
"appbar_at_100": sliver_appbar_height(100.0),
"appbar_at_300_pinned": sliver_appbar_height(300.0),
},
)
)
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-listview-lab/explore-listview.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-listview.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2000.zip