What is the proper way of using ThemeData copyWith in flutter MaterialApp widget?

Viewed 4812

I was trying to change the accentColor after copying the ThemeData.light(), then I have this sample screen with a FloatingActionButton

class Sample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ),
     )}}

Then in the main widget in main.dart to call runApp, if I set the ThemeData for the MaterialApp widget like this, The FloatingActionButton will have a color of orange.

theme: ThemeData(
  accentColor: Colors.orange
)

but if I tried to inherit the color from the Themedata.light().copyWith, the FloatingActionButton will still have the color blue from the light theme.

theme: ThemeData.light().copyWith(
  accentColor: Colors.orange
)

I was expecting the FloatingActionButton should have the orange color, because It inherit the light theme and override the accentColor.

2 Answers

look, I don't think that u can inherit the accent color using the ThemeData.copyWith(),but if u are sure to use ThemeData.copyWith() to change your floatingActionButton Color then u can do this in the following way,

theme:ThemeData.dark().copyWith(
    textTheme:ThemeData.dark().textTheme.copyWith(
        title :TextStyle( --your color and text configuratons here like color,font etc)
        button: TextStyle(--do--),
        ...and so on....
    )
)

configurations u want in your default text title will go inside the TextStyle of above title property and same for button

Now u can achieve this inside your FAB by using this,

color: Theme.of(context).textTheme.button.color,

by doing this u can get the color u set for the buttons inside the ThemeData.

If u force on getting the default accent color then u have to use

theme:ThemeData(
    primaryColor: -----
    accentColor : -----
)

this will allow u to use Default accent Color for FAB

Related