到第 2 讲为止,画面只显示文件里写着的东西。叫号屏这么做,号一变就得重发一份画面文件。
把位置空出来
{ "type": "page", "title": "Step 3",
"initialState": { "now": "0", "waiting": 0 },
"content": { "type": "center",
"child": { "type": "linear", "direction": "vertical", "spacing": 10, "alignment": "center",
"children": [
{ "type": "text", "text": "NOW SERVING", "style": { "fontSize": 15, "letterSpacing": 5, "color": "#6b7280" } },
{ "type": "text", "text": "{{now}}", "style": { "fontSize": 92, "fontWeight": "bold", "color": "#111827" } },
{ "type": "text", "text": "{{waiting}} waiting", "style": { "fontSize": 20, "color": "#6b7280" } } ] } } }
多了两样。
initialState — 这个画面打开那一刻持有的值,免得一开就看见空白。
{{now}} — 把状态里的 now 放在这儿。可以整格用,也可以像 {{waiting}} waiting 那样嵌在句子里。
把值放进去
像 rt.stateManager.set('now', '42') 这样传键和值,用到那个键的节点就重画。宿主不知道是哪些节点,也不需要知道。
接上客户端课程做的东西,连一个个塞都不必了。
onToolCall: (tool, params) async {
// 服务器返回的 map 原样倒进去
final r = await client.callTool(tool, params);
final state = jsonDecode((r.content.first as TextContent).text)
as Map<String, dynamic>;
state.forEach(rt.stateManager.set);
},
服务器课程第 3 讲让工具返回 {"now": ..., "waiting": ...}。那份 map 的键和画面上 {{...}} 的名字一致时,中间不需要转换代码。