Creating Buttons in Flutter: ElevatedButton TextButton and IconButton

Buttons are a crucial component of any mobile or web application, serving as the primary way for users to interact with your app. In Flutter, buttons are flexible and customizable, allowing developers to create various styles and functionality depending on the needs of their project.

Creating Buttons in Flutter: ElevatedButton TextButton and IconButton

In this blog, we will explore the three most commonly used button widgets in Flutter: ElevatedButton, TextButton, and IconButton. We’ll also discuss their key properties, usage, and how to customize them to create interactive and visually appealing interfaces.

Overview of Flutter Buttons

Flutter provides a variety of button widgets, each serving different purposes. Let’s take a brief look at the three main types:

  1. ElevatedButton: Used for emphasis, as it has a raised, 3D effect.
  2. TextButton: A flat button without elevation, ideal for less prominent actions.
  3. IconButton: A button with an icon, typically used for compact actions.

All three buttons share some common behavior, such as being responsive to taps and clicks. They can be styled, customized, and configured to execute specific actions when interacted with.

1. ElevatedButton: Raised and Prominent

The ElevatedButton widget is one of the most widely used button types in Flutter. It is a raised button that adds a sense of depth with its 3D effect. This makes it ideal for actions that need more prominence or should stand out on the screen.

Basic Usage of ElevatedButton:

ElevatedButton(
onPressed: () {
print('ElevatedButton pressed!');
},
child: Text('Press Me'),
)

In this example, the button triggers the action inside the onPressed function when tapped.

Key Properties of ElevatedButton:

Styling ElevatedButton:

The style property lets you fully customize the button’s look and feel. You can adjust its background color, text style, shape, padding, and more.

Example:

ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.green, // Background color
onPrimary: Colors.white, // Text color
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
),
child: Text('Styled Button'),
)

In this example, the button has a green background, white text, custom padding, and rounded corners.

Adding an Icon to ElevatedButton:

You can also add an icon to an ElevatedButton using the Icon widget alongside text, typically using Row to arrange them horizontally.

Example:

ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.thumb_up),
label: Text('Like'),
style: ElevatedButton.styleFrom(primary: Colors.blue),
)

This button includes an icon next to the label text, ideal for actions that benefit from visual cues.

2. TextButton: Simple and Flat

The TextButton widget is a flat button without any elevation or shadow. It’s typically used for less prominent actions, such as “Cancel” buttons, or for actions in dialog boxes. The focus here is on the label (text), and not on creating emphasis through design.

Basic Usage of TextButton:

TextButton(
onPressed: () {
print('TextButton pressed!');
},
child: Text('Click Me'),
)

The button’s appearance is simple, making it perfect for situations where you want a clean, minimalist design.

Key Properties of TextButton:

Styling TextButton:

You can easily customize the appearance of TextButton using the style property. This includes setting background colors, text colors, and padding.

Example:

TextButton(
onPressed: () {},
style: TextButton.styleFrom(
primary: Colors.red, // Text color
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
backgroundColor: Colors.yellow, // Background color
),
child: Text('Styled TextButton'),
)

Here, we’ve customised the button to have red text, yellow background, and specific padding.

3. IconButton: Interactive Icons

The IconButton widget is a compact button used to display an icon. It’s ideal for actions where a symbol is more intuitive than text, such as navigation controls, toggles, or adding items to a cart.

Basic Usage of IconButton:

IconButton(
icon: Icon(Icons.volume_up),
onPressed: () {
print('IconButton pressed!');
},
)

In this example, an icon button with a volume-up symbol is used. When pressed, it prints a message to the console.

Key Properties of IconButton:

Customizing IconButton:

While IconButton is inherently simple, you can still customize its size, color, and padding.

Example:

IconButton(
icon: Icon(Icons.favorite),
color: Colors.pink,
iconSize: 40.0,
onPressed: () {
print('Favorite pressed!');
},
tooltip: 'Mark as favorite',
)

This example creates a larger, pink heart icon with a tooltip that displays “Mark as favorite.”

Combining Buttons for Better UI

You can combine different button types to create more dynamic and interactive UIs. For instance, placing an ElevatedButton and a TextButton next to each other allows you to highlight a primary action while providing secondary options.

Example: Using ElevatedButton and TextButton Together:

Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Submit'),
),
SizedBox(width: 20),
TextButton(
onPressed: () {},
child: Text('Cancel'),
),
],
)

In this layout, the ElevatedButton serves as the primary action, while the TextButton offers a secondary, less prominent choice.

Handling Button States: Disabled Buttons

Sometimes, you may want to disable a button based on certain conditions. To disable a button in Flutter, simply set the onPressed property to null.

Example of a Disabled Button:

ElevatedButton(
onPressed: null,
child: Text('Disabled'),
)

Here, the button is disabled and can’t be interacted with until onPressed is given a valid callback function.

Best Practices for Buttons in Flutter

  1. Use Appropriate Button Types: Choose the button widget that best fits the context. Use ElevatedButton for actions that need prominence, TextButton for secondary actions, and IconButton for compact or icon-only actions.
  2. Accessibility: Always add tooltips to IconButton for accessibility purposes. Tooltips provide additional context to users, especially those who rely on screen readers.
  3. Responsive Design: Use padding and layout strategies to ensure buttons look good across different screen sizes. Combining buttons with layout widgets like Row, Column, and Wrap helps in creating flexible designs.
  4. Button Feedback: Always provide visual feedback when a button is pressed (e.g., by using splash or highlight colors). Flutter does this by default, but you can customize the feedback colors using the style property.

Conclusion

Flutter provides a rich set of button widgets—ElevatedButton, TextButton, and IconButton—to help you build interactive and responsive user interfaces. Whether you’re highlighting primary actions, offering secondary options, or adding intuitive icons, Flutter’s button system offers the flexibility you need.

By mastering these button widgets and customizing their appearance with properties like style, icon, and tooltip, you can create highly polished and user-friendly apps.

Explore Other Flutter Topics…

  1. Introduction to Flutter and Dart
  2. Why choose Flutter
  3. Installing Flutter On Your Windows Mac And Linux System
  4. Your first Flutter app
  5. Flutter project structure
  6. Building blocks of Flutter
  7. Stateful vs. Stateless Widgets Explained
  8. Flutter layout system
  9. Flutter text widget
  10. Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
  11. Handling User Input with Flutter Forms
  12. Container class in Flutter
  13. Flutter Navigation
  14. Flutter – Pass Data One Screen To Another Screen
  15. Managing Device Orientation in Flutter
  16. Stateful widget lifecycle in Flutter
  17. Future of Flutter
  18. Flutter Themes
  19. Flutter Animations
  20. Flutter AppBar Customization
  21. ListView in Flutter
  22. Flutter GridView
  23. Flutter Expanded Widget
  24. Flutter BottomNavigation Bar
  25. Floating Action Button
  26. Drawer Widgets in Flutter
  27. Form Validation in Flutter
  28. Flutter TextField
  29. Adding AdMob ads to a Flutter app
  30. Building Flutter Web & Desktop Applications
  31. What is Async and Await in Flutter
  32. HTTP requests in Flutter
  33. Parsing JSON in Flutter
  34. Tinder-Style Swipe Cards in Flutter
  35. Flutter Tic Tac Toe Game Tutorial
  36. Flutter Login UI Tutorial
  37. Flutter Card Widget Tutorial
  38. Flutter music player app tutorial
  39. Flutter introduction screens
  40. Shared Preferences in Flutter
  41. SQLite Database in Flutter
  42. Firebase Authentication in Flutter
  43. Firebase Firestore in Flutter
  44. Push Notifications in Flutter
  45. Handling File Uploads in Flutter
  46. Responsive Design in Flutter
  47. Provider in Flutter
  48. Riverpod in Flutter
  49. Flutter BLoC Pattern Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *