How to extend a widget to override some properties of it?

Viewed 263

How do I override a widget in order to provide some custom modifications to one of its property.

For example: Let's say I want to create my own Text widget which will convert whole text to uppercase. I'll do something like:

class MyOwnText extends Text {
  MyOwnText(String data) : super(data.toUpperCase());
}

But with this approach, I can't use other properties of Text in my own widget, style, for example. For that, I'll have to add style property in my class constructor like this:

MyOwnText(String data, {TextStyle style}) : super(data.toUpperCase(), style: style);

But Text has got around 12-13 properties, and just to override one, I need to define all of those properties and then their assertion. I'm sure I may not be doing something right. Can anyone please help?


Note: Neither I want to use extension methods nor some client side code. MyOwnText should be a Text.

1 Answers

I get what you wanted to achieve, but I do not recommend to do this.

Why?? - Because most of the class has private variable and they have their separate getter and setter which are expose to the outer front of the class.

Again, If you wanted to for design and func. then you should not extend the widget. Instead you can directly use those in your build method

Why?? - You can't inherit more than one class(Mixin is other way around here)

So ultimately you need to assign properties directly or you could use spread operator

Related