[英]How to add a constructor to an existing class in Dart?
I'd like to make some convenience/factory constructors for a few common UI classes. 我想为一些常见的UI类提供一些便利/工厂构造函数。 For example, TextStyle:
例如,TextStyle:
enum Font {
AvenirNext,
AvenirNextCondensed
}
enum Weight {
Regular,
Medium,
DemiBold,
Bold
}
// Use:
// TextStyle(Fonts.AvenirNext, Weight.Medium, 20, Colors.white)
// Maybe it can be even shorter?
// TextStyle(AvenirNext, Medium, 20, Colors.black)
How to go about this in Dart? 如何在Dart中进行此操作?
How about extending TextStyle like below 如何像下面那样扩展TextStyle
class Font {
static const AvenirNext = 'AvenirNext';
static const AvenirNextCondensed = 'AvenirNextCondensed';
}
class Weight{
static const Regular = FontWeight.w400;
static const Medium = FontWeight.w500;
static const DemiBold = FontWeight.w700;
static const Bold = FontWeight.w900;
}
class CTextStyle extends TextStyle {
CTextStyle(String f, FontWeight w, num s, Color c)
: super(
fontFamily: f,
fontWeight: w,
fontSize: s.toDouble(),
color: c);
}
}
usage: 用法:
TextStyle myCustom = CTextStyle(Font.AvenirNext, Weight.Medium, 20, Colors.black);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.