0

I'd like to be able to instantiate a Text Widget with many proprieties & create a new instance from it but with a new TextStlye by just calling a method on it like so :

  final Text myText = Text('hello', ...);

  final Text myTextWithNewStyle = myText.setNewStyle();

I thought of using an extension on Text but I'm not sure on how to keep all of it's current parameter values

1 Answer 1

1

You can update style like this if it works for you:

TextStyle defultStyle = TextStyle(color: Colors.red);
TextStyle newStyle = defultStyle.copyWith(color: Colors.blue)

or

extension TextExtension on Text {
  Widget setNewStyle(TextStyle newStyle, {Key key}) {
    return Text(this.data,
        key: key,
        style: newStyle,
        strutStyle: this.strutStyle,
        textAlign: this.textAlign,
        textDirection: this.textDirection,
        locale: this.locale,
        softWrap: this.softWrap,
        overflow: this.overflow,
        textScaleFactor: this.textScaleFactor,
        maxLines: this.maxLines,
        semanticsLabel: this.semanticsLabel,
        textWidthBasis: this.textWidthBasis,
        textHeightBehavior: this.textHeightBehavior);
  }
}

and use it like this:

Text text = Text(
      'dasdasd',
      style: TextStyle(color: Colors.blue),
    );
    Widget newText = text.setNewStyle(TextStyle(color: Colors.red));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I was looking for the second answer : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.