Buttons in Flutter: Functional and Effective Use

 

Buttons are one of the most used widgets in Flutter. Because they are a fundamental tool for user interaction, triggering actions within the app and facilitating navigation. Flutter’s flexibility and customization capabilities make it possible to create a wide variety of visually appealing buttons.

Before moving on to the design part, there are basically three types of buttons: ElevatedButton, TextButton and OutlinedButton. Understanding the basic differences between these button types can help us choose our buttons correctly.

ElevatedButton

By default it has a raised effect, meaning that the background color is raised significantly. And a corresponding shading effect appears behind it.

ElevatedButton(
onPressed: () {},
child: const Text('Elevated Button'),
),

TextButton

It represents a text-based button and usually has a simpler appearance. The background color is either absent or has a light color.

TextButton(
onPressed: () {},
child: const Text('Text Button'),
),

OutlinedButton

It looks like a hollow button with defined edges. The outer edges are outlined with a striped frame, hence the name.

OutlinedButton(
onPressed: () {},
child: const Text('Outlined Button'),
),

IconButton

We use it for buttons that contain only one icon by this way we get a minimal image.

IconButton(
onPressed: () {},
icon: const Icon(Icons.home),
iconSize: 32.0,
),

Characteristics

We can manage the functional use of buttons with the onPressed method. In the examples above, if I give null to the onPressed property of the buttons I gave as an example for anonymous use, the buttons will be displayed but not clickable. We can also do this by giving true or false to the enabled property.

We can add animations to the buttons to improve the user experience. It can be simple transition effects or touch reactions, it can be color options that will be shown when hovered over, we can get creative and engaging user interfaces by making customizations like this.

onLongPressed property is another one where we specify the actions to be taken when the button is pressed for a long time.

Style Features

In Flutter, we can customize buttons as we wish through the “style” property.

We can specify individual style properties for each button type through special functions such as ElevatedButton.styleFromTextButton.styleFromandOutlinedButton.styleFrom.

For example

  • shadowColor: shading color,
  • elevation: height of the button above the floor,
  • textStyle: style properties of the text to be displayed on the button,
  • padding: the space between the content of the button and its edges,
  • shape: button shape, borders, corner rounding style,
  • duration: there are many customizable features such as animation duration.

Apart from these, we can adjust the button width with the size property.

style: ElevatedButton.styleFrom(
minimumSize: Size(200, 50), // adjust width and height
),

We can bind the color of the button to a condition and dynamically change it to a different color when that condition is met.

style: ElevatedButton.styleFrom(
backgroundColor: someCondition ? Colors.blue : Colors.red,
),

To make the button as large as its content, we can place the Expanded widget inside it.

Expanded(
child: ElevatedButton(
onPressed: () {},
child: const Text('You can click the button as many times as you want.'),
),
),

We can use row and column widgets to customize the content of the button and add multiple elements.

ElevatedButton(
onPressed: () {},
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.star),
SizedBox(width: 8),
Text('Like'),
],
),
),

In conclusion, buttons in Flutter are an easy and, in my opinion, somewhat fun way to make the user interface interactive and attractive. In addition, choosing the right type of button and customizing the design allows us to improve the user experience. I hope you can find some useful and inspiring uses in this article.

I wish us all ease.

Selin

Hiç yorum yok: