27

Goal: Changing the color of the prefixIcon next to the TextField when clicking on the TextField.

TextField(
  decoration: InputDecoration(
    prefixIcon: Icon(Icons.lock_outline),
    hintText: 'Username'
  )
)
NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Marco Zielbauer
  • 576
  • 1
  • 7
  • 18

9 Answers9

21

When Selected the color shown is app primaryColor: of Theme.

One Way of changing is using Theme Widget.

 Theme(
                              child: TextField(
                                decoration: InputDecoration(
                                  prefixIcon: Icon(Icons.email),
                                  labelText: "Email",
                                  hintText: "example@mail.com",
                                ),
                                autofocus: true,
                              ),
                              data: Theme.of(context)
                                  .copyWith(primaryColor: Colors.redAccent,),
                            ),

Other is to change primaryColor at MaterialApp Theme level.

anmol.majhail
  • 48,256
  • 14
  • 136
  • 105
13

In Flutter 2.5, you can set the active color of the icon in ColorScheme.primary:

theme: ThemeData().copyWith(
  colorScheme: ThemeData().colorScheme.copyWith(
    primary: Colors.green,
  ),
),

Live Demo

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
10

As per flutter's update, accentColor property has been deprecated.

https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties

OLD:

theme: ThemeData(
  accentColor: Colors.blue,
),

NEW:

theme: ThemeData(
  colorScheme: ThemeData().colorScheme.copyWith(
    secondary: Colors.blue,
  ),
),
Shrijan Regmi
  • 450
  • 5
  • 10
6

If you don't want to change the whole app theme just for 1 behavior of the field, you can use MaterialStateColor class from dart and switch colors based of the state.

Here is an example

InputDecoration(
        prefixIcon: widget.icon,
        prefixIconColor: MaterialStateColor.resolveWith((states) =>
            states.contains(MaterialState.focused)
                ? Colors.black
                : Colors.grey)
  ),
Quadrition
  • 83
  • 1
  • 9
3

Convert StatefulWidget in state class create FocusNodes and use TextFormField

 List<FocusNode> _focusNodes = [
  FocusNode(),
  FocusNode(),
];

  @override
  void initState() {
    _focusNodes.forEach((node){
    node.addListener(() {
      setState(() {});
    });
  });
    super.initState();
  }


TextFormField(
            focusNode: _focusNodes[0],
            decoration: InputDecoration(
                prefixIcon: Icon(
                  Icons.alternate_email,
                  color: _focusNodes[0].hasFocus ? Colors.green : Colors.grey,
                ),
                hintText: "Email"),
          ),
Çağlar YILMAZ
  • 111
  • 2
  • 4
3

I was able to achieve that with

ThemeData(
   colorScheme: ThemeData().colorScheme.copyWith(
              primary:Colors.red,
  ),
), 

inside MaterialApp or you add Theme on your TextFormField

Theme(
    data:Theme.of(context).copyWith(
        colorScheme: ThemeData().colorScheme.copyWith(
              primary:Colors.red,
        ),
       ),
    child:TextFormField()
)
Aymen
  • 163
  • 2
  • 10
0

I was able to accomplish this by modifying the accentColor property at the MaterialApp Theme level.

Lee Mordell
  • 473
  • 5
  • 16
0

try to add a ColorScheme with fromSeed Color inside your MaterialApp.

Color primaryAccentColor = const Color(0xFF4D70FF);

MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData.light().copyWith(
            colorScheme: ColorScheme.fromSeed(seedColor: primaryAccentColor),
          ),
    ...
);
leonms
  • 83
  • 1
  • 8
0

Just specify prefixIconColor property in inputDecoration. If it's not specified then it will take MaterialStatProperty by Default.

Here is Example

TextFormField(decoration: InputDecoration(
              prefixIcon: const Icon(Icons.search,),
              prefixIconColor: Colors.grey,
                ),
          ), 
AJ-
  • 1,638
  • 1
  • 24
  • 49
Hiren
  • 1