0

I have a TextFormField, which contains a prefix Icon that I would like to turn red when it is focused on.

Unfocused

To achieve that, I have wrapped the TextFormField with a Theme object (Relevant SO Question):

 Theme(data:ThemeData(colorScheme:
                    ThemeData().colorScheme.copyWith(primary:Colors.red)),
                    child: TextFormField()

Red focused color

This works great, but the problem arises when I switch to Dark mode inside my application. Because I am using this setting (I am assuming), it turns the entire TextFormField to use the primary color which is black and then the entire TextFormField is black.

Dark mode

Other TextFormField elements which I have not wrapped with a Theme object, are styled correctly according to theme.

Dark mode other textformfield

How would I go about fixing this?

What I would like to achieve is have the TextFormField appear like the others in Dark mode and be highlighted red when it is focused on, regardless of light or dark mode.

Would I need to define a custom application theme for this?

tomerpacific
  • 4,704
  • 13
  • 34
  • 52

1 Answers1

0

Tru to inherit from current theme by using copyWith method:

Theme(
    data: Theme.of(context).copyWith(
      primaryColor: Colors.red
    ),
      child: TextFormField(),
  )

UPDATE

Or alternatively, you can try using darkTheme property of the MaterialApp widget:

MaterialApp(
  darkTheme: Theme.of(context).copyWith(...),
  home: ...
)
powerman23rus
  • 1,297
  • 1
  • 9
  • 17