Changing the trailing icon color in ExpansionTile

21k views Asked by At

I need to change the color of the trailing keyboard_down_arrow in ExpansionTile. I have tried wrapping it in Theme widget and setting accent, primary and IconTheme also, but nothing seems to work.

Theme(
                  data: Theme.of(context).copyWith(
                    dividerColor: Colors.transparent,
                    accentColor: Colors.black,
                  ),
                  child: ExpansionTile(
                    //
                    title: Text("Some Text"
                    ),
                    childrenPadding: EdgeInsets.symmetric(horizontal: 15),
                    children: [
                      
                    ],
                  ),
                ),
5

There are 5 answers

3
TarushiAg On BEST ANSWER

I found the solution to the above problem.

set unselectedWidgetColor property to the color you want in Theme class in flutter.

1
MRazaImtiaz On

To change the trailing icon Color you can use the fallowing parameter in Expansion Tile

 trailing: Icon(
              Icons.keyboard_arrow_down,
              color: Colors.green,
            ),

Example:

 ExpansionTile(
                //
                title: Text("Some Text"),
                trailing: Icon(
                  Icons.keyboard_arrow_down,
                  color: Colors.green,
                ),
              ),

And for theme Color use color: Theme.of(context).primaryColor,

1
Kris On

I have latest solution with both open/close state:

Theme(
    data: Theme.of(context).copyWith(
      unselectedWidgetColor: Colors.white, // here for close state
      colorScheme: ColorScheme.light(
          primary: Colors.white,
      ), // here for open state in replacement of deprecated accentColor
      dividerColor: Colors.transparent, // if you want to remove the border
    ),
    child: ExpansionTile(
        ...
    ),
),...
0
Maciej Szakacz On

ExpansionTile has a properties:

iconColor: Colors.black,
collapsedIconColor: Colors.orange,

It changes the color of the trailing icon depending on if the tile is expanded or not.

0
efe cankat türkmen On

If you need more dynamic/general solution;

    ExpansionTileThemeData expansionTileTheme(ColorScheme colors) {
    return ExpansionTileThemeData(iconColor: Colors.white);
  }

use this theme data in your theme.dart file that you use for theme light & dark like this

   ThemeData light([Color? targetColor]) {
    final colorScheme = colors(Brightness.light, targetColor);
    return ThemeData.light().copyWith(
      // unselectedWidgetColor: colorScheme.onPrimary,
      expansionTileTheme: expansionTileTheme(colorScheme),);
  }

in main.dart

 MaterialApp.router(            
  theme: theme.light(Color),
  );

This will affect your whole project, so you won't need to implement this color in every expansion tile that you will use.