简体   繁体   English

如何为给定类型的 Flutter 小部件强制执行某些参数值?

[英]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.例如,我正在开发一个有很多屏幕的应用程序,所以我创建了很多ScaffoldAppBar

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 标题应该始终居中,在所有平台上”,即我希望AppBarcenterTitle参数始终为true

I could wrap AppBar with a custom widget MyAppBar and always set centerTitle to true , but我可以用自定义小部件MyAppBar包装AppBar并始终将centerTitle设置为true ,但是

  1. This implies a lot of boilerplate to expose all of the other AppBar properties这意味着要公开所有其他AppBar属性的大量样板文件
  2. This requires annoying maintenance to keep up with framework changes to 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.

相关问题 如何使用颤振构建小部件? - how can I build Widgets using flutter? 如何禁用 flutter 中的背景小部件 - How can I disable background widgets in flutter 如何恢复 flutter 内置小部件? - How can I restore flutter built in widgets? Flutter:如何在构造函数中传递值,以便重用我的小部件? - Flutter: How can I pass values in the constructor so I can reuse my widgets? 如何根据 Flutter 中的 ListView 选择显示某些小部件? - How do I display certain widgets based on a ListView selection in Flutter? 如何在 Flutter 中经过一定时间后切换小部件? - How to Switch Widgets after certain time in Flutter? 如何解决“无法将参数类型'String'分配给参数类型'int'” - Flutter - How can I resolve "The argument type 'String' can't be assigned to the parameter type 'int' " - Flutter 如何解决“参数类型'字符串?' 无法分配给参数类型 'String'" - Flutter - How can I resolve "The argument type 'String?' can't be assigned to the parameter type 'String' " - Flutter 如何将 Admob 的广告添加到 Flutter 小部件树? - How can i add Admob's ads to Flutter widgets tree? 如何将一些小部件添加到 Flutter 行中的兄弟姐妹的 position - How can I add some widgets to the position of siblings in the row at Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM