Flutter - ElevatedButton Color
The ElevatedButton class represents a material design elevated button. The
flutter developers can use elevated buttons to add dimension to otherwise
mostly flat layouts such as in long busy lists of content, or in wide spaces.
The flutter developers should avoid using elevated buttons on already-elevated
content such as dialogs or cards. The flutter developers can override the
ElevatedButton style with its style parameter. The static styleFrom() method
is a convenient way to create an ElevatedButton ButtonStyle from simple
values.
The following flutter application development tutorial will demonstrate how to set or change an ElevatedButton widget’s various colors. Here we will use the ElevatedButton class’s styleFrom() method to change the ElevatedButton widget’s colors such as foreground color, background color, and shadow color.
The ElevatedButton class’s styleFrom() method is a static convenience method that constructs an elevated button ButtonStyle given simple values. By default, ElevatedButton class styleFrom() method returns a ButtonStyle that doesn't override anything.
The ButtonStyle class represents the visual properties that most buttons have in common. The ButtonStyle class’s all properties are null by default.
The ElevatedButton styleFrom() method’s foregroundColor, backgroundColor, disabledForegroundColor, disabledBackgroundColor, shadowColor, and surfaceTintColor parameters are used to set or change an Elevatedbutton’s corresponding color.
The primary parameter is deprecated and should use the backgroundColor parameter instead of it. And the onPrimary parameter is also deprecated and should use the foregroundColor parameter instead of it.
The following flutter application development tutorial will demonstrate how to set or change an ElevatedButton widget’s various colors. Here we will use the ElevatedButton class’s styleFrom() method to change the ElevatedButton widget’s colors such as foreground color, background color, and shadow color.
The ElevatedButton class’s styleFrom() method is a static convenience method that constructs an elevated button ButtonStyle given simple values. By default, ElevatedButton class styleFrom() method returns a ButtonStyle that doesn't override anything.
The ButtonStyle class represents the visual properties that most buttons have in common. The ButtonStyle class’s all properties are null by default.
The ElevatedButton styleFrom() method’s foregroundColor, backgroundColor, disabledForegroundColor, disabledBackgroundColor, shadowColor, and surfaceTintColor parameters are used to set or change an Elevatedbutton’s corresponding color.
The primary parameter is deprecated and should use the backgroundColor parameter instead of it. And the onPrimary parameter is also deprecated and should use the foregroundColor parameter instead of it.
main.dart
import 'package:flutter/material.dart';
void main(){
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Flutter - ElevatedButton Color")
),
body: Center(
child: ElevatedButton(
onPressed: (){},
child: const Text("Click me"),
style: ElevatedButton.styleFrom(
primary: Colors.pink,
onPrimary: Colors.black,
shadowColor: Colors.green,
onSurface: Colors.yellow,
padding: const EdgeInsets.symmetric(
horizontal: 48,
vertical: 24
)
),
)
),
),
)
);
}