简体   繁体   English

Flutter 小部件渲染设计名称?

[英]Flutter widget rendering design name?

I'm enjoying learning about Flutter.我很享受学习 Flutter 的过程。 When data changes, a whole tree of widgets is rebuilt.当数据发生变化时,会重建整个小部件树。 Then Flutter compares the new tree with the old tree and updates the UI as necessary.然后 Flutter 将新树与旧树进行比较,并根据需要更新 UI。 What is this general style of programming called?这种通用的编程风格叫什么?

It reminds me of some things I saw about Haskel years ago and pure functional programming.这让我想起了几年前我看到的一些关于 Haskel 和纯函数式编程的事情。 Instead of modifying an existing database, create a whole new database and let the persistence layer figure out how to store it efficiently.与其修改现有的数据库,不如创建一个全新的数据库,让持久层弄清楚如何有效地存储它。

This is totally different from MVC where the programmer is responsible to know which parts of the UI need to be updated.这与 MVC 完全不同,MVC 程序员负责知道 UI 的哪些部分需要更新。 With MVC it's necessary to know how to first build the UI and also know how to update it.对于 MVC,有必要知道如何首先构建 UI 并知道如何更新它。 With Flutter we only need to know the first part, building the UI, it seems.使用 Flutter,我们似乎只需要了解第一部分,即构建 UI。

This feature is a mixture of multiple paradigms, especially declarative , but also reactive and functional programming.此功能是多种范式的混合,尤其是声明式编程,但也包括响应式函数式编程。


Declarative programming means that you don't need to worry about how things change.声明式编程意味着您无需担心事情会如何变化。 Rather, you just describe how they should look, given some data.相反,您只需根据一些数据描述它们的外观。 Then, the framework figures out what to change.然后,框架确定要更改的内容。 That's pretty much the paradigm you described in your question.这几乎就是您在问题中描述的范式。

Functional programming refers to a deterministic relationship between the input you give to a widget constructor and the "output" of the widget's build method.函数式编程是指您提供给小部件构造函数的输入与小部件build方法的“输出”之间的确定性关系。 That describes almost the same thing as declarative programming but focuses on the relationship between the input and output rather than style in general.这与声明式编程几乎相同,但侧重于输入和输出之间的关系,而不是一般的风格。 StatelessWidget s fit this paradigm pretty well. StatelessWidget非常适合这种范式。

Reactive programming refers to the pattern that changes to the widgets' constructors ripple down into lower-level widgets and events bubble back up.响应式编程是指对小部件构造函数的更改向下波及到较低级别的小部件和事件冒泡的模式。 It focuses more on how nested widgets interact with each other.它更多地关注嵌套小部件如何相互交互。

Here's an excerpt from the Flutter FAQ .这是Flutter FAQ的摘录。 On the site, they also describe several other paradigms Flutter uses.在网站上,他们还描述了 Flutter 使用的其他几种范式。

Flutter is a multi-paradigm programming environment. Flutter 是一个多范式的编程环境。 Many programming techniques developed over the past few decades are used in Flutter. 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.小部件的构建方法通常是具有多层嵌套构造函数的单个表达式,使用 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.例如,UserAccountsDrawerHeader 小部件有一个很长的构建方法(20 多行),由单个嵌套表达式组成。 This can also be combined with the imperative style to build UIs that would be harder to describe in a pure-declarative approach.这也可以与命令式风格相结合,以构建在纯声明式方法中更难描述的 UI。

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).具有构建方法的小部件仅由描述小部件如何对其配置中的更改做出反应的表达式组成,是功能性反应小部件(例如,Material Divider 类)。 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).其构建方法在多个语句上构建子项列表的小部件,描述小部件如何对其配置中的变化做出反应,是命令式反应小部件(例如,Chip 类)。

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.整个应用程序只能使用 StatelessWidgets 构建,这些函数本质上是描述参数如何映射到其他函数的函数,在计算布局或绘制图形的基元中达到最低点。 (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.此外,大量使用不可变数据结构,包括整个 Widget 类层次结构以及众多支持类,例如 Rect 和 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.在较小的范围内,Dart 的 Iterable API 大量使用函数样式(map、reduce、where 等),经常用于处理框架中的值列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM