In Flutter, widgets are the fundamental building blocks of the user interface. Think of them as the Lego bricks that you use to construct your app's UI. Widgets are the visual and interactive elements that users see and interact with on their devices, such as buttons, text fields, images, and so on. See Figure 1.4:
在Flutter中,小部件是用户界面的基本构建块。把它们想象成你用来构建应用UI的乐高积木。小部件是用户在其设备上看到并与之交互的可视和交互式元素,如按钮、文本字段、图像等。见图1.4:
What makes widgets powerful in Flutter is their composability. Just like how you can combine different Lego bricks to create complex structures, you can combine widgets to create intricate and feature-rich user interfaces. Widgets can be nested and arranged in a tree-like structure, forming a hierarchy where each widget serves a specific purpose and contributes to the overall UI.
在Flutter中,小部件的强大之处在于它们的可组合性。就像您可以组合不同的乐高积木来创建复杂的结构一样,您可以组合小部件来创建复杂且功能丰富的用户界面。小部件可以嵌套并排列成树状结构,形成一个层次结构,其中每个小部件都有特定的用途,并为整个UI做出贡献。
Flutter follows a reactive programming model, which means that widgets are not only responsible for defining the visual elements of the UI but also for managing their own state and responding to changes. This allows widgets to be dynamic and responsive to user interactions.
Flutter遵循响应式编程模型,这意味着小部件不仅负责定义UI的可视化元素,还负责管理它们自己的状态并响应更改。这使得小部件能够动态地响应用户交互。
Widgets in Flutter come in two flavors: stateless widgets and stateful widgets. Stateless widgets represent components that don't have any internal state and their appearance is solely determined by the properties they receive. They are useful for building UI elements that don't change based on user interactions, such as static text or icons.
Flutter中的小部件有两种类型:无状态小部件和有状态小部件。无状态小部件表示没有任何内部状态的组件,它们的外观完全由它们接收的属性决定。它们对于构建不随用户交互而改变的UI元素很有用,比如静态文本或图标。
On the other hand, stateful widgets are widgets that can maintain and update their internal state. They are used for UI elements that need to change dynamically based on user actions or changing data. Stateful widgets can handle events, update their state, and trigger UI updates accordingly.
另一方面,有状态小部件是可以维护和更新其内部状态的小部件。它们用于需要根据用户操作或更改数据动态更改的UI元素。有状态小部件可以处理事件、更新其状态并相应地触发UI更新。
One of the key advantages of using widgets in Flutter is the hot reload feature. With hot reload, you can make changes to your widget tree and see the updates instantly without restarting the app. This rapid iteration cycle greatly enhances the developer experience and allows for quick experimentation and fine-tuning of the UI.
在Flutter中使用小部件的一个主要优点是热重新加载特性。通过热加载,你可以对widget树进行更改,无需重新启动应用即可立即看到更新。这种快速迭代周期大大增强了开发人员的体验,并允许快速实验和微调UI。
Don't worry; there is a section in this book dedicated to widgets. We will go over everything about widgets.
别担心;本书中有一节专门讨论小部件。我们将讨论关于小部件的所有内容。