在Flutter 開發(fā)中Text
組件用到的也挺多的唯蝶,不過基本都用Container
來包一層來設(shè)置間距大小等約束坑鱼,為此我也簡(jiǎn)單封裝一份画侣,請(qǐng)大家過目:
相關(guān)代碼如下:
/// Text 封裝
class WLTextContainer extends StatelessWidget {
final String text;
final Color color;
final double width;
final double height;
final Color bgColor;
final int maxLines;
final double fontSize;
final TextAlign align;
final Function onClickTxt;
final StrutStyle strutStyle;
final TextOverflow overflow;
final Decoration decoration;
final Alignment baseAlignment;
final EdgeInsetsGeometry margin;
final EdgeInsetsGeometry padding;
final BoxConstraints constraints;
final TextDirection textDirection;
const WLTextContainer({
Key key,
this.text,
this.width,
this.height,
this.margin,
this.padding,
this.onClickTxt,
this.strutStyle,
this.decoration,
this.constraints,
this.maxLines = 1,
this.fontSize = 16,
this.textDirection,
this.color = Colors.white,
this.align = TextAlign.start,
this.bgColor = Colors.transparent,
this.overflow = TextOverflow.ellipsis,
this.baseAlignment = Alignment.center,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
margin: margin,
color: bgColor,
padding: padding,
child: WLContainer(
onClick: onClickTxt,
decoration: decoration,
alignment: baseAlignment,
constraints: constraints,
child: Text(
text != null ? text.trimLeft().trimRight() : "",
textAlign: align,
overflow: overflow,
maxLines: maxLines,
strutStyle: strutStyle,
textDirection: textDirection,
// style: TextStyle(color: color, fontSize: scaleValue(fontSize)),
style: TextStyle(color: color, fontSize: fontSize),
),
),
);
}
}
使用如下:
WLTextContainer(
width: 222,
height: 60,
fontSize: 20,
text: "去同步",
margin: insetT(15),
onClickTxt: () {
print('去同步');
},
);
查看WLContainer
:傳送門
這樣就不用Container
先包一層啤握,然后再把Container
用InkWell
包上加點(diǎn)擊事件了非洲,一次性都包裝好了未状,后期就方便使用了俯画,代碼層級(jí)也不那么深了。
我覺得給我節(jié)省了一些不必要的寫代碼時(shí)間司草。
大家覺得合適就拿過去使用吧艰垂,有什么不合適的可以留言泡仗,我可以升級(jí)調(diào)整優(yōu)化,謝謝啦~~~