1.思考下面的转换

2.命令式风格(Imperative style)
我们可以通过findViewById等方式找到b 对象,然后调用b对象内部方法来改变b
// Imperative style
b.setColor(red)
b.clearChildren()
ViewC c3 = new ViewC(...)
b.add(c3)
3.声明式风格(Declarative style)
这种方式是创建一个新的ViewB对象,而不再是改变原来的B对象,
不同UI 之间的转换则是由底层的RenderObjects负责了,我们通过setState来刷新RenderObjects
也就是说我们没有办法直接的命令B做出改变,而是向框架报告一个新的B状态,框架Diff, 更新RenderObjects(长期存在),然后绘制新的屏
// Declarative style
return ViewB(
color: red,
child: ViewC(...),
)
参考 https://flutter.dev/docs/get-started/flutter-for/declarative
网友评论