简体   繁体   English

更改焦点 Flutter 上的 suffixIcon 颜色

[英]change suffixIcon color on focus Flutter

I am trying to make a login ui in flutter.我正在尝试在颤振中创建登录 ui。 I have a suffixicon decoration in TextFormField .我在TextFormField有一个suffixicon装饰。 I need to set it's color then change it later when the TextFormField is in a focused state我需要设置它的颜色,然后在TextFormField处于聚焦状态时更改它

I tried giving color to the icon, but it stays constant, I also tried with theme, but since i am new i got lost.我尝试为图标添加颜色,但它保持不变,我也尝试使用主题,但由于我是新手,我迷路了。

dartpad link 飞镖链接

 Color suffixColor = Colors.grey;

    Focus(
            onFocusChange: (hasFocus) {
              setState(() {
                (hasFocus)
                    ? suffixColor = Colors.red
                    : suffixColor = Colors.grey;
              });
            },
            child: TextFormField(
              decoration: InputDecoration(
                suffixIcon: Icon(
                  Icons.monetization_on,
                  color: suffixColor,
                ),
              ),
            ),

use focus property of TextFormField to achieve your result使用 TextFormField 的 focus 属性来实现您的结果

FocusNode focus = FocusNode();
  
  @override
  Widget build(BuildContext context) {
    
    return TextFormField(
      focusNode: focus,
      onTap: () {
          FocusScope.of(context).requestFocus(focus);
      },
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        suffixIcon: Icon(
          Icons.search,
          color: focus.hasFocus ? Colors.red : Colors.green,
        ),
      ),
    );
  }

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

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