I want to have an Widget that expands the functionality of an existing Widget.
Example: I want an "ResponisveText
"-Widget. It should have the exact same properties as the existing Text
-Widget but with the difference that the fontSize is handled different here.
How can I create such an Widget in an clean way, so that I have access to the fontSize etc.?
Before, I did it like this, but I think there’s a better way:
Text('foo', style: Font.body1.copyWith(fontSize: responsiveFontSize(Font.body1.fontSize))
I want It to be like this:
ResponsiveText('foo', style: Font.body1)
2
Answers
That would be my solution:
You utilize native headline (or other ThemeOfContext) parameters that you can modify by hand in the file that handles app ui.
you can achieve what you want by extending the
Text
widget, and then instead of using directly thestyle
property, you declare a newstyle
parameter, that before assigning its value to thesuper.style
parameter, you can manipulate yourfontSize
value as you wish.let me know if you have any question