简体   繁体   English

如何更改 Flutter 中的默认前缀图标主题?

[英]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.我使用 colorScheme 更改 TextField 中前缀图标的颜色,但如何更改默认前缀图标颜色。 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 Github上有一个关于这个的问题

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 :我能够改变包装TextField

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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