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

Viewed 1873

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?

3 Answers

Wrap it with DefaultTextStyle, eg:

    DefaultTextStyle.merge(
        style: TextStyle(color: Colors.grey[400], fontSize: 12),
        child: Column(...),
    )

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

Something like this?

child: Text(
        'Post',
         style: TextStyle(color: Colors.white),),
Related