<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Padmal on Medium]]></title>
        <description><![CDATA[Stories by Padmal on Medium]]></description>
        <link>https://medium.com/@blog.padmal?source=rss-4cdb3ebf8936------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wsETKt7NRdG3mE_g.jpg</url>
            <title>Stories by Padmal on Medium</title>
            <link>https://medium.com/@blog.padmal?source=rss-4cdb3ebf8936------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 08:00:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@blog.padmal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Flutter Dismissible Widget swipe both ways !!!]]></title>
            <link>https://medium.com/@blog.padmal/flutter-dismissible-widget-swipe-both-ways-a696a1edb67b?source=rss-4cdb3ebf8936------2</link>
            <guid isPermaLink="false">https://medium.com/p/a696a1edb67b</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[dismissible]]></category>
            <category><![CDATA[swipe-to-dismiss]]></category>
            <category><![CDATA[dismissible-widget]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Padmal]]></dc:creator>
            <pubDate>Tue, 09 Jul 2019 18:02:44 GMT</pubDate>
            <atom:updated>2019-07-09T18:02:44.056Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flutter Dismissible Widget swipe both ways !!!</h3><p>When I open Gmail app from my mobile, I use swipe right to delete and swipe left to archive my emails. Of course this needs to be pre-configured from Gmail settings and that’s not what we’re going to learn from this article..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_b108ACcWXgt_N-fXzzGzQ.png" /></figure><p>Most of the apps have this feature. Rather than interacting with just a click and/or a long click, it’s very convenient for user to interact with list items this way.</p><h3><strong>Not sure why? </strong>🤔</h3><p>🅰️ No need to tell user what the action does. You can put a text and/or an icon with a color for him to understand what the swiping action would do. With a button.. OK let him figure out..</p><p>🅱️ You have more actions now. — Click to open — Long click to move — Swipe left to delete — Swipe right to edit etc etc..</p><p>If you’re planning to have this feature in your Flutter app, let me tell you how to do it.</p><p>Before we begin, make sure you have Flutter and IDE configured. I’m going to use Android Studio through out this.</p><p>First we need a list! I’m going to start a new Flutter project and use the boilerplate code for a start. We’ll follow step by step.</p><p>We are using a Stateful widget here as the list view needs to get updated dynamically every time as user interacts with the screen. If you’re unsure about this, please have a read at Flutter Stateful widgets.</p><h3><strong>Step 1: Create a List of Items</strong></h3><p>Create a variable at the top of the state class as follows;</p><pre><strong>class </strong>_MyHomePageState <strong>extends </strong>State&lt;MyHomePage&gt; {<br>    <strong>final itemsList </strong>= List&lt;String&gt;.generate(10, (n) =&gt; <strong>&quot;List item </strong>${n}<strong>&quot;</strong>);</pre><pre>...<br>}</pre><p>This will generate 10 strings starting from <strong>List item 0</strong>, <strong>List item 1</strong> up-to <strong>List item 9</strong>.</p><h3>Step 2: Generate the List</h3><p>We’ll use the 10 strings generated in Step 1 to populate a list view using ListView builder in Flutter. Add the following method to the state class.</p><pre>ListView generateItemsList() {<br>  <strong>return </strong>ListView.builder(<br>    itemCount: <strong>itemsList</strong>.<strong>length</strong>,<br>    itemBuilder: (context, index) {<br>      <strong>return </strong>ListTile(title: Text(<strong>&#39;</strong>${<strong>itemsList</strong>[index]}<strong>&#39;</strong>));<br>    },<br>  );<br>}</pre><p>Add this method as the body property in widget build method.</p><pre>@override<br>Widget build(BuildContext context) {<br>  <strong>return </strong>Scaffold(<br>    appBar: AppBar(<br>      title: Text(<strong>widget</strong>.<strong>title</strong>),<br>    ),<br>    body: generateItemsList(),<br>  );<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*VSOUEAcVMbXvFWCnlpH0iw.png" /></figure><h3>Step 3: Give the list item a click effect</h3><p>Now we have a list of items. I think it’s better we give them a clickable behavior; like a ripple effect when user clicks on it. Flutter provides a special widget called <strong>InkWell</strong> widget; once you wrap it around a widget like a ListTile, it’ll add a clickable behavior to it. (That means a ripple 🌊)</p><p>We’ll change the <strong>itemBuilder</strong> property of <strong>generateItemsList()</strong> as follows;</p><pre>itemBuilder: (context, index) {<br>  <strong>return </strong>InkWell(<br>      onTap: () {<br>        print(<strong>&quot;</strong>${<strong>itemsList</strong>[index]}<strong> clicked&quot;</strong>);<br>      },<br>      child: ListTile(title: Text(<strong>&#39;</strong>${<strong>itemsList</strong>[index]}<strong>&#39;</strong>)));<br>},</pre><p>Render the view and see how it adds the ripple click behavior to each list item and the console will show you the message “List item n clicked” …</p><h3>Step 4: Wrap list items around Dismissible Widget</h3><pre>itemBuilder: (context, index) {<br>  <strong>return </strong>Dismissible(<br>    child: InkWell(<br>        onTap: () {<br>          print(<strong>&quot;</strong>${<strong>itemsList</strong>[index]}<strong> clicked&quot;</strong>);<br>        },<br>        child: ListTile(title: Text(<strong>&#39;</strong>${<strong>itemsList</strong>[index]}<strong>&#39;</strong>))),<br>  );<br>}</pre><p>Once we wrap it around a <strong>Dismissible</strong> widget, it’ll show us an error saying it needs a key. Flutter maintain keys to access widgets and children to help it rebuild the widget tree. Same goes for Dismissible items. For them to be separable, they need unique keys. In our case, each list item name is pretty much unique. So we can straightaway use item name as the key.</p><pre><strong>return </strong>Dismissible(<br>  key: Key(<strong>itemsList</strong>[index]),<br>  child: InkWell(<br>      onTap: () {<br>        print(<strong>&quot;</strong>${<strong>itemsList</strong>[index]}<strong> clicked&quot;</strong>);<br>      },<br>      child: ListTile(title: Text(<strong>&#39;</strong>${<strong>itemsList</strong>[index]}<strong>&#39;</strong>))),<br>);</pre><p>Now let’s render our app and see. Try sliding items left and right to see they are getting removed from the list view. Pretty easy…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*35ot8sqGakA_qZrM85Il0Q.png" /></figure><p>Now comes the important parts. Dismissible widget has a few properties. Let me summarize the ones that are important for us to implement swipe both ways to get two actions done.</p><ol><li><strong>background</strong>: It’s the widget that is visible when we slide the list item to right. There’s another property called secondaryBackground. If that property is not set, this will be the background for both sliding directions.</li><li><strong>secondaryBackground</strong>: It’s the widget that is visible when we slide the list item to left.</li><li><strong>confirmDismiss</strong>: This is where we can integrate a user input for confirmation such as a dialog box prompting user to delete or not.</li><li><strong>onDismissed</strong>: This method is called when the list item is gone from the view. We can put an action like “Undo Delete” here.</li></ol><h3>Step 5: Add swipe backgrounds to list items</h3><p>Now we are going to use both the <strong>background</strong> and <strong>secondaryBackground</strong> properties to set custom widgets; one for delete and one for edit. Let’s create two methods for both the widgets.</p><p><strong>Right side sliding :: goes as “background”</strong></p><pre>Widget slideRightBackground() {<br>  <strong>return </strong>Container(<br>    color: Colors.<em>green</em>,<br>    child: Align(<br>      child: Row(<br>        mainAxisAlignment: MainAxisAlignment.<strong>start</strong>,<br>        children: &lt;Widget&gt;[<br>          SizedBox(<br>            width: 20,<br>          ),<br>          Icon(<br>            Icons.<em>edit</em>,<br>            color: Colors.<em>white</em>,<br>          ),<br>          Text(<br>            <strong>&quot; Edit&quot;</strong>,<br>            style: TextStyle(<br>              color: Colors.<em>white</em>,<br>              fontWeight: FontWeight.<em>w700</em>,<br>            ),<br>            textAlign: TextAlign.<strong>left</strong>,<br>          ),<br>        ],<br>      ),<br>      alignment: Alignment.<em>centerLeft</em>,<br>    ),<br>  );<br>}</pre><p><strong>Left side sliding :: goes as “secondaryBackground”</strong></p><pre>Widget slideLeftBackground() {<br>  <strong>return </strong>Container(<br>    color: Colors.<em>red</em>,<br>    child: Align(<br>      child: Row(<br>        mainAxisAlignment: MainAxisAlignment.<strong>end</strong>,<br>        children: &lt;Widget&gt;[<br>          Icon(<br>            Icons.<em>delete</em>,<br>            color: Colors.<em>white</em>,<br>          ),<br>          Text(<br>            <strong>&quot; Delete&quot;</strong>,<br>            style: TextStyle(<br>              color: Colors.<em>white</em>,<br>              fontWeight: FontWeight.<em>w700</em>,<br>            ),<br>            textAlign: TextAlign.<strong>right</strong>,<br>          ),<br>          SizedBox(<br>            width: 20,<br>          ),<br>        ],<br>      ),<br>      alignment: Alignment.<em>centerRight</em>,<br>    ),<br>  );<br>}</pre><p>Now set these two widgets as properties for backgrounds as follows;</p><pre>background: slideRightBackground(),<br>secondaryBackground: slideLeftBackground(),</pre><p>Let’s see how it looks;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*r3IAh61wAYNxiFxG2u-tow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*YGwwXTTWtF08UbzEXMyBMg.png" /></figure><h3>Step 6: Add actions to swipes</h3><p>Before doing something like a deletion, it’s wise to ask for user confirmation. Let’s add the feature using <strong>confirmDismiss</strong> property of Dismissible widget.</p><pre>confirmDismiss: (direction) <strong>async </strong>{<br>  <strong>if </strong>(direction == DismissDirection.<strong>endToStart</strong>) {<br>    <strong>final </strong>bool res = <strong>await </strong>showDialog(<br>        context: context,<br>        builder: (BuildContext context) {<br>          <strong>return </strong>AlertDialog(<br>            content: Text(<br>                <strong>&quot;Are you sure you want to delete </strong>${<strong>itemsList</strong>[index]}<strong>?&quot;</strong>),<br>            actions: &lt;Widget&gt;[<br>              FlatButton(<br>                child: Text(<br>                  <strong>&quot;Cancel&quot;</strong>,<br>                  style: TextStyle(color: Colors.<em>black</em>),<br>                ),<br>                onPressed: () {<br>                  Navigator.<em>of</em>(context).pop();<br>                },<br>              ),<br>              FlatButton(<br>                child: Text(<br>                  <strong>&quot;Delete&quot;</strong>,<br>                  style: TextStyle(color: Colors.<em>red</em>),<br>                ),<br>                onPressed: () {<br>                  <em>// </em><strong><em>TODO: Delete the item from DB etc..<br>                  </em></strong>setState(() {<br>                    <strong>itemsList</strong>.removeAt(index);<br>                  });<br>                  Navigator.<em>of</em>(context).pop();<br>                },<br>              ),<br>            ],<br>          );<br>        });<br>    <strong>return </strong>res;<br>  } <strong>else </strong>{<br>    <em>// </em><strong><em>TODO: Navigate to edit page;<br>  </em></strong>}<br>},</pre><p>Since we are waiting for a user input, an asynchronous method is used to handle this property. The result is something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*p-lUzIOO041ETSBnxeWybA.png" /></figure><p>That’s all :) Find the complete code for this article <a href="https://gist.github.com/CloudyPadmal/4512122c0db5d730aeee6c59f376a2cf">here</a> for you to copy paste and try out other parameters such as <strong>direction</strong>, <strong>dismissThreshold</strong> provided by the <strong>Dismissible</strong> widget.</p><p>For more information, please read the official documentation :::</p><p><a href="https://api.flutter.dev/flutter/widgets/Dismissible-class.html">Dismissible class</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a696a1edb67b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>