"""Offline Flutter Material 3 ThemeData, ColorScheme, Theme.of inheritance, Dark Mode & TextTheme verifier for Ch15 (theming)."""
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 build_color_scheme(seed_name, brightness):
if brightness == "light":
return {
"brightness": "light",
"seed": seed_name,
"primary": "#0061A4",
"onPrimary": "#FFFFFF",
"secondary": "#535F70",
"onSecondary": "#FFFFFF",
"error": "#BA1A1A",
"onError": "#FFFFFF",
"surface": "#FDFCFF",
}
return {
"brightness": "dark",
"seed": seed_name,
"primary": "#9ECAFF",
"onPrimary": "#003258",
"secondary": "#BBC7DB",
"onSecondary": "#253140",
"error": "#FFB4AB",
"onError": "#690005",
"surface": "#1A1C1E",
}
def resolve_effective_brightness(theme_mode, system_brightness):
if theme_mode == "light":
return "light"
if theme_mode == "dark":
return "dark"
return system_brightness
def main():
cases = []
# Case 1: Material 3 ThemeData global component theme configuration
theme_data = {
"useMaterial3": True,
"fontFamily": "Roboto",
"colorScheme": build_color_scheme("Colors.blue", "light"),
"appBarTheme": {"centerTitle": True, "elevation": 0},
"elevatedButtonTheme": {"padding": {"horizontal": 24, "vertical": 12}},
"inputDecorationTheme": {"borderRadius": 8, "filled": True},
}
cases.append(
check(
"theme_data_material3_config",
"Verify Material 3 ThemeData global config (seedColor, fontFamily, AppBar/Button/Input sub-themes)",
{
"useMaterial3": True,
"fontFamily": "Roboto",
"appBar_centerTitle": True,
"button_h_pad": 24,
"input_filled": True,
},
{
"useMaterial3": theme_data["useMaterial3"],
"fontFamily": theme_data["fontFamily"],
"appBar_centerTitle": theme_data["appBarTheme"]["centerTitle"],
"button_h_pad": theme_data["elevatedButtonTheme"]["padding"]["horizontal"],
"input_filled": theme_data["inputDecorationTheme"]["filled"],
},
)
)
# Case 2: Theme.of(context) ColorScheme token lookup & local Theme override scoping
root_scheme = build_color_scheme("Colors.blue", "light")
scoped_override = dict(root_scheme, primary="#6750A4")
cases.append(
check(
"theme_of_context_token_lookup",
"Verify Theme.of(context) resolves paired primary/onPrimary, secondary/onSecondary, error/onError tokens and nested overrides",
{
"root_primary": "#0061A4",
"root_on_primary": "#FFFFFF",
"root_error": "#BA1A1A",
"scoped_primary": "#6750A4",
},
{
"root_primary": root_scheme["primary"],
"root_on_primary": root_scheme["onPrimary"],
"root_error": root_scheme["error"],
"scoped_primary": scoped_override["primary"],
},
)
)
# Case 3: Dark mode ThemeMode.system/light/dark resolution via stateful ValueNotifier<ThemeMode>
state = {"themeMode": "system", "systemBrightness": "dark"}
eff_system = resolve_effective_brightness(state["themeMode"], state["systemBrightness"])
# Toggle to light then dark via state (ThemeData itself is immutable and has no setThemeMode)
state["themeMode"] = "light"
eff_light = resolve_effective_brightness(state["themeMode"], state["systemBrightness"])
state["themeMode"] = "dark"
eff_dark = resolve_effective_brightness(state["themeMode"], state["systemBrightness"])
dark_scheme = build_color_scheme("Colors.blue", eff_dark)
cases.append(
check(
"dark_mode_theme_mode_resolution",
"Verify ThemeMode.system/light/dark resolution and state-driven switching (ThemeData is immutable)",
{
"system_dark_effective": "dark",
"forced_light": "light",
"forced_dark": "dark",
"dark_surface": "#1A1C1E",
},
{
"system_dark_effective": eff_system,
"forced_light": eff_light,
"forced_dark": eff_dark,
"dark_surface": dark_scheme["surface"],
},
)
)
# Case 4: Material 3 TextTheme 15-style typography scale hierarchy
text_theme = {
"displayLarge": 57, "displayMedium": 45, "displaySmall": 36,
"headlineLarge": 32, "headlineMedium": 28, "headlineSmall": 24,
"titleLarge": 22, "titleMedium": 16, "titleSmall": 14,
"bodyLarge": 16, "bodyMedium": 14, "bodySmall": 12,
"labelLarge": 14, "labelMedium": 12, "labelSmall": 11,
}
cases.append(
check(
"material3_text_theme_scale",
"Verify all 15 Material 3 TextTheme slots and descending hierarchy from displayLarge(57) to labelSmall(11)",
{
"slot_count": 15,
"display_monotonic": True,
"headline_monotonic": True,
"body_monotonic": True,
"range": [57, 11],
},
{
"slot_count": len(text_theme),
"display_monotonic": text_theme["displayLarge"] > text_theme["displayMedium"] > text_theme["displaySmall"],
"headline_monotonic": text_theme["headlineLarge"] > text_theme["headlineMedium"] > text_theme["headlineSmall"],
"body_monotonic": text_theme["bodyLarge"] > text_theme["bodyMedium"] > text_theme["bodySmall"],
"range": [text_theme["displayLarge"], text_theme["labelSmall"]],
},
)
)
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-theming-lab/explore-theming.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-theming.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2008.zip