top of page
Search

Creating draggable buttons in Flutter

  • Writer: Aarthi Krishnan
    Aarthi Krishnan
  • Apr 30, 2024
  • 2 min read

Updated: May 15, 2024


Creating draggable buttons in Flutter
Creating draggable buttons in Flutter

Hey let’s learn Creating draggable buttons in Flutter.  Creating draggable buttons in Flutter adds an interactive and dynamic element to your user interface, offering a seamless user experience. It makes the user engage for more time. It also enhances the visual quality of your app.  Basically the purpose of Creating draggable buttons in Flutter it allow users to easily drag a button for full screen and place it anywhere they want. 


We need help of the package https://pub.dev/packages/draggable_fab



dependencies:
  flutter:
    sdk: flutter
  draggable_fab: ^0.1.1


To implement draggable buttons in your Flutter application, the first step is to define the button widget. For this purpose, you can use the FloatingActionButton widget, also known as FAB. This widget is an ideal starting point due to its predefined characteristics and its widespread use in Flutter apps.


Floating Action Bar widget :


DraggableFab(
        child: FloatingActionButton(
          onPressed: _incrementCounter,
          child: Icon(Icons.add),
        ),
      );


After adding the FloatingActionButton to your app, you can enable the draggable feature by using Flutter's GestureDetector widget. This widget helps in detecting different types of gestures such as tapping, swiping, and dragging, which forms the basis for implementing drag-and-drop functionality. 


Full Example Code:


import 'package:draggable_fab/draggable_fab.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Draggable FAB Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Draggable FAB Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.bodyText1,
            ),
          ],
        ),
      ),
      floatingActionButton: DraggableFab(
        child: FloatingActionButton(
          onPressed: _incrementCounter,
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}



To enable drag and drop functionality within the GestureDetector widget, you must define a callback function that will handle the drag events. The callback function will be responsible for updating the position of the button as per the user's touch movements. By keeping track of the difference between the initial touch point and subsequent touch points, you can accurately calculate the new position of the button as it is dragged across the screen.



Comments


bottom of page