世界最大級のオンライン学習サービス「Udemy」のセール状況はこちら

【Flutter】ElevatedButtonの非活性(disabled)色を変更

マテリアルボタン(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,
          ),
        ),
      ),
    );
  }
}

以上です。

合わせて読みたい

参考サイト

目次