Flutter widget 渲染设计名称?

标签 flutter

我很享受学习 Flutter。当数据发生变化时,整个小部件树都会被重建。然后 Flutter 会将新树与旧树进行比较,并根据需要更新 UI。 这种一般的编程风格叫什么?

这让我想起了几年前我看到的有关 Haskel 和纯函数式编程的一些事情。不要修改现有数据库,而是创建一个全新的数据库并让持久层弄清楚如何有效地存储它。

这与 MVC 完全不同,在 MVC 中,程序员负责知道 UI 的哪些部分需要更新。使用 MVC,有必要知道如何首先构建 UI 并知道如何更新它。对于 Flutter,我们似乎只需要了解第一部分,即构建 UI。

最佳答案

此功能混合了多种范例,尤其是声明式,还有响应式(Reactive)函数式编程。


声明式编程意味着您无需担心事情如何变化。相反,您只需在给定一些数据的情况下描述它们应该是什么样子。然后,框架会找出要更改的内容。这几乎就是您在问题中描述的范例。

函数式编程是指您提供给小部件构造函数的输入与小部件的 build 方法的“输出”之间的确定性关系。它描述的内容与声明式编程几乎相同,但重点关注输入和输出之间的关系,而不是一般的样式。 StatelessWidget非常适合这种范例。

响应式(Reactive)编程是指对小部件构造函数的更改波及到较低级别的小部件并且事件向上冒泡的模式。它更关注嵌套小部件如何相互交互。

这是 Flutter FAQ 的摘录。在该网站上,他们还描述了 Flutter 使用的其他几种范例。

Flutter is a multi-paradigm programming environment. Many programming techniques developed over the past few decades are used in Flutter. We use each one where we believe the strengths of the technique make it particularly well-suited. In no particular order:

Declarative programming

The build methods of widgets are often a single expression with multiple levels of nested constructors, written using a strictly declarative subset of Dart. Such nested expressions could be mechanically transformed to or from any suitably expressive markup language. For example, the UserAccountsDrawerHeader widget has a long build method (20+ lines), consisting of a single nested expression. This can also be combined with the imperative style to build UIs that would be harder to describe in a pure-declarative approach.

Reactive programming

The widget and element trees are sometimes described as reactive, because new inputs provided in a widget’s constructor are immediately propagated as changes to lower-level widgets by the widget’s build method, and changes made in the lower widgets (for example, in response to user input) propagate back up the tree via event handlers. Aspects of both functional-reactive and imperative-reactive are present in the framework, depending on the needs of the widgets. Widgets with build methods that consist of just an expression describing how the widget reacts to changes in its configuration are functional reactive widgets (for example, the Material Divider class). Widgets whose build methods construct a list of children over several statements, describing how the widget reacts to changes in its configuration, are imperative reactive widgets (for example, the Chip class).

Functional programming

Entire applications can be built with only StatelessWidgets, which are essentially functions that describe how arguments map to other functions, bottoming out in primitives that compute layouts or paint graphics. (Such applications can’t easily have state, so are typically non-interactive.) For example, the Icon widget is essentially a function that maps its arguments (color, icon, size) into layout primitives. Additionally, heavy use is made of immutable data structures, including the entire Widget class hierarchy as well as numerous supporting classes such as Rect and TextStyle. On a smaller scale, Dart’s Iterable API, which makes heavy use of the functional style (map, reduce, where, etc), is frequently used to process lists of values in the framework.

关于Flutter widget 渲染设计名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189623/

相关文章:

firebase - 如何将 firebase 存储中的图像显示到小部件中?

flutter - 如何修复文本在 flutter 中滚动图像

firebase - Firestore 为 noSQL 和 Flutter 复制 SQL Join

flutter - BLE 模块问题 : Exception: Another scan is already in progress

flutter - 如何让服务器知道根据指纹 flutter 发送什么数据?

ios - 由于 podfile 中的模块化 header 设置,Flutter iOS 无法构建

flutter - Assets 看不到子文件夹

android - 试图在 flutter 中创建一个没有 DRY 的 switchListTile

ios - Sirikit与Flutter集成

firebase - Flutter:当 TextView 聚焦时,屏幕键盘不打开