How can I change the text of all child widgets inside a parent widget?
Solution 1
You could wrap mySpecialColumn
in Theme
widget. Then use ThemeData
to set the text color of its children.
Theme(
// Create a unique theme with "ThemeData"
data: ThemeData(
textTheme: TextTheme(
body1: TextStyle(
color: Colors.red
)
)
),
child: mySpecialColumn(
...
)
);
For more details see the docs
Solution 2
Wrap it with DefaultTextStyle
, eg:
DefaultTextStyle.merge(
style: TextStyle(color: Colors.grey[400], fontSize: 12),
child: Column(...),
)
Magnus
I have delivered value. But at what cost? Bachelor of Science degree in Computer Engineering. ✪ Started out on ATARI ST BASIC in the 1980's, writing mostly "Look door, take key" type games. ✪ Spent a few years in high school writing various small programs for personal use in Delphi. ✪ Learned PHP/SQL/HTML/JS/CSS and played around with that for a few years. ✪ Did mostly Android and Java for a few years. ✪ Graduated from Sweden Mid University with a BSc in Computer Engineering. At this point, I had learned all there was to know about software development, except where to find that darn "any" key... ✪ Currently working with Flutter/Dart and Delphi (again).
Updated on December 12, 2022Comments
-
Magnus over 1 year
I have a convenience method that returns a
Column
with someText
widgets inside it, let's call itmySpecialColumn(String header, String body)
. Now I have a situation where I want to change the text color in one particular place where I use this method. Since this is a very rare exception, adding a (optional)Color
parameter to the method signature ofmySpecialColumn
seems overkill.I found out that I can wrap this instance of my custom widget with
Material
, since it has atextStyle
property. This seems to work fine, but is this the preferred way to do this?Perhaps I'm wrong about this, but
Material
seems like it should mainly be used for creating customMaterial
widgets, not something as trivial as changing text color? I would have expected there to be some common widget likeContainer
or similar that could alter the text color of it's children?