简体   繁体   English

Flutter 将样式应用于子小部件是否有必要将其包装在容器小部件中

[英]Flutter to apply styling to the child widget Is it necessary to wrap it inside a Container Widget

Is it necessary to use wrap another widget (ie Text, image etc) inside the Container widget as a child, if we want to apply styling to the child widget?如果我们想将样式应用于子小部件,是否有必要在 Container 小部件中使用包装另一个小部件(即文本、图像等)作为子小部件? In other words, can I apply styling to my Text or Image widgets without wrapping them inside Container widgets?换句话说,我可以将样式应用于我的 Text 或 Image 小部件而不将它们包装在 Container 小部件中吗?

Yes its is necessary to wrap your widget using a Container or DecoratedBox .是的,有必要使用ContainerDecoratedBox包装您的小部件。

The reason is that in flutter decorations are applied using the BoxDecoration class.原因是在 flutter 中使用BoxDecoration class 应用装饰。 If you want use styles like Borders, Background Colors Shadows etc you would need to use a Widget that uses a BoxDecoration .如果您想使用 styles 之类的边框、背景 Colors 阴影等,则需要使用使用BoxDecoration的 Widget。

If you don't want to use a Container you can use the DecoratedBox widget provided by flutter amongst others.如果您不想使用Container ,可以使用 flutter 等提供的DecoratedBox小部件。

If you want to apply styles only to your text data, then you don't need to use Container or DecoratedBox , as pure text styles can be provided to the Text Widget, by providing the style property with the a TextStyle Class.如果您只想将 styles 应用于您的文本数据,那么您不需要使用ContainerDecoratedBox ,因为纯文本 styles 可以提供给Text小部件,通过提供带有TextStyle Z9BD81329FEDBF6EFEZ22 的样式属性。

DecoratedBox Widget Docs DecoratedBox 小部件文档

BoxDecoration Docs BoxDecoration 文档

TextStyle Docs文本样式文档

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

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