how to create new text widget for flutter package?

Issue

I need to create a custom text-widget in the Flutter. I have to change any color it accepts in the style with an opacity of 0.7. If it does not have a style, I have to show the color from the default style with an opacity of 0.7.
Finally, I want to publish it in a package and (independent of the platform text) can be used.

My problem is creating a new text widget with the feature I described.

Solution

when no set the TextStyle to the Text Widget , Flutter Using DefaultTextStyle class.

i solved problem by this class and extension.

class NewTextWidget extends StatelessWidget {
  const NewTextWidget({
    Key? key,
  required this.textWidget,
  }):  super(key: key);

 final Text textWidget;

  @override
  Widget build(BuildContext context) {
    return Text(textWidget.data! ,style:textWidget.style.colorOpacity(context: context),);
  }
}

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));
    }
  }
}

thanks to all.

Answered By – Esmaeil Ahmadipour

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published