<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Flutter on smarx.com</title>
    <link>https://smarx.com/tags/flutter/</link>
    <description>Recent content on smarx.com</description>
    <language>en-us</language>
    <managingEditor>smarx@smarx.com (Steve Marx)</managingEditor>
    <webMaster>smarx@smarx.com (Steve Marx)</webMaster>
    <lastBuildDate>Thu, 13 Aug 2020 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://smarx.com/tags/flutter/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Automatic Scroll-To-Bottom in Flutter</title>
      <link>https://smarx.com/posts/2020/08/automatic-scroll-to-bottom-in-flutter/</link>
      <pubDate>Thu, 13 Aug 2020 00:00:00 +0000</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/automatic-scroll-to-bottom-in-flutter/</guid>
      <description>




    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/automatic-scroll-to-bottom-in-flutter/auto-scroll-featured.png&#34; data-lightbox=&#34;e2d50861ee4f0d886f5992c8a0cd7e7e&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/08/automatic-scroll-to-bottom-in-flutter/auto-scroll-featured_huedecc16f9995a73752b2c1d9c6d301a9_106072_384x0_resize_lanczos_2.png&#34; width=&#34;384&#34; height=&#34;217&#34;&gt;
    
    &lt;/a&gt;
    
    
&lt;/figure&gt;

&lt;p&gt;Earlier today, I saw a question on the &lt;a href=&#34;https://discord.com/invite/N7Yshp4&#34;&gt;FlutterDev Discord&lt;/a&gt; about how to automatically scroll to the bottom of a list when a new item is added.&lt;/p&gt;
&lt;p&gt;Getting this right requires understanding when various things happen in Flutter, including state updates, tree rebuilds, and rendering.&lt;/p&gt;
&lt;h2 id=&#34;demo&#34;&gt;Demo&lt;/h2&gt;
&lt;p&gt;Below is the end result, a chat-like interface where new messages trigger an automatic scroll-to-bottom. This is a live demo where a new random message appears every two seconds. Once enough messages load, try scrolling up to see what happens when a new message comes in:&lt;/p&gt;
&lt;iframe class=&#34;mx-auto border&#34; style=&#34;max-width:400px;height:600px&#34; src=&#34;https://smarx.com/demos/flutter-scroll-to-bottom/index.html&#34;&gt;&lt;/iframe&gt;
&lt;h2 id=&#34;using-a-_scrollcontroller_&#34;&gt;Using a &lt;em&gt;ScrollController&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;To perform any programmatic scrolling, you need to use a &lt;a href=&#34;https://api.flutter.dev/flutter/widgets/ScrollController-class.html&#34;&gt;&lt;code&gt;ScrollController&lt;/code&gt;&lt;/a&gt;. Here&amp;rsquo;s the basic structure of the demo UI, with a &lt;code&gt;ScrollController&lt;/code&gt; configured on the &lt;code&gt;ListView&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-dart&#34; data-lang=&#34;dart&#34;&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;_ChatUIState&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extends&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;State&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ChatUI&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_messages&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
  &lt;span class=&#34;kd&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ScrollController&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ScrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;

  &lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;

  &lt;span class=&#34;n&#34;&gt;build&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BuildContext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ListView&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
      &lt;span class=&#34;nl&#34;&gt;controller:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
      &lt;span class=&#34;nl&#34;&gt;children:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_messages&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;msg&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ChatBubble&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;msg&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toList&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(),&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://api.flutter.dev/flutter/widgets/ScrollController/animateTo.html&#34;&gt;&lt;code&gt;ScrollController.animateTo()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&#34;https://api.flutter.dev/flutter/widgets/ScrollPosition/maxScrollExtent.html&#34;&gt;&lt;code&gt;ScrollPosition.maxScrollExtent&lt;/code&gt;&lt;/a&gt; can be used together to smoothly scroll to the bottom of the view:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-dart&#34; data-lang=&#34;dart&#34;&gt;&lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animateTo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;position&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;maxScrollExtent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
  &lt;span class=&#34;nl&#34;&gt;duration:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Duration&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nl&#34;&gt;milliseconds:&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;200&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
  &lt;span class=&#34;nl&#34;&gt;curve:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Curves&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;easeInOut&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;but-when&#34;&gt;But when?&lt;/h2&gt;
&lt;p&gt;If you just want to scroll when the user clicks a button, you already have all the code you need. But if you want to automatically scroll when new items are added to the list, you&amp;rsquo;ll soon run into trouble.&lt;/p&gt;
&lt;p&gt;Your first attempt would likely look like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-dart&#34; data-lang=&#34;dart&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// when it&amp;#39;s time to display a new message
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;_addMessage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animateTo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;position&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;maxScrollExtent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;...);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This looks reasonable, but if you try it, you&amp;rsquo;ll find that the list is always being scrolled to the &lt;em&gt;second-to-last&lt;/em&gt; item in the &lt;code&gt;ListView&lt;/code&gt;. Why is that?&lt;/p&gt;
&lt;p&gt;The reason is timing. The call to &lt;code&gt;animateTo()&lt;/code&gt; is executed before the UI has been rebuilt with the new message, so the &lt;code&gt;maxScrollExtent&lt;/code&gt; is the &lt;em&gt;old&lt;/em&gt; extent.&lt;/p&gt;
&lt;p&gt;The developer struggling with this on the FlutterDev Discord added a &lt;code&gt;Future.delayed()&lt;/code&gt; call to wait 50ms before scrolling. This worked because it gave the UI enough time to update first, but the developer was understandably bothered by this sort of hack.&lt;/p&gt;
&lt;h2 id=&#34;waiting-until-the-rebuild&#34;&gt;Waiting until the rebuild&lt;/h2&gt;
&lt;p&gt;To get the timing right, you need to wait for two things to happen after the &lt;code&gt;setState()&lt;/code&gt; call adds the new message to the widget state:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You need to wait for Flutter to &lt;strong&gt;rebuild&lt;/strong&gt; the widget.&lt;/li&gt;
&lt;li&gt;You need to wait for Flutter to &lt;strong&gt;render&lt;/strong&gt; the UI so the scroll extent is updated.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It&amp;rsquo;s easy to wait for the rebuild. You can just move the &lt;code&gt;animateTo()&lt;/code&gt; call to your &lt;code&gt;build()&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-dart&#34; data-lang=&#34;dart&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// Keep track of whether a scroll is needed.
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;bool&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;_scrollToEnd&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;async&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;animateTo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_scrollController&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;position&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;maxScrollExtent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;...);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;_addMessage&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;
  &lt;span class=&#34;c1&#34;&gt;// Instead of scrolling here, flag that a scroll is needed.
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;  &lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;build&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BuildContext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_scrollToEnd&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This still isn&amp;rsquo;t quite right. &lt;code&gt;_scrollToEnd()&lt;/code&gt; runs before any of the UI has been rendered to the screen. To wait for that to happen, you can use &lt;a href=&#34;https://api.flutter.dev/flutter/scheduler/SchedulerBinding/addPostFrameCallback.html&#34;&gt;WidgetsBinding.instance.addPostFrameCallback()&lt;/a&gt;, which schedules a callback to run at the end of the render frame.&lt;/p&gt;
&lt;p&gt;With this tiny tweak to the &lt;code&gt;build()&lt;/code&gt; method, everything works properly:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-dart&#34; data-lang=&#34;dart&#34;&gt;&lt;span class=&#34;n&#34;&gt;build&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BuildContext&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;WidgetsBinding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;addPostFrameCallback&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;_scrollToEnd&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;());&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;_needsScroll&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;warning-a-bug-i-couldnt-fix&#34;&gt;Warning: a bug I couldn&amp;rsquo;t fix&lt;/h2&gt;
&lt;p&gt;If you play with the demo for awhile, you&amp;rsquo;ll notice that it doesn&amp;rsquo;t always scroll the whole way to the bottom. I believe this only happens when you&amp;rsquo;re not already scrolled to the bottom, but I&amp;rsquo;m not sure. If you know why this happens, please let me know so I can update this post!&lt;/p&gt;
&lt;h2 id=&#34;full-source-code&#34;&gt;Full source code&lt;/h2&gt;
&lt;p&gt;You can find the full demo code on &lt;a href=&#34;https://dartpad.dev/959ea12d6690f2af7da9c1f8dd54860f&#34;&gt;DartPad&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
