Flutter, the open-source UI development kit by Google, is gaining popularity among developers worldwide due to its ease of use and excellent performance. One of the essential components of Flutter is the FloatingActionButton (FAB) widget, which provides a simple way to create interactive buttons in your app. In this blog post, we will discuss how to use the FloatingActionButton widget in Flutter and create a button that performs a specific action when clicked.

First, let’s understand what a FloatingActionButton is. A FloatingActionButton is a circular button that hovers over the user interface and usually performs the primary action in an app, such as adding a new item or opening a dialog box. In Flutter, you can use the FloatingActionButton widget to create these buttons with ease.

To create a FloatingActionButton, you need to define a Scaffold widget. The Scaffold widget is a built-in widget in Flutter that provides the basic layout structure for the app. Once you have defined the Scaffold widget, you can add the FloatingActionButton widget inside it.

Here is an example of how to create a simple FloatingActionButton:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FloatingActionButton Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('FloatingActionButton Demo'),
        ),
        body: Center(
          child: Text('Press the button'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // Perform the action when the button is pressed.
            print('Button pressed!');
          },
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

In the example above, we created a simple app that displays a message and a FloatingActionButton. When the button is clicked, it prints a message to the console. The onPressed property of the FloatingActionButton widget specifies the action that should be performed when the button is clicked. In this case, we used a simple print statement to output a message to the console.

You can customize the appearance of the FloatingActionButton by changing the backgroundColor, foregroundColor, and elevation properties. For example, to change the background color of the button, you can set the backgroundColor property to a specific color, like this:

floatingActionButton: FloatingActionButton(
  onPressed: () {
    // Perform the action when the button is pressed.
    print('Button pressed!');
  },
  child: Icon(Icons.add),
  backgroundColor: Colors.blue,
),

In addition to the basic FloatingActionButton, Flutter provides several variants, such as the extended FAB and the mini FAB. The extended FAB displays a label below the icon, while the mini FAB is a smaller version of the regular FAB. To use these variants, you need to set the isExtended or isMini property of the FloatingActionButton widget, like this:

floatingActionButton: FloatingActionButton.extended(
  onPressed: () {
    // Perform the action when the button is pressed.
    print('Button pressed!');
  },
  icon: Icon(Icons.add),
  label: Text('Add'),
),

In this example, we used the extended variant to create a button with a label and an icon.


In conclusion, the FloatingActionButton widget is a simple and effective way to create interactive buttons in your Flutter app. With a little bit of customization, you can create buttons that fit the design of your app and provide a great user experience.

Write A Comment