Open In App

Flutter – Draggable Floating Action Button

Last Updated : 19 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Flutter, Floating Action Buttons are considered modern UI Widgets, in most applications the position of the FAB is fixed but In this article, we are going to explore how can we make a Draggable Floating Action Button and change its position with the help of a Draggable widget in Flutter. A sample video is given below to get an idea about what we are going to do in this article.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




void main() => runApp(MyApp());


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData( 
        primarySwatch: Colors.green, // Set the app's primary theme color 
      ), 
      debugShowCheckedModeBanner: false
      home: DraggableFAB(),
    );
  }
}


Step 5: Create DraggableFAB Class

In this class we are going to create a draggable Floating Action Button (FAB) in Flutter, we can use the Stack widget to overlay the FAB on your content and the Positioned widget to define its position. Additionally, you can use the GestureDetector to handle drag events. Here We use the Draggable widget for the FAB and the onDragEnd callback to update the FAB’s position when it’s dragged.

Stack(
children: <Widget>[
// Your main content here
Center(
child: Text('GFG Draggable FAB'),
),
// Draggable FAB
Positioned(
left: fabPosition.dx,
top: fabPosition.dy,
child: Draggable(
feedback: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
child: isFABVisible
? FloatingActionButton(
onPressed: toggleFABVisibility,
child: Icon(Icons.add),
)
: Container(), // Hide FAB when isFABVisible is false
onDragEnd: (details) {
setState(() {
fabPosition = details.offset; // Update FAB position when dragged
});
},
),
),
],
),

Dart




class DraggableFAB extends StatefulWidget {
  @override
  _DraggableFABState createState() => _DraggableFABState();
}
  
class _DraggableFABState extends State<DraggableFAB> {
  bool isFABVisible = true; // Tracks FAB visibility
  Offset fabPosition = Offset(20, 20); // Initial position of the FAB
  
  void toggleFABVisibility() {
    setState(() {
      isFABVisible = !isFABVisible; // Toggle FAB visibility
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Draggable FAB Demo'),
      ),
      body: Stack(
        children: <Widget>[
          // Your main content here
          Center(
            child: Text('GFG Draggable FAB'),
          ),
  
          // Draggable FAB
          Positioned(
            left: fabPosition.dx,
            top: fabPosition.dy,
            child: Draggable(
              feedback: FloatingActionButton(
                onPressed: () {},
                child: Icon(Icons.add),
              ),
              child: isFABVisible
                  ? FloatingActionButton(
                      onPressed: toggleFABVisibility,
                      child: Icon(Icons.add),
                    )
                  : Container(), // Hide FAB when isFABVisible is false
              onDragEnd: (details) {
                setState(() {
                  fabPosition = details.offset; // Update FAB position when dragged
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/material.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData( 
        primarySwatch: Colors.green, // Set the app's primary theme color 
      ), 
      debugShowCheckedModeBanner: false
      home: DraggableFAB(),
    );
  }
}
  
class DraggableFAB extends StatefulWidget {
  @override
  _DraggableFABState createState() => _DraggableFABState();
}
  
class _DraggableFABState extends State<DraggableFAB> {
  bool isFABVisible = true; // Tracks FAB visibility
  Offset fabPosition = Offset(20, 20); // Initial position of the FAB
  
  void toggleFABVisibility() {
    setState(() {
      isFABVisible = !isFABVisible; // Toggle FAB visibility
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Draggable FAB Demo'),
      ),
      body: Stack(
        children: <Widget>[
          // Your main content here
          Center(
            child: Text('GFG Draggable FAB'),
          ),
  
          // Draggable FAB
          Positioned(
            left: fabPosition.dx,
            top: fabPosition.dy,
            child: Draggable(
              feedback: FloatingActionButton(
                onPressed: () {},
                child: Icon(Icons.add),
              ),
              child: isFABVisible
                  ? FloatingActionButton(
                      onPressed: toggleFABVisibility,
                      child: Icon(Icons.add),
                    )
                  : Container(), // Hide FAB when isFABVisible is false
              onDragEnd: (details) {
                setState(() {
                  fabPosition = details.offset; // Update FAB position when dragged
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads