Slava
Slava

Reputation: 27

How to change default prefix icon theme in Flutter?

I have had an issue. I change color of prefix icon in TextField with colorScheme, but how to change default prefix icon color. I have tried to change it this way

data: Theme.of(context).copyWith(
        iconTheme: Theme.of(context).iconTheme.copyWith(
            color: Colors.red,
        ),
        colorScheme: Theme.of(context).colorScheme.copyWith(
            primary: Colors.blue,
        ),
),

But this way doesn't work

Upvotes: 0

Views: 678

Answers (2)

Lucas Josino
Lucas Josino

Reputation: 835

If you want a fixed icon color you can use this:

TextField(
  decoration: InputDecoration(
    prefixIcon: Icon(Icons.done, color: Colors.green),
  ),
),

There's a issue on Github about this

Update:

See this code

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  FocusNode _focus = FocusNode();

  //Define your default color  
  Color fieldColor = Colors.green;

  //Using focus node everytime the focus change the listener will change the 
  //color and call setState
  @override
  void initState() {
    super.initState();
    _focus.addListener(() {
      setState(() {
        fieldColor = _focus.hasFocus ? Colors.red : Colors.black;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: TextField(
          focusNode: _focus,
          decoration: InputDecoration(
            prefixIcon: Icon(Icons.done, color: fieldColor),
          ),
        ),
      ),
    );
  }
}

Upvotes: 2

NelsonThiago
NelsonThiago

Reputation: 830

I was able to change wrapping the TextField:

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

Upvotes: 0

Related Questions