简体   繁体   中英

How to change default preffix 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

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),
          ),
        ),
      ),
    );
  }
}

I was able to change wrapping the TextField :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM