How can I change the text of all child widgets inside a parent widget?

1,124

Solution 1

You could wrap mySpecialColumn in Theme widget. Then use ThemeDatato 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(...),
    )
Share:
1,124
Magnus
Author by

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, 2022

Comments

  • Magnus
    Magnus over 1 year

    I have a convenience method that returns a Column with some Text widgets inside it, let's call it mySpecialColumn(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 of mySpecialColumn seems overkill.

    I found out that I can wrap this instance of my custom widget with Material, since it has a textStyle 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 custom Material widgets, not something as trivial as changing text color? I would have expected there to be some common widget like Container or similar that could alter the text color of it's children?