繁体   English   中英

如何改变 TextButton.icon 的颜色

[英]How to change color in TextButton.icon

我想要一个带有文本的图标,所以我使用TextButton.icon但我无法更改文本或图标的颜色:请建议是否有人有解决方案这是我的代码:

SizedBox(height: 8,),
        TextButton.icon(
          icon: Icon(Icons.delete),
          label: Text('delete'),

          onPressed: (){},
        ),

您可以在 TextButton 的样式上使用 TextButton.stylefrom。 在这种方法中,您可以使用 primary 来设置图标和标签的颜色。 如果要为图标设置另一种颜色,可以在 Icon 中设置图标颜色。

TextButton.icon(
   onPressed:(){}),
   style: TextButton.styleFrom(
      primary: Colors.blue,
   ),
   icon: Icon(Icons.ac_unit, color: Colors.red),
   label: Text("label"),
 )

基于Emmanuel Ashitey的回答,从 v3.1.0 开始,“主要”方法已被弃用(根据 VSCode 工具提示),应替换为“foregroundColor”:

TextButton.icon(
    onPressed: () {},
    icon: const Icon(Icons.delete),
    label: const Text('Delete'),
    style: TextButton.styleFrom(
        foregroundColor: Theme.of(context).errorColor,
    ),
),

对于这种情况,显然Theme.of(context).errorColor可以替换为Colors.red

试试下面的代码希望它对你有帮助。
请参阅 此处TextButton

TextButton.icon(
  icon: Icon(
    Icons.delete,
    color: Colors.red,//for icon color
  ),
  label: Text(
    'Delete',
    style: TextStyle(
      color: Colors.red,//for text color
    ),
  ),
  onPressed: () {},
),

结果屏幕-> 图片

暂无
暂无

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

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