1

I have a problem here I want to change color of textbutton from themedata but its not working. Here is my code :

darkTheme: ThemeData(
        primaryColor:Colors.white,
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(primary: Colors.white),
        )
      ),

and my button code :

 TextButton(
            style: TextButton.styleFrom(
             primary: Theme.of(context).primaryColor,
            textStyle: TextStyle(fontSize: 16),),
            onPressed: (){}, child: Text("Hellosir",))
MendelG
  • 14,885
  • 4
  • 25
  • 52

1 Answers1

0

I can think of two problems why this is not working.

  • First, you want to access ThemeData defined in darkTheme, but your themeMode is not dark. So in MaterialApp add themeMode: ThemeMode.dark parameter as well.
  • Second, your button where you call Theme.of(context).primaryColor is inside same widget as your definition of Theme, and your context still doesn't have that data. So only context of children of current widget have this data. Solution would be to make a new widget with your button inside it, or wrap your button with Builder widget which have context inside its builder.

Your problem can be first, second or both.

SadatD
  • 160
  • 7
  • Sir my i defined my ThemeData in darkTheme and my themeMode is dark in MaterailApp but still not working. Darktheme is working but i want textbutton text color white in darkmode and black in lightTheme. Thank you sir if you get my point – Sayed Suliman Jun 30 '22 at 13:48
  • Did you try to wrap your button with `Builder` widget. Can you also share your code so we can be of more help. – SadatD Jun 30 '22 at 14:28
  • Sorry sir You were right i wasn't set themeMode as dark. It worked now thanks. – Sayed Suliman Jul 02 '22 at 01:07
  • If this solved your problem, please select the answer as correct. Cheers – SadatD Jul 04 '22 at 07:34