[英]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.dart和LoginTablet.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.