Search code examples
flutterflutter-test

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 . when i used in on Text widget , i can change opacity of widget to 0.7.

UPDATE: if Text widget has style and color, this ok . but when not have style receive null error .

extension TextStyleOpacity on Text {
  Text  opacity70(){
    return Text("A", style:this.style!.copyWith(color:this.style?.color!.withOpacity(0.3) ) ,) ;
  }
}

Solution

  • 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),);