简体   繁体   English

Flutter:无状态小部件中的可变字段

[英]Flutter: Mutable fields in stateless widgets

The class StatelessWidget is marked as immutable .StatelessWidget被标记为immutable However, I am using the scoped model , which means that I avoid StatefulWidget and use the model to alter state in StatelessWidget .不过,我使用的scoped model ,这意味着我避免StatefulWidget和使用model来改变stateStatelessWidget This leads to me having non-final fields in StatelessWidget , which doesn't cause errors , because it's just a warning .这导致我在StatelessWidgetnon-final fields ,这不会导致errors ,因为它只是一个warning But I wondered if there is a better way?但我想知道是否有更好的方法?

Stateless widgets should only have final fields, with no exceptions .无状态小部件应该只有 final 字段,没有例外 Reason: When the parent widget is rebuilt for some reason (screen rotation, animations, scrolling...), the build method of the parent is called, which causes all widgets to be reconstructed.原因:当父widget由于某种原因(屏幕旋转、动画、滚动...)重建时,会调用parent的build方法,导致所有widget都被重建。

Classes the extend StatefulWidget must follow the same rule, because those are also reconstructed.扩展StatefulWidget类必须遵循相同的规则,因为它们也是重构的。 Only the State , which can contain mutable fields, is kept during the lifetime of widget in the layout tree.在布局树中小部件的生命周期中,只有State可以包含可变字段。

There is no reason to avoid StatefulWidget .没有理由避免StatefulWidget It is a fundamental building block of Flutter.它是 Flutter 的基本构建块。

In fact, ScopedModelDescendant is also a stateful widget.事实上, ScopedModelDescendant也是一个有状态的小部件。 The primary benefit of scoped_model is that you can separate the business logic from the widget layer. scoped_model 的主要好处是您可以将业务逻辑与小部件层分开。 It doesn't eliminate the need for stateful widgets.它并没有消除对有状态小部件的需求。

Use stateful widgets for:将有状态小部件用于:

  • Injecting scoped models into the tree (the widget that builds the ScopedModel widget).将范围模型注入树(构建ScopedModel小部件的小部件)。 Store the Model instance in the State .Model实例存储在State
  • Storing user input ( TextEditingController , state of a checkbox)存储用户输入( TextEditingController ,复选框状态)
  • Animated widgets which require AnimationController s需要AnimationController的动画小部件
  • To store anything that ends with Controller ( TabController , ScrollController , ...)存储以Controller结尾的任何内容( TabControllerScrollController ,...)

It is often a good idea to make the "page" widgets (widgets which build a Scaffold , accessible using the Navigator ) stateful.使“页面”小部件(构建Scaffold小部件,可使用Navigator访问)有状态通常是一个好主意。 Often these are the hosts for scoped models.通常这些是作用域模型的宿主。

Here's you question:这是你的问题:

Do you think there is a better approach to accomplish what I want while keeping the "short class structure" that helps me keep oversight and being able to trigger rebuilds of those classes from anywhere?你认为有没有更好的方法来完成我想要的,同时保持“短类结构”来帮助我保持监督并能够从任何地方触发这些类的重建?

What you're asking for here ↑ seems to be one more app state management approach that should be better than scoped model .你在这里要求的 ↑ 似乎是另一种应该比 scoped model 更好的应用程序状态管理方法

As you know, app state management approaches are a set of techniques that allow you as a developer:如您所知,应用程序状态管理方法是一组技术,可让您作为开发人员:

  • to bind data with widgets .将数据与小部件绑定

Binding data with widgets, in turn, helps you as a developer:反过来,将数据与小部件绑定可以帮助您作为开发人员:

  • to rebuild widgets automatically on every change of bound data .在每次更改绑定数据时自动重建小部件

Maybe, for that purpose, you can make use of rxdart :也许,为此,您可以使用rxdart

Here you will find some very helpful list of app state management approaches that can lead you to a better way of app development:在这里,您会找到一些非常有用的应用状态管理方法列表,它们可以引导您获得更好的应用开发方式:

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

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