Buttons are an essential element for creating interactive and visually appealing applications. Among the array of button widgets available, the ElevatedButton stands out as one of the most commonly used widgets. Whether you're a beginner or an experienced developer, this guide will take you through everything you need to know about the ElevatedButton in Flutter.
What is the ElevatedButton Widget?
The ElevatedButton widget is a material design button that elevates when pressed. It replaces the older RaisedButton widget and is highly customizable, making it a go-to choice for developers building apps with Flutter.
Key Features of ElevatedButton
- Elevation Effect: Adds depth to your app design with elevation upon user interaction.
- Customizable Appearance: Offers options to modify colors, shapes, and styles.
- Built-in Callback: Supports
onPressed
andonLongPress
for handling user actions. - Adaptive: Works seamlessly across different platforms, including Android, iOS, and web.
How to Use ElevatedButton
Using the ElevatedButton is straightforward. Here's a simple example:
ElevatedButton(
onPressed: () {
print('Button Pressed!');
},
style: ElevatedButton.styleFrom(
primary: Colors.blue, // Button color
onPrimary: Colors.white, // Text color
elevation: 5, // Shadow effect
),
child: Text('Press Me'),
),
)
So what's in this button?
- onPressed: This callback gets triggered when the button is pressed.
- style: Customizes the button's appearance using
ElevatedButton.styleFrom
. - child: Defines the content inside the button, which is typically a
Text
widget.
Advanced Styling with ElevatedButton
The ElevatedButton
widget can be further customized for advanced use cases:
ElevatedButton(
onPressed: () {
print('Customized Button Pressed!');
},
style: ElevatedButton.styleFrom(
primary: Colors.teal,
onPrimary: Colors.white,
shadowColor: Colors.tealAccent,
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.thumb_up),
SizedBox(width: 8),
Text('Like'),
],
),
);
Customization Options:
- Color Properties:
primary
for background color,onPrimary
for text color. - Shape: Use
RoundedRectangleBorder
or other shapes. - Padding: Adjust inner spacing with
EdgeInsets
. - Icons: Add icons alongside text for better UX.
- MaterialStateProperty: apperance based on its material states. Let's learn more.
MaterialStateProperty:
MaterialStateProperty
to dynamically change its properties, such as color, elevation, padding, and more, depending on its state. It is the base Button Style property. We will impliment it later in theme data to use globally.Definig in Theme Data:
You can define the global button styles in the ThemeData
using the elevatedButtonTheme
, textButtonTheme
, or outlinedButtonTheme
. Each of these themes accepts a ButtonStyle
to customize the respective button types.
Here’s how you can define the ButtonStyle
for an ElevatedButton in ThemeData
:
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all(Colors.blue), // Blue background
foregroundColor: WidgetStateProperty.all(Colors.white), // White text
padding: WidgetStateProperty.all(EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0)), // Padding for a more professional look
shape: WidgetStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0), // Rounded corners for a professional look
)),
elevation: WidgetStateProperty.all(0), // Subtle elevation for a sleek design
textStyle: WidgetStateProperty.all(TextStyle(
fontSize: 16.0, // Slightly larger font for readability
fontWeight: FontWeight.w600, // Bold for emphasis
letterSpacing: 0.5, // Slight spacing for a cleaner look
)),
),
)
Advantages of Using MaterialStateProperty
- Dynamic Styling: Easily define different styles for different states.
- Reusable Logic: Encapsulate styling logic in a
MaterialStateProperty
for consistency. - Improved UX: Provide visual feedback to users based on their interactions.
Best Practices for Using ElevatedButton
- Accessibility: Ensure proper contrast between button text and background.
- Consistency: Maintain consistent styles across buttons in your app.
- Responsive Design: Adjust button size and padding for various screen sizes.
When to Use ElevatedButton
- Highlight primary actions on the screen.
- Provide visual emphasis to encourage user interaction.
- Create a floating effect for important buttons.
Conclusion
The ElevatedButton widget is a powerful tool in the Flutter toolkit for creating interactive and aesthetically pleasing buttons. By mastering its features and customization options, you can enhance the usability and visual appeal of your Flutter applications.