[英]How can I enforce certain parameter values for Flutter widgets of a given type?
For instance, I am working on an app with many screens, so I am creating lots of Scaffold
s and AppBar
s.例如,我正在开发一个有很多屏幕的应用程序,所以我创建了很多Scaffold
和AppBar
。
Say my design spec says that "the appbar title should be centered always, on all platforms", ie I want the centerTitle
argument to AppBar
to always be true
.假设我的设计规范说“appbar 标题应该始终居中,在所有平台上”,即我希望AppBar
的centerTitle
参数始终为true
。
I could wrap AppBar
with a custom widget MyAppBar
and always set centerTitle
to true
, but我可以用自定义小部件MyAppBar
包装AppBar
并始终将centerTitle
设置为true
,但是
AppBar
properties这意味着要公开所有其他AppBar
属性的大量样板文件AppBar
这需要烦人的维护以跟上AppBar
的框架更改Is there an option better than "just try to remember to set it everywhere you create an AppBar
"?有没有比“试着记住在你创建AppBar
的任何地方设置它”更好的选项?
Making a custom MyAppBar
widget that always center the title is the correct solution.制作始终居中标题的自定义MyAppBar
小部件是正确的解决方案。
But the premise that is has a lot of boilerplate/hard to maintain is false.但是有很多样板/难以维护的前提是错误的。
You don't need all AppBar
properties.您不需要所有AppBar
属性。 Only expose those that are relevant to your app.仅公开与您的应用相关的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.