簡體   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