简体   繁体   English

颤振改变焦点颜色和图标颜色但不起作用

[英]Flutter change focus color and icon color but not works

Change focus color and icon color but not work更改焦点颜色和图标颜色但不起作用

TextFormField(
              cursorColor: Colors.red[600],
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                filled: false,
                iconColor: Colors.red,
                focusColor: Colors.red,
                icon: Icon(Icons.phone),
                hintText: 'Where can we reach you?',
                labelText: 'Phone Number *',
                prefixText: '+86',
              ),
              keyboardType: TextInputType.phone,
              onSaved: (String? value) {
                this._phoneNumber = value;
                print('phoneNumber=$_phoneNumber');
              },
              // TextInputFormatters are applied in sequence.
              inputFormatters: <TextInputFormatter>[
                FilteringTextInputFormatter.digitsOnly
              ],
            ),

I have changed the focus color and icon color to red.我已将焦点颜色和图标颜色更改为红色。 I do hot restart but the output still is blue.我做了热重启,但输出仍然是蓝色的。 My theme primary color is also red.我的主题原色也是红色。

theme: ThemeData(primaryColor: Colors.red, fontFamily: 'Poppins'),

What is the problem?问题是什么? Here are the current output.这是当前的输出。

Try using FocusNode .尝试使用FocusNode

  late FocusNode focusNode = FocusNode()
    ..addListener(() {
      setState(() {});
    });
TextFormField(
  focusNode: focusNode,
  cursorColor: Colors.red[600],
  decoration: InputDecoration(
    icon: Icon(
      Icons.phone,
      color: focusNode.hasFocus ? Colors.red : Colors.grey,
    ),
  ),

Also, dispose the focus node另外,设置焦点节点

  @override
  void dispose() {
    focusNode.dispose();
    super.dispose();
  }

More about FocusNode更多关于FocusNode

From the offical documentation this is available:官方文档中可以找到:

This sample shows how to style a TextField with a prefixIcon that changes color based on the MaterialState through the use of ThemeData.此示例演示如何使用基于 MaterialState 通过使用 ThemeData 更改颜色的 prefixIcon 设置 TextField 的样式。

Your code could therefore be:因此,您的代码可能是:

Theme(
  data: Theme.of(context).copyWith(
    inputDecorationTheme: Theme.of(context).inputDecorationTheme.copyWith(
      iconColor: MaterialStateColor.resolveWith((Set<MaterialState> states) {
        if (states.contains(MaterialState.focused)) {
          return Colors.red;
        }
        if (states.contains(MaterialState.error)) {
          return Colors.deepOrange;
        }
        return Colors.grey;
      }),
    ),
  ),
  child: TextFormField(
    cursorColor: Colors.red[600],
    decoration: const InputDecoration(
      border: UnderlineInputBorder(),
      filled: false,
      icon: Icon(Icons.phone),
      hintText: 'Where can we reach you?',
      labelText: 'Phone Number *',
      prefixText: '+86',
    ),
    keyboardType: TextInputType.phone,
    onSaved: (String? value) {
      this._phoneNumber = value;
      print('phoneNumber=$_phoneNumber');
    },
    // TextInputFormatters are applied in sequence.
    inputFormatters: <TextInputFormatter>[
      FilteringTextInputFormatter.digitsOnly
    ],
  ),
),

The good thing with this is that you can, if you want, set this inputDecorationTheme to be applied for the entire app just once using the theme property on the MaterialApp widget, and will be applied everywhere.这样做的好处是,如果需要,您可以使用MaterialApp小部件上的theme属性将此 inputDecorationTheme 设置为仅应用于整个应用程序一次,并将应用于任何地方。

在此处输入图像描述

If you wish to change the color of Icon on focus then you can use this instead如果你想改变焦点图标的颜色,那么你可以使用它来代替

Color iconColor = Colors.grey;//add this as a variable

//use the focus widget in place of your textfield.
 Focus(
     onFocusChange: (hasFocus)
        {
         if(hasFocus)
             {
               iconColor = Colors.red;
              }
         else{
                iconColor = Colors.grey;
             }
             setState((){});
           },
          child: TextField(
             decoration: InputDecoration(prefix: Icon(Icons.phone, color: iconColor,)),
            ),
      ),

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

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