I'd like to make some convenience/factory constructors for a few common UI classes. For example, 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?
How about extending TextStyle like below
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.