<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Khoshsafa</title>
    <description>The latest articles on DEV Community by Ali Khoshsafa (@alikhoshsafa).</description>
    <link>https://dev.to/alikhoshsafa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F746363%2F15b7929b-5797-481e-a8e5-f3794833f709.jpeg</url>
      <title>DEV Community: Ali Khoshsafa</title>
      <link>https://dev.to/alikhoshsafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alikhoshsafa"/>
    <language>en</language>
    <item>
      <title>Building a "Back to Top" Button in Flutter</title>
      <dc:creator>Ali Khoshsafa</dc:creator>
      <pubDate>Tue, 29 Jul 2025 10:46:48 +0000</pubDate>
      <link>https://dev.to/alikhoshsafa/building-a-back-to-top-button-in-flutter-40mi</link>
      <guid>https://dev.to/alikhoshsafa/building-a-back-to-top-button-in-flutter-40mi</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4zu8ehunybtpf3acey3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4zu8ehunybtpf3acey3.png" alt=" " width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you or the PO. ever scrolled through a long list on a mobile app and wished for a quick way back to the top? In this article, I’ll walk you through creating a sleek “Back to Top” button in Flutter that slides out from the left side of the screen as you scroll down, then gracefully slides back in when you’re near the top. This project is a practical demo of enhancing user experience with animations and scroll handling.&lt;/p&gt;

&lt;p&gt;The Idea&lt;br&gt;
Inspired by common UI patterns, this Flutter app features a yellow FloatingActionButton with an upward arrow that activates after scrolling 200 pixels. Using AnimatedSlide and a ScrollController, the button animates smoothly, offering a tap-to-top functionality with a 400ms ease-out effect.&lt;/p&gt;

&lt;p&gt;How It Works&lt;br&gt;
Scroll Detection: The button appears when the scroll offset exceeds 200 pixels and hides when back within that threshold.&lt;br&gt;
Animation: The slide-out effect uses Offset(1.0, 0) to move off-screen to the right, reverting to Offset(0, 0) when visible.&lt;br&gt;
Customization: Adjust colors, thresholds, or animation curves to fit your design.&lt;br&gt;
Getting Started&lt;br&gt;
Clone the repo, run flutter pub get, and launch with flutter run. Check out the &lt;a href="https://github.com/alikhoshsafa/Flutter_BackToTopDemo/tree/main" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; for the full code!&lt;/p&gt;

&lt;p&gt;Why It Matters&lt;br&gt;
This solution improves navigation in long-scrolling interfaces, a must for apps with extensive content. Plus, it’s a fun way to explore Flutter’s animation capabilities.&lt;/p&gt;

&lt;p&gt;Ready to implement this in your project? Dive into the code and let me know your tweaks in the comments! Follow me for more Flutter tips and tricks.&lt;/p&gt;

&lt;p&gt;More challenges to work on&lt;br&gt;
Try not to use FAB in case you need it for a create or refresh button. (Althought you can make a multi purpose FAB but a sliding back to top feature may sound more attractive)&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
    </item>
    <item>
      <title>Creating a Draggable FloatingActionButton in Flutter: A Complete Guide</title>
      <dc:creator>Ali Khoshsafa</dc:creator>
      <pubDate>Sun, 01 Jun 2025 07:15:50 +0000</pubDate>
      <link>https://dev.to/alikhoshsafa/creating-a-draggable-floatingactionbutton-in-flutter-a-complete-guide-2gon</link>
      <guid>https://dev.to/alikhoshsafa/creating-a-draggable-floatingactionbutton-in-flutter-a-complete-guide-2gon</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The FloatingActionButton (FAB) is a staple in Material Design apps, typically used for primary actions. But what if you want to make it draggable, allowing users to reposition it anywhere on the screen, preventing from covering other parts of the screen or even texts in other widgets.&lt;br&gt;
In this article, we’ll implement a fully draggable FAB in Flutter as a solution using GestureDetector and Positioned inside a Stack. This approach ensures smooth dragging, proper boundary checks, and a polished user experience.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Use a Draggable FAB?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Better UX: Lets users place the FAB where it’s most convenient.&lt;/li&gt;
&lt;li&gt;Avoids Obstructions: Useful when the FAB might block important content.&lt;/li&gt;
&lt;li&gt;Engagement: Adds a playful, interactive element to your app.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Implementation: Step-by-Step
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;1. Setting Up the Widget Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We’ll use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Stack&lt;/code&gt;&lt;/strong&gt; → To overlay the FAB on top of other content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Positioned&lt;/code&gt;&lt;/strong&gt; → To control the FAB’s coordinates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;GestureDetector&lt;/code&gt;&lt;/strong&gt; → To handle drag gestures.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  2. Initializing the FAB Position
&lt;/h3&gt;

&lt;p&gt;We calculate the initial position after the first frame render to ensure correct placement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) =&amp;gt; _setInitialPosition());
}

void _setInitialPosition() {
  final screenSize = MediaQuery.of(context).size;
  final paddingBottom = MediaQuery.of(context).padding.bottom;

  setState(() {
    _fabPosition = Offset(
      screenSize.width - _fabSize - 16, // Right margin
      screenSize.height - paddingBottom - _fabSize - 16, // Bottom margin
    );
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Making the FAB Draggable
&lt;/h3&gt;

&lt;p&gt;We use &lt;code&gt;GestureDetector&lt;/code&gt;’s &lt;code&gt;onPanUpdate&lt;/code&gt; to update the FAB’s position while dragging.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GestureDetector(
  onPanUpdate: (details) {
    final screenSize = MediaQuery.of(context).size;
    final paddingTop = MediaQuery.of(context).padding.top;
    final paddingBottom = MediaQuery.of(context).padding.bottom;

    setState(() {
      _fabPosition = Offset(
        (_fabPosition.dx + details.delta.dx)
            .clamp(0, screenSize.width - _fabSize),
        (_fabPosition.dy + details.delta.dy)
            .clamp(paddingTop, screenSize.height - paddingBottom - _fabSize),
      );
      _isDragging = true;
    });
  },
  onPanEnd: (_) =&amp;gt; setState(() =&amp;gt; _isDragging = false),
  child: FloatingActionButton(
    onPressed: _isDragging ? null : () =&amp;gt; print("FAB Pressed!"),
    child: Icon(_isDragging ? Icons.drag_handle : Icons.add),
  ),
),
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Features:
&lt;/h2&gt;

&lt;p&gt;✔ ## Boundary-aware → Stays within screen limits.&lt;/p&gt;

&lt;p&gt;✔ ## Visual feedback → Changes icon while dragging.&lt;/p&gt;

&lt;p&gt;✔ ## Press handling → Disables click during drag.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Full Code Implementation
&lt;/h3&gt;

&lt;p&gt;Here’s the complete widget:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

class DraggableFABScreen extends StatefulWidget {
  @override
  _DraggableFABScreenState createState() =&amp;gt; _DraggableFABScreenState();
}

class _DraggableFABScreenState extends State&amp;lt;DraggableFABScreen&amp;gt; {
  Offset _fabPosition = Offset(0, 0);
  bool _isDragging = false;
  final double _fabSize = 56.0;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) =&amp;gt; _setInitialPosition());
  }

  void _setInitialPosition() {
    final screenSize = MediaQuery.of(context).size;
    final paddingBottom = MediaQuery.of(context).padding.bottom;

    setState(() {
      _fabPosition = Offset(
        screenSize.width - _fabSize - 16,
        screenSize.height - paddingBottom - _fabSize - 16,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Scaffold(
          appBar: AppBar(title: Text("Draggable FAB Demo")),
          body: Center(child: Text("Main Content")),
        ),
        if (_fabPosition != Offset.zero)
          Positioned(
            left: _fabPosition.dx,
            top: _fabPosition.dy,
            child: GestureDetector(
              onPanUpdate: (details) {
                final screenSize = MediaQuery.of(context).size;
                final paddingTop = MediaQuery.of(context).padding.top;
                final paddingBottom = MediaQuery.of(context).padding.bottom;

                setState(() {
                  _fabPosition = Offset(
                    (_fabPosition.dx + details.delta.dx)
                        .clamp(0, screenSize.width - _fabSize),
                    (_fabPosition.dy + details.delta.dy)
                        .clamp(paddingTop, screenSize.height - paddingBottom - _fabSize),
                  );
                  _isDragging = true;
                });
              },
              onPanEnd: (_) =&amp;gt; setState(() =&amp;gt; _isDragging = false),
              child: FloatingActionButton(
                onPressed: _isDragging ? null : () =&amp;gt; print("FAB Pressed!"),
                child: Icon(_isDragging ? Icons.drag_handle : Icons.add),
              ),
            ),
          ),
      ],
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Possible Enhancements
&lt;/h2&gt;

&lt;p&gt;Edge-Snapping → Make the FAB snap to screen edges when released.&lt;br&gt;
Expandable → Creating expandable FAB widget with other buttons&lt;br&gt;
Shape → Circular shaped FAB or even changing shapes while dragging&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With just GestureDetector + Positioned, we’ve built a fully draggable FAB that enhances UX while staying performant. Try it in your next Flutter project!&lt;/p&gt;

&lt;h3&gt;
  
  
  Credits &amp;amp; References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://flutter.dev/docs" rel="noopener noreferrer"&gt;Flutter Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://material.io/components/buttons-floating-action-button" rel="noopener noreferrer"&gt;Material Design Guidelines&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll push all the code on my GitHub repository and will put the link &lt;a href="https://github.com/alikhoshsafa" rel="noopener noreferrer"&gt;here &lt;/a&gt;later on.&lt;/p&gt;

&lt;p&gt;Let me know in the comments how you’d improve this! 😊​&lt;br&gt;
Follow me for more Flutter tips!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>mobile</category>
      <category>development</category>
    </item>
  </channel>
</rss>
