在哪里为 flutter 中的每个小部件编写逻辑?
我知道这是一个基本问题,但我想知道你们如何实现小部件的逻辑。我通常在返回类型上方的 build() 函数中编写逻辑。
I know this is a basic question but I wanna know how you guy's implement logic for your widgets. I normally write logic in build() function just above the return type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说的逻辑是什么意思??如果您正在谈论添加一些值..那么我们在构建方法之外使用不同的方法,因为当调用设置状态方法时构建方法将重建,因此有时可以在构建内部使用逻辑,但有时我们需要用不同的方法编写逻辑并调用它们..
What do you mean by logic..?? If you are talking about like adding some values.. then we use a different method for that outside the build Method because build method will rebuild when the set state method called so some times logic can be used inside the build but some time we need to write the logic in different method and called them..
每次应用程序状态更改或刷新时,构建方法都会重建
如果你谈论小部件状态
您可以使用状态管理方法(例如(Getx,Provider))将其写入单独的文件中
如果您需要执行 setState 该方法将更新状态,您可以将其编写在单独的函数中
...尝试对逻辑使用单独的函数和控制器,这将帮助您提高代码的可扩展性
Build method will rebuild every time the app state got change or refresh
if you talking about widget state
You can write it in separate file using state management approaches like (Getx,Provider)
You can write it in separate function if you need to perform setState that method will update the state
...Try to use separate functions and controllers for logic it will help you in scalability for your code
如果你在构建方法中编写逻辑意味着每次刷新页面时它都会重建。
我建议您遵循MVVM(模型视图视图模型)架构,以便所有业务逻辑都可以编写在控制器
(例如:cart_controller.dart)
中。这样您的文件与实际的小部件分开。If you write your logic in build method means it will rebuild every time when the page is refreshed.
I will suggest you to follow MVVM(Model View ViewModel) architecture, so that all the business logics can be written in controllers
(ex: cart_controller.dart)
.So that your files are separated from actual widgets.