[Flutter]ElevatedButtonのボタンの背景色と文字色の設定・変更方法

このページには広告が含まれる場合があります。

 

(注)2022/9にリリースされたFlutter3.3では、primary / onPrimary属性が backgroundColor / foregroundColorに変更となっていますのでご注意下さい。

Flutter2.0以降は、ボタンのWidgetが「RaisedButton」から「ElevatedButton」に変更されましたが、

「ElevatedButton」の背景色及びボタンに表示させる文字色を設定・変更する場合は、

  • ElevatedButtonのstyle属性で、「ElevatedButton.styleFrom」メソッドを使って
  • ボタンの背景色は「primary」属性で
  • 文字色は「onPrimary」属性で

設定します。

RaisedButton(
  color: Colors.red, // background
  textColor: Colors.white, // foreground
  onPressed: () { },
  child: Text('RaisedButton with custom foreground/background'),
)

ElevatedButton(
  style: ElevatedButton.styleFrom(
    primary: Colors.red, // background
    onPrimary: Colors.white, // foreground
  ),
  onPressed: () { },
  child: Text('ElevatedButton with custom foreground/background'),
)

(以下の動画でも解説していますので、よろしければご覧下さい)

こんな記事も読まれています

人気No.1

前向きな気持ちになれる 占星術・タロットリーディング

あすコロ式・相談サロン 【ナビるやん】

人に言えない悩みから使命・運勢診断まで、あなたに寄り添いながら“ナビゲート”する、あすコロ人気No.1のオンライン相談サロン。占星術・心理学・タロットの叡智を活かし、普段認識していない「潜在意識」にアクセスしてあなたの気づいていない可能性を拓かせ、明日から前向きに歩めるようになる60分のリーディングセッション。

今すぐ詳細を見る(メルマガ登録で初回半額) ▶

TOP