简体   繁体   English

将“多个”有状态和无状态小部件存储在一个 dart 文件中。 没事吧?

[英]Store “multiple” Stateful and Stateless Widgets in one dart file. Is it okay?

I'm slightly new to Flutter and just want to ask some questions/clarifications regarding the development using it.我对 Flutter 有点陌生,只是想就使用它的开发提出一些问题/澄清。 I am currently building a flutter app and wanted to change the layout based on different devices and orientations.我目前正在构建一个 flutter 应用程序,并希望根据不同的设备和方向更改布局。 I created 2 different dart files containing different app layouts of my Login page ( LoginMobile.dart and LoginTablet.dart respectively).我创建了 2 个不同的 dart 文件,其中包含我的登录页面的不同应用程序布局(分别为LoginMobile.dartLoginTablet.dart )。 I created also a separate dart file (LoginComponents.dart) to store "all" the object UI/components of my login form (txtEmail, txtPassword, btnLogin, etc.).我还创建了一个单独的 dart 文件 (LoginComponents.dart) 来存储“所有”我的登录表单的 object UI/组件(txtEmail、txtPassword、btnLogin 等)。 I heard doing like Widget txtEmail() {return TextformField(...);} is not advisable as it can affect the app performance, so I tried making them as classes.我听说像Widget txtEmail() {return TextformField(...);}这样的做法是不可取的,因为它会影响应用程序的性能,所以我尝试将它们作为类。 Am I doing it right?我做对了吗? Is it okay to store multiple stateful widgets in one dart file(?) since the txtPassword have a setState() for show/reveal password and the btnLogin for the authentication process.是否可以在一个 dart 文件中存储多个有状态的小部件(?),因为 txtPassword 具有用于显示/显示密码的 setState() 和用于身份验证过程的 btnLogin。 Is there any negative effects that I may face in the long run if I continue doing it this way?如果我继续这样做,从长远来看,我可能会面临任何负面影响吗? Any tips and advise were highly appreciated.任何提示和建议都受到高度赞赏。 Thanks!谢谢!

Storing your widgets in a single file is okay but would cause confusion when making a large app.将小部件存储在单个文件中是可以的,但在制作大型应用程序时会引起混乱。 when the amount of widgets is getting increased in that file it would be harder to do a small change because its harder to find the widget.当该文件中的小部件数量增加时,进行小的更改将更加困难,因为它更难找到小部件。

i would recommend using multiple files, so you can find them and organize them easily.我建议使用多个文件,这样您就可以轻松地找到它们并组织它们。

Yes, you can.是的你可以。 But point comes to maintainability.但重点是可维护性。 I prefer to keep one public widget which having the same name as the filename and remaining private widgets.我更喜欢保留一个与文件名同名的公共小部件和剩余的私有小部件。

So now ques is How many widgets in a single file?所以现在的问题是一个文件中有多少个小部件?

Its actually depend there is no such rule to restrict the limit of file.它实际上取决于没有这样的规则来限制文件的限制。 Different authors having different preference.不同的作者有不同的偏好。 I prefer try to keep 5-6 classes(widgets) and each one having 5-6 functions.我更喜欢尝试保留 5-6 个类(小部件),每个类有 5-6 个功能。

  • Try to make a file single responsible ie(5-6 classes together responsible for single functionality).尝试使文件单一负责,即(5-6 个类一起负责单一功能)。 Don't make god class which having unrelated concerns together later it will pains(haha)不要让上帝 class 以后有无关的问题会很痛苦(哈哈)

  • If it's a common widget keep them separate to respect DRY principle(Don't repeat yourself)如果它是一个常见的小部件,请将它们分开以尊重 DRY 原则(不要重复自己)

  • If the widget is further divided into 3-4 widgets or it children widget change depend upon rest response keep seprate for good practise如果小部件进一步分为 3-4 个小部件,或者它的子小部件更改取决于 rest 响应保持单独的良好做法

  • Bonus Tip: try using code folding shortcuts to push a little more额外提示:尝试使用代码折叠快捷方式来推动更多

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

相关问题 Flutter 中有状态和无状态小部件之间的关系是什么? - What is the relation between stateful and stateless widgets in Flutter? 在无状态或有状态小部件外部使用上下文 - Using context outside stateless or stateful widgets 无状态和有状态小部件之间有什么区别? - What difference between stateless and stateful widgets? 无状态小部件到有状态小部件转换 Dart Flutter - Stateless widget to Stateful widget convert Dart Flutter Flutter - 两个有状态的一个无状态或两个无状态的一个有状态 - Flutter - Two stateful in one stateless or two stateless in one stateful 在 Flutter 中使用 BLoC - 在有状态小部件与无状态小部件中的使用 - Using BLoC in Flutter - Usage in Stateful widgets vs Stateless Widgets 试图将无状态小部件转换为有状态小部件。 我将 main.dart 中的关键字 stateless 替换为 stateful - Trying to convert stateless widget to stateful widget. I replaced the keyword stateless to stateful in main.dart 为什么有状态/无状态小部件扩展而不实现? - Why stateful/stateless widgets extends and doesn't implements? 无状态还是有状态? - Stateless or Stateful? 如何访问在一个文件中的有状态小部件中创建的列表到另一个文件中的无状态小部件? - How to access a list created in a stateful widget in one file to stateless widget in another file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM