[英]How to change opacity of text widget to 0.7 with flutter extension?
I need change style of Text Widget in flutter with an extension.我需要通过扩展更改文本小部件的样式。 this extension must be change opacity of Text Widget to 0.7 .
此扩展必须将 Text Widget 的不透明度更改为 0.7 。 when i used in on Text widget , i can change opacity of widget to 0.7.
当我在 Text 小部件上使用时,我可以将小部件的不透明度更改为 0.7。
UPDATE: if Text widget has style and color, this ok .更新:如果 Text 小部件具有样式和颜色,则可以。 but when not have style receive null error .
但是当没有样式时收到 null 错误。
extension TextStyleOpacity on Text {
Text opacity70(){
return Text("A", style:this.style!.copyWith(color:this.style?.color!.withOpacity(0.3) ) ,) ;
}
}
ok.好的。 I change this extension for text style and solved , answer is :
我将此扩展名更改为文本样式并解决,答案是:
extension TextStyleOpacity on TextStyle?{
TextStyle colorOpacity ({required BuildContext context}){
Color? color =this==null?DefaultTextStyle.of(context).style.color: this?.color;
if(this==null){
return DefaultTextStyle.of(context).style.copyWith(color: color!.withOpacity(0.7));
}else{
return this!.copyWith(color: color!.withOpacity(0.7));
}
}
}
And is used like this example:并像这个例子一样使用:
Text(textWidget.data! ,style:textWidget.style.colorOpacity(context: context),);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.