マテリアルボタン(ElevatedButton, OutlinedButton, TextButton)の背景色・前景色を変更する方法を紹介します。
今回は「ElevatedButton」で解説していきますが、他のマテリアルボタンも同様です。
目次
マテリアルボタンの背景色・前景色を変更する方法

ボタンの色を変更するには「style」で「ElevatedButton.styleFrom」を使用します。
背景色は「backgroundColor」、前景色は「foregroundColor」で指定できます。
ElevatedButton(
child: Text('Click Me'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
foregroundColor: Colors.yellow,
),
onPressed: () {},
),
サンプルコード
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Flutter')),
body: Center(
child: ElevatedButton(
child: Text('Click Me'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple,
foregroundColor: Colors.yellow,
),
onPressed: () {},
),
),
),
);
}
}
以上です。
合わせて読みたい
あわせて読みたい


【Flutter】IconButtonの使い方|アイコン型ボタンを実装
Flutterのウィジェット「IconButton」の使い方を紹介します。 「IconButton」ではアイコン型ボタンを実装できます。 IconButtonの使い方 「IconButton」の「icon」で表…
あわせて読みたい


【Flutter】ElevatedButtonの非活性(disabled)色を変更
マテリアルボタン(ElevatedButton, OutlinedButton, TextButton)が非活性されているときの色を変更する方法を紹介します。 今回は「ElevatedButton」で解説していきま…