简体   繁体   English

是否可以为可重用的小部件创建单独的 Getx 控制器?

[英]Is it possible to create a separate Getx Controller for a reusable widget?

Is it possible to use getx controller to manage state for reusable widget type?是否可以使用 getx 控制器来管理可重用小部件类型的状态? I want to create a small widget such as an elegant number button widget for the shopping cart section with each widget having a separate controller that does not share data with each other.我想为购物车部分创建一个小部件,例如优雅的数字按钮小部件,每个小部件都有一个单独的控制器,彼此不共享数据。 Is it possible to do so and should it be done?有可能这样做吗?应该这样做吗? Please advise me.请给我提意见。 Sorry for my bad English对不起,我的英语不好

Yes, it looks like Magic, Get will find your controller, and will deliver it to you.是的,它看起来像 Magic,Get 会找到您的控制器,并将其交付给您。 You can have 1 million controllers instantiated, Get will always give you the right controller.您可以实例化 100 万个控制器,Get 将始终为您提供正确的控制器。

Controller controller = Get.find();

No you cannot.你不能。 It is always recommended to keep the the reusable widget a stateless widget that takes data to show in widget view.始终建议将可重用小部件保留为无状态小部件,该小部件将数据显示在小部件视图中。 Then on top of that reusable widget you can wrap with GetBuilder and give its own controller that can have its own data separate.然后在该可重用小部件之上,您可以使用 GetBuilder 进行包装,并提供自己的控制器,该控制器可以将自己的数据分开。

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

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