"""Offline Flutter TextField, TextEditingController, FocusNode, Buttons, Switch/Checkbox/Radio, Slider verifier for Ch10 (user-input)."""
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,
}
class TextEditingController:
def __init__(self, initial=""):
self._text = initial
self.disposed = False
self.change_log = []
@property
def text(self):
return self._text
@text.setter
def text(self, val):
if self.disposed:
raise RuntimeError("TextEditingController used after dispose")
self._text = val
self.change_log.append(val)
def clear(self):
self.text = ""
def dispose(self):
self.disposed = True
class FocusManager:
def __init__(self):
self.focused = None
def request_focus(self, node_id):
self.focused = node_id
def unfocus(self, node_id):
if self.focused == node_id:
self.focused = None
def main():
cases = []
# Case 1: TextField configuration & TextEditingController read/set/clear/dispose lifecycle
ctrl = TextEditingController()
ctrl.text = "flutter_user"
read_val = ctrl.text
ctrl.clear()
after_clear = ctrl.text
ctrl.text = "预设文本"
after_preset = ctrl.text
ctrl.dispose()
masked = "*" * len("secret123")
cases.append(
check(
"textfield_and_editing_controller",
"Verify TextEditingController text set/read/clear/dispose lifecycle and password obscureText masking",
{
"read_val": "flutter_user",
"after_clear": "",
"after_preset": "预设文本",
"disposed": True,
"obscured_password": "*********",
},
{
"read_val": read_val,
"after_clear": after_clear,
"after_preset": after_preset,
"disposed": ctrl.disposed,
"obscured_password": masked,
},
)
)
# Case 2: FocusNode exclusive focus transfer & unfocus
fm = FocusManager()
trace = [fm.focused]
fm.request_focus("input1")
trace.append(fm.focused)
fm.request_focus("input2")
trace.append(fm.focused)
fm.unfocus("input1") # no-op because input2 holds focus
trace.append(fm.focused)
fm.unfocus("input2")
trace.append(fm.focused)
cases.append(
check(
"focus_node_state_machine",
"Verify FocusNode exclusive focus switching between input1 and input2 and clean unfocus",
[None, "input1", "input2", "input2", None],
trace,
)
)
# Case 3: Button callbacks & Switch / Checkbox / Radio mutually exclusive selection
clicks = []
buttons = ["TextButton", "ElevatedButton", "OutlinedButton", "IconButton", "ElevatedButton.icon", "CupertinoButton"]
for b in buttons:
clicks.append(b)
switch_val = False
switch_val = not switch_val
checkbox_val = None
checkbox_val = checkbox_val if checkbox_val is not None else False
checkbox_val = True
radio_group = "A"
radio_group = "B"
cases.append(
check(
"buttons_and_toggle_controls",
"Verify 6 button types dispatch callbacks and Switch/Checkbox/Radio state updates",
{"button_count": 6, "switch": True, "checkbox": True, "radio_group": "B"},
{"button_count": len(clicks), "switch": switch_val, "checkbox": checkbox_val, "radio_group": radio_group},
)
)
# Case 4: Slider divisions quantization & RangeSlider bounds validation
def quantize_slider(val, min_v=0.0, max_v=1.0, divisions=10):
clamped = max(min_v, min(max_v, val))
step = (max_v - min_v) / divisions
snapped = round((clamped - min_v) / step) * step + min_v
return round(snapped, 4)
q_val = quantize_slider(0.73, 0.0, 1.0, 10)
range_vals = (0.3, 0.7)
cases.append(
check(
"slider_and_range_slider_math",
"Verify Slider divisions=10 quantization (0.73 -> 0.7, label 70%) and RangeSlider start<=end invariant",
{"snapped": 0.7, "label": "70%", "range_span": 0.4, "valid_range": True},
{
"snapped": q_val,
"label": f"{round(q_val * 100)}%",
"range_span": round(range_vals[1] - range_vals[0], 4),
"valid_range": 0.0 <= range_vals[0] <= range_vals[1] <= 1.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-user-input-lab/explore-user-input.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-user-input.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a1988.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2010.zip