扑改变焦点颜色和图标颜色,但行不通

发布于 2025-02-13 05:25:11 字数 1261 浏览 0 评论 0 原文

更改焦点颜色和图标颜色,但不起作用,

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
              ],
            ),

我将焦点颜色和图标颜色更改为红色。我确实重新启动,但输出仍然是蓝色的。我的主题主要颜色也是红色。

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

问题是什么? 这是当前输出。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放手` 2025-02-20 05:25:12

如果您想更改焦点上的图标颜色,则可以使用此方法

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

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,)),
            ),
      ),
澉约 2025-02-20 05:25:12

来自 intical文档这是可用的:

此示例显示了如何使用prefixicon的文本场
通过使用themedata来根据物质状态更改颜色。

因此,您的代码可能是:

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
    ],
  ),
),

好处是,如果您愿意,可以将此InputDecoration设置为整个应用程序,仅一次使用 theme> theme proterty在 pocity> posital> pocity> portialapp /代码>小部件,并将在任何地方应用。

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.

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.

enter image description here

牛↙奶布丁 2025-02-20 05:25:12

尝试使用 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,
    ),
  ),

此外,请详细介绍焦点节点

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

有关 a>

Try using 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文