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

非活性ボタンの色を変更するには「style」で「ElevatedButton.styleFrom」を使用します。
非活性時の背景色は「disabledBackgroundColor」、前景色は「disabledForegroundColor」で指定できます。
ElevatedButton(
child: Text('Click Me'),
style: ElevatedButton.styleFrom(
disabledBackgroundColor: Colors.grey.withOpacity(0.5),
disabledForegroundColor: Colors.white.withOpacity(0.8),
),
onPressed: null,
),
サンプルコード
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(
disabledBackgroundColor: Colors.grey.withOpacity(0.5),
disabledForegroundColor: Colors.white.withOpacity(0.8),
),
onPressed: null,
),
),
),
);
}
}
以上です。
合わせて読みたい
あわせて読みたい


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