我知道我们应该使用组合优于继承 https://stackoverflow.com/a/51477727/1769177在颤振中。当我们谈论时这很有效Widgets
.
但是当一个类不是一个类时我该怎么办Widget
?
例如,我想要我的TextFields
在某些屏幕中具有一组特定的值InputDecoration
.
我应该延长吗InputDecoration
?我怎样才能重用这个特定的InputDecoration
在许多文本字段中?
EDIT:遵循 Rémi Rousselet 的指导,我扩展了InputDecoration
。这是最终结果:
class LoginInputDecoration extends InputDecoration {
@override
InputBorder get errorBorder =>
UnderlineInputBorder(borderSide: BorderSide(color: AppColors.danger));
@override
EdgeInsetsGeometry get contentPadding => const EdgeInsets.symmetric(
horizontal: Dimens.halfSpace,
vertical: Dimens.singleSpace,
);
@override
InputBorder get border =>
UnderlineInputBorder(borderSide: BorderSide(color: AppColors.primary));
@override
TextStyle get labelStyle =>
TextStyle(color: AppColors.white, decorationColor: AppColors.white);
@override
InputBorder get enabledBorder =>
UnderlineInputBorder(borderSide: BorderSide(color: AppColors.primary));
@override
TextStyle get hintStyle =>
TextStyle(color: AppColors.white, decorationColor: AppColors.white);
LoginInputDecoration({String labelText}) : super(labelText: labelText);
}
当然。 “不要扩展小部件”实际上仅限于:小部件。
该“限制”背后的真正原因是扩展小部件不允许正确更改小部件的样式,因为如何build
作品。所以扩展小部件没有意义。
但这个原因并不适用于其他种类的物体。所以不用担心,继续吧!
Flutter 中已经有很多可用的示例。例如,Alignment
是一个子类AlignmentGeometry
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)