<?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 Tsung-Wei Hsu on Medium]]></title>
        <description><![CDATA[Stories by Tsung-Wei Hsu on Medium]]></description>
        <link>https://medium.com/@tsung-wei_hsu?source=rss-ae777bb3a6bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xYMFtR7pgLWnd7fpRAATPQ.jpeg</url>
            <title>Stories by Tsung-Wei Hsu on Medium</title>
            <link>https://medium.com/@tsung-wei_hsu?source=rss-ae777bb3a6bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:39:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tsung-wei_hsu/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: How to Build Draggable Bottom Sheet like Google Maps]]></title>
            <link>https://medium.com/@tsung-wei_hsu/flutter-how-to-build-draggable-bottom-sheet-like-google-maps-1165f5b07366?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/1165f5b07366</guid>
            <category><![CDATA[bottomsheet]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[widget]]></category>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Sat, 02 Sep 2023 17:52:44 GMT</pubDate>
            <atom:updated>2023-09-02T17:52:44.489Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Photo by balazsketyi modified." src="https://cdn-images-1.medium.com/max/1024/1*cuXsFADoLyhbMxYatxPMEQ.png" /></figure><p>A draggable bottom sheet, that we see quite often especially in navigation apps, where a child view for details needs be placed alongside with the parent view. The widgets such as <strong>BottomSheet</strong>, <strong>ModalBottomSheet</strong> or <strong>Dialog</strong> will probably come to mind quickly, but also quickly do we realize, that their nature makes the parent view <em>non-interactable</em> and dimmed out, thus unusable.</p><p>For this specific view, what we want normally to achieve are:</p><ul><li>The child gives additional info to the parent.</li><li>The child is sizable and dismissible.</li><li>Both the parent and the child are interactable.</li></ul><p>Luckily, there IS an official widget that check all the boxes above called <strong>DraggableScrollableSheet</strong>, here are some tips and quirks of how to implement it smoothly into your apps:</p><h4>Build the Draggable Sheet</h4><p>The sheet is relatively flexible and can be placed in any container. Below, we create a sheet with all attributes it offers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cdcfcaad1ec6737cab76baa04824168/href">https://medium.com/media/3cdcfcaad1ec6737cab76baa04824168/href</a></iframe><p>Firstly, we set the <strong>initialChildSize</strong> to 0.5 making it shown by default. Otherwise, setting it to 0 hides the sheet at the beginning.</p><p>All <strong>sizes</strong> are defined fractionally to the parent height, e.g. a max size of 0.5 means the sheet can be dragged up to the half of its parent height. Here, the sheet can be expanded to fully cover the parent.</p><p><strong>snap</strong> enables the behavior that the sheet will move to the closest snap point instead of staying still at the dragging position. It is also worth noting that <strong>maxChildSize</strong> and <strong>minChildSize</strong> are automatically treated as a snap point, so no need to define them again in <strong>snapSizes</strong>.</p><p>It is <strong>crucial</strong> to assign the <strong>scrollController</strong> in the builder, if you have a scrollable widget (actually the most cases). It helps the sheet to enable scrolling for the scrollable widget, when the sheet reaches its maximal height. If not assigned, you could still scroll but the sheet cannot be dragged at all.</p><h4>Tune the Sizes</h4><p>It is often desirable that the sheet has a collapsed state before it is completely closed. In addition, the collapsed state usually has a <strong>specific</strong> height instead of a fractional. With <strong>LayoutBuilder</strong> below, we obtain the parent height and turn the exact height of 60px to the fractional:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1402223e00e9b4e51f09eb8a30273d3/href">https://medium.com/media/e1402223e00e9b4e51f09eb8a30273d3/href</a></iframe><p>Now, the standard states of a bottom sheet have been established, namely <em>expanded</em>, <em>anchored</em>, <em>collapsed</em> and lastly <em>hidden</em> states.</p><h4>Handle the Positions</h4><p>Since the <strong>minChildSize</strong> is 0, the sheet will snap and close itself, when users drag too deep to the bottom, which we in some cases want to avoid. That’s when the <strong>_controller</strong> defined above takes over by adding a <strong>listener</strong> at the initialization:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19987e88e7213d1d90cfb8abff8ba283/href">https://medium.com/media/19987e88e7213d1d90cfb8abff8ba283/href</a></iframe><p>The listener fires every time when the sheet is being dragged, in which we can properly track the current size of the sheet and, with a simple condition, prevent it from snapping to 0 and stay above collapsed point.</p><p>Remember <strong>_sheet</strong> given to the sheet widget previously? It is now used as the reference to access <strong>snapSizes</strong> defined in the build tree as well as all the rest of its attributes like <strong>maxChildSize</strong>, etc. These values help creating further actions to position the sheet and make whole sheet control relatively dynamic.</p><h4>Customize as Wished</h4><p>Now, it’s time for you to customize what content should be presented inside the sheet by individual use cases. Below are key takeaways of building a well-functioning sheet with background interactable:</p><ul><li><strong>DraggableScrollableSheet</strong> is not callable such as <strong>showBottomSheet()</strong> but a widget placed directly into the widget tree.</li><li>All sizes refer to height and are defined fractionally depending on the parent widget.</li><li>Both <strong>GlobalKey</strong> and <strong>LayoutBuilder</strong> help obtaining useful attributes like the parent height, snap sizes, etc.</li><li>The Sheet should contain a scrollable view assigned with the <strong>ScrollController</strong> provided by the builder, so that the dragging of the sheet and the scrolling of content do not overlapped.</li><li>Consider adding a state management, which enables the control of sheet positions above or below the widget tree.</li></ul><p>Eventually, you might get something like this resembling the mainstream navigation apps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*nuYK_wi47-OnxbsREAjPXw.gif" /><figcaption>A mockup of mainstream navigation apps.</figcaption></figure><p>Happy Coding! 🙌🏼<br>Below are some useful insights in Flutter, which might also interest you:</p><ul><li><a href="https://medium.com/@tsung-wei_hsu/a-crucial-performance-fundamental-i-wish-ive-learned-sooner-in-flutter-e5f452654c30">A Crucial Performance Fundamental I Wish I’ve Learned Sooner in Flutter</a></li><li><a href="https://medium.com/@tsung-wei_hsu/flutter-custom-expandable-i-e-expansionpanel-aa8e056324c">Flutter: Custom Expandable i.e. ExpansionPanel</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1165f5b07366" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Crucial Performance Fundamental I Wish I’ve Learned Sooner in Flutter]]></title>
            <link>https://medium.com/@tsung-wei_hsu/a-crucial-performance-fundamental-i-wish-ive-learned-sooner-in-flutter-e5f452654c30?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5f452654c30</guid>
            <category><![CDATA[flutter-stateful-widget]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[data-management]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Mon, 03 Jul 2023 21:25:37 GMT</pubDate>
            <atom:updated>2023-07-03T21:25:37.258Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N2aioFpv_QUqho9j" /><figcaption>Photo by <a href="https://unsplash.com/@dose?utm_source=medium&amp;utm_medium=referral">Dose Media</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If being asked to name one thing that I wish I’ve known sooner in Flutter, it would definitely be <strong><em>the art of state management</em></strong>, of which importance grows significantly as your app develops. It could have an enormous impact on the overall performance, if not being handled properly, a hard lesson learned speaking from own experiences.</p><p>Let me explain why:</p><p>The well-known Stateful Widget helps you manipulate UI according to the change of state, which subsequently triggers a re-rendering of the widget, a very simple work flow. However, an often overlooked fact is that we build apps under the structure of multiple widget trees, where data is shared in complexity within. Passing down variables to child widgets could be a normal practice for new comers. The app could look like this:</p><figure><img alt="A typical mother widget containing a tree of child widgets." src="https://cdn-images-1.medium.com/max/500/1*8ZAFIS9i-Up8gn6riXqDpw.png" /><figcaption>A typical mother widget containing a tree of child widgets.</figcaption></figure><p>When assigning a new value to Variable 4, Widget F is re-rendered as expected, just one, easy peasy. But what if changing the value inside the mother widget such as Variable 2? It might shock you that every single widget extended from the mother will be re-rendered, 6 widgets in sum, even the widgets totally unrelated to Variable 2 (B, D, E and F). Not to mention, widget trees in reality are usually more complicated. A rendering of the whole tree could cause a serious suffering on performance.</p><p>This is when the importance of state management shines and should be urgently considered from the start of the development, if your app is going to scale. There are 3 disciplines I hold on making sure that states are properly managed:</p><p>✖ <strong>Cautious When Using </strong><strong>setState()</strong><br>Develop not merely code but also the consciousness towards it. Every time when using setState(), ask the question of how broad the impact could be and how many widgets would be affected and possibly pended to re-rendering.</p><p>✖ <strong>The Use of </strong><strong>const</strong><br>A prefix that could deliver an immense win of efficiency. Assigning this powerful keyword to a widget basically tells Flutter that this widget does not change and will always stay constant. When a re-render is triggered from a mother widget, Flutter will NOT re-render any const children beneath it. Take a close look at the previous example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ngGJZTItepHipNwOFKQf9g.png" /><figcaption>Widget tree with const widgets.</figcaption></figure><p>Here, we are able to assign const to D and F that do not have a relation to their parents. Now, a re-render from the mother widget will skip these two reducing the original amount from 6 to 4.</p><p>✖ <strong>The Use of Data Providers</strong><br>The most advanced strategy would be relocating variables to a data provider eliminating the dependency between widgets and also making variables accessible to any widgets down to the widget tree. A look at the previous model with a data provider:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NJy9jM2nZUxQJfYaKGdv_g.png" /><figcaption>Widget tree with a data provider, e.g. Provider() and Consumer().</figcaption></figure><p>Here, we do not pass down any variables. Instead, they are given from the data provider breaking the bounds of dependency. Each widget can now be defined as const type. Let’s apply a change to Variable 2 same as the previous examples. This time, only Widget A and C, that contain the variable, will be re-rendered further reducing the render amount from 6 to merely 2 or even not the 2 whole widgets (see the example below).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e51017e6dc5205df7886b387222e325/href">https://medium.com/media/9e51017e6dc5205df7886b387222e325/href</a></iframe><p>Aside from the native Provider() and Consumer(), there are several other powerful state management libraries such as <a href="https://pub.dev/packages/bloc">Bloc</a> and <a href="https://pub.dev/packages/riverpod">Riverpod</a> worth checking out.</p><p>Overall, the best to handle states seems to be the last, but it should always be weighted based on individual cases to flexibly apply the mixture of them all. Building an app with a conscious management over states from the beginning is really the key to the optimal performance and spares lots of time on reworking it, which I greatly wish every new comer can acknowledge sooner. And now you do :)</p><p>These might interest you as well:</p><ul><li><a href="https://medium.com/@tsung-wei_hsu/flutter-page-transition-for-both-entering-and-leaving-pages-de4a4b100737">Flutter: Page Transition for Both Entering and Leaving Pages</a></li><li><a href="https://medium.com/@tsung-wei_hsu/flutter-custom-expandable-i-e-expansionpanel-aa8e056324c">Flutter: Custom Expandable i.e. ExpansionPanel</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5f452654c30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter: Page Transition for Both Entering and Leaving Pages]]></title>
            <link>https://medium.com/@tsung-wei_hsu/flutter-page-transition-for-both-entering-and-leaving-pages-de4a4b100737?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/de4a4b100737</guid>
            <category><![CDATA[route]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[transitions]]></category>
            <category><![CDATA[page-transition]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Mon, 17 Apr 2023 13:20:03 GMT</pubDate>
            <atom:updated>2023-04-17T13:20:56.101Z</atom:updated>
            <content:encoded><![CDATA[<p>Page Transition is undoubtedly one of the fundamentals in app development, and Flutter already offers a great default transition that aligns aesthetically with its platforms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*LZZIZZxHIqzVXoYksUaTfQ.gif" /><figcaption>Left for Android and right for iOS</figcaption></figure><p>But what if you want to customize it as a part of your app design language, especially when the transitions take place in the context of a nested navigation where the standard one might not fit nicely?<br>Let’s get it programmed! 🙌🏼</p><p>If you’ve already created some pages with navigations, onGenerateRoute should be familiar to you, in which target pages are defined. Below is a simple example:</p><pre>// when using root navigation<br>MaterialApp(<br>  title: &#39;Demo App&#39;,<br>  onGenerateRoute: _onGenerateRoute,<br>  ...,<br>);<br><br>// when using nested navigation<br>Navigator(<br>  onGenerateRoute: _onGenerateRoute,<br>  ...,<br>)<br><br>Route _onGenerateRoute(RouteSettings settings) {<br>  final page = _buildPage(settings.name);<br>  return PageRouteBuilder(<br>    pageBuilder: (context, anim, secAnim) =&gt; page,<br>    transitionsBuilder: _buildTransitions,<br>  );<br>}<br><br>Widget _buildPage(name) {<br> switch (settings.name) {<br>    case &#39;/next&#39;:<br>      return NextPage();<br>    default:<br>      return HomePage();<br>  }<br>}</pre><p>There are several route builders such as MaterialPageRoute()and CupertinoPageRoute(), but here we use PageRouteBuilder() with a lower level of configurations. The param transitionsBuilder within is where we customize both entering and leaving animations.</p><p>Now, take a closer look at the builder itself which provides four inputs:<br>a build context, a primary animation, a secondary animation, and a child.</p><pre>return PageRouteBuilder(<br>  pageBuilder: (context, anim, secAnim) =&gt; page,<br>  transitionsBuilder: (context, anim, secAnim, child) {<br>    // we start here<br>  },<br>);</pre><ul><li>The primaryAnimation, also the most mentioned one, stands for the entering page, i.e. the animation of the next page.</li><li>The secondaryAnimation is the opposite for the leaving page which however is significantly less documented or described elsewhere. Here, we will tackle them both.</li></ul><p>Starting with <strong>the entering page</strong>, two tweens are defined separately for the slide and the fade transitions. Attach both tweens to the primary animation that makes the next page slide from the right side by Offset(1, 0) with the opacity from 0.</p><pre>return PageRouteBuilder(<br>  transitionsBuilder: (context, anim, secAnim, child) {<br>    // tween for the value tranistion of opacity<br>    final fadeInTween = Tween&lt;double&gt;(begin: 0, end: 1);<br>    // tween for the value tranistion of position<br>    final slideInTween = Tween&lt;Offset&gt;(<br>      begin: const Offset(1, 0),<br>      end: Offset.zero,<br>    );<br>    // make the next page slide into the screen with opicity<br>    return FadeTransition(<br>      opacity: anim.drive(fadeInTween),<br>      child: SlideTransition(<br>        position: anim.drive(slideInTween),<br>        child: child,<br>      ),<br>    );<br>  },<br>);</pre><p>It is worth noting that the wrap order does not affect the transitions, and their duration can be defined in the PageRouteBuilder() with the param transitionDuration. Your result should look similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*M9mWd9YXXwNCTqUJJShK3Q.gif" /><figcaption>Sliding in and out of the next page with a fade effect.</figcaption></figure><p>Next, we want animate <strong>the leaving page</strong> as well by creating the identical type of tweens but in a different direction, i.e. the previous page slides out to the left side by Offset(-1, 0) with the opacity reducing to 0.</p><pre>final slideOutTween = Tween&lt;Offset&gt;(<br>  begin: Offset.zero,<br>  end: const Offset(-1, 0),<br>);<br>final fadeOutTween = Tween&lt;double&gt;(begin: 1, end: 0);<br>return FadeTransition(<br>  opacity: secAnim.drive(fadeOutTween),<br>  child: SlideTransition(<br>    position: secAnim.drive(slideOutTween),<br>    child: FadeTransition(<br>      opacity: anim.drive(fadeInTween),<br>      child: SlideTransition(<br>        position: anim.drive(slideInTween),<br>        child: child,<br>      ),<br>    ),<br>  ),<br>);</pre><p>Same as its counterpart, the newly created tweens are applied to the secondary animation, that enables the transition of the previous page. Now, the page route animates both pages at the same time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*DGf2mOpLtafoUSLga75Kyw.gif" /><figcaption>A complete page transition for both entering and leaving pages</figcaption></figure><p>As described above, the wrapping order of the transitions does not influence the overall animation, but a grouping order, e.g. the secondary followed by the primary, might still be convenient for the readability.</p><p>To sum it up, with transitionsBuilder under PageRouteBuilder(), we understand that the primary animation controls the transition of the entering page similarly for the secondary in the relation to the leaving page. By respectively injecting them into the transition widgets, a complete transition of both pages is achieved.</p><p>Since the ground animations are exposed in the builder, the customization can surely go further with the additions such as curved animations, gesture manipulations, etc. The ability to swipe back to the previous page is certainly a useful extension, but it’s a topic of another day. Stay tuned! 📡</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de4a4b100737" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter: Custom Expandable i.e. ExpansionPanel]]></title>
            <link>https://medium.com/@tsung-wei_hsu/flutter-custom-expandable-i-e-expansionpanel-aa8e056324c?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa8e056324c</guid>
            <category><![CDATA[custom]]></category>
            <category><![CDATA[expansion]]></category>
            <category><![CDATA[widget]]></category>
            <category><![CDATA[expandable]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Sun, 22 Jan 2023 20:22:40 GMT</pubDate>
            <atom:updated>2023-09-02T17:55:48.321Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*0tL0BpRDBgeb_MD41HCq5g.jpeg" /></figure><p>A view containing a clickable header and a expandable body is almost a standard design pattern in every app. For that, Flutter has already a built-in widget called <a href="https://api.flutter.dev/flutter/material/ExpansionPanelList-class.html"><em>ExpansionPanelList</em></a> combining with <a href="https://api.flutter.dev/flutter/material/ExpansionPanel-class.html"><em>ExpansionPanel</em></a>(s) that create expandable content. However, it is not highly customisable with e.g. a chevron icon that cannot be removed.</p><p>Surely, there are a handful of plugins such as <a href="https://pub.dev/packages/expandable"><em>expandable</em></a> that serves the exact purpose. But what if we want the <strong>maximum*</strong> control optically as well as functionally? The best answer is of course Do-It-Ourselves!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*6NeQ0HNEI_Q9W1tSiCpDdw.gif" /></figure><p>1️⃣ Using <strong>AnimatedContainer()</strong><br>It might be the fastest and simplest way to achieve such behaviour when all bodies have the <strong>fixed*</strong> height. Starting with a StatefulWidget that contains a variable <em>_currentHeight</em> and a final <em>_expandedHeight</em> of your choice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/200c8eb7c394adf310174eff3438673a/href">https://medium.com/media/200c8eb7c394adf310174eff3438673a/href</a></iframe><p>Now, after the state change has been taken care of, we create the trigger of the expandable, namely the header, and its body inside the build method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/690f76cef4de7d0e1b78f64f4111676d/href">https://medium.com/media/690f76cef4de7d0e1b78f64f4111676d/href</a></iframe><p>That’s pretty much it with a result in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*2Vg9og1rJke0DbtuMAyjVQ.gif" /><figcaption>Expandables with a fixed height</figcaption></figure><p>Through manipulating the height of the container by switching the state of the current height between zero and the expanded height, the effect of an expandable view can be simulated. But what if the height depends on the size of the content?</p><p>2️⃣ Using <strong>SizeTransition()</strong> with <strong>AnimationController<br></strong>This approach can handle the bodies of <strong>dynamic*</strong> height. Since it is a deeper level of AnimatedContainer(), it might require the basic understanding of how animations are built in Flutter. Let’s first create a snippet of a StatefulWidget with an AnimationController, an Animation, a Tween and a state tracker. We initialise their values in the initState():</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70468648544c371fa5f0a9eddfdaf73d/href">https://medium.com/media/70468648544c371fa5f0a9eddfdaf73d/href</a></iframe><p>Use <strong>Tween()</strong> to define the <em>start</em> and <em>end</em> value of the animation. In our case, we want to completely hide and expose the expandable body. Thus, the start value of the body size should be 0 (collapse state) and the end 1 (expanded state). With <strong>forward()</strong> of the controller, the value moves from 0 to 1 within the duration of 150 milliseconds simulating the expansion effect. Similarly, <strong>reverse()</strong> does the opposite from 1 to 0.</p><p>Finally, a slight adjustment to the build method by adding <strong>SizeTransition()</strong> and the animation we’ve just configured with a given body widget:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8951df7ced60ee0949e2d2a6c6263a26/href">https://medium.com/media/8951df7ced60ee0949e2d2a6c6263a26/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*-Z3EWqktuZbvFuwlpToUkg.gif" /><figcaption>Expandables with different heights</figcaption></figure><p>Tada! It is that magical and simple to maximise the customisability of your expandable, where its functionality can certainly be further enriched as you need it to be. For instance, adding an additional chevron at the location you desire by using a second animation with RotationTransition(), or creating a controller that allows only one expandable to be opened at the same time among multiple others.</p><p>Before you go, these might also interest you:</p><ul><li><a href="https://medium.com/@tsung-wei_hsu/flutter-how-to-build-draggable-bottom-sheet-like-google-maps-1165f5b07366">Flutter: How to Build Draggable Bottom Sheet like Google Maps</a></li><li><a href="https://medium.com/@tsung-wei_hsu/a-crucial-performance-fundamental-i-wish-ive-learned-sooner-in-flutter-e5f452654c30">A Crucial Performance Fundamental I Wish I’ve Learned Sooner in Flutter</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa8e056324c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ionic: Background Geolocation Easily Managed With Leaflet]]></title>
            <link>https://medium.com/@tsung-wei_hsu/ionic-background-geolocation-easily-managed-with-leaflet-3ccbc315ec31?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ccbc315ec31</guid>
            <category><![CDATA[geolocation]]></category>
            <category><![CDATA[leaflet]]></category>
            <category><![CDATA[background]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[ionic]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Thu, 27 Oct 2022 22:37:41 GMT</pubDate>
            <atom:updated>2022-10-27T22:45:56.214Z</atom:updated>
            <content:encoded><![CDATA[<p>For a lot of apps (I beliebe yours as well 😉), Background Geolocation belongs to one of the most important features. But how to implement it effectively and make it easily to manage seems not well documented in the official Ionic website. Let’s just get started!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ZyMuAZHiqT11kr40oIv7yA.png" /></figure><p>📍 <strong>First</strong> | <em>Choose the RIGHT Plugin</em></p><p>Strangely, something to avoid is the <a href="https://ionicframework.com/docs/native/background-geolocation">official Ionic doc</a> which recommends a long not working plugin, that will frustrate one to her/his core when blindly followed. There are two good maintained for your arsenal:</p><p>1. <a href="https://github.com/capacitor-community/background-geolocation">capacitor-community/background-geolocation</a><br>2. <a href="https://github.com/HaylLtd/cordova-background-geolocation-plugin">HaylLtd/cordova-background-geolocation-plugin</a></p><p>Depending on your preference, I will choose the second one that provides a bit more configurations. The first one is however simple and quick to implement.</p><p>📍 <strong>Second</strong> | <em>Get the Plugin, Mobile Systems and Leaflet Ready</em></p><p>I will be using Angular under the hood throughout 🚨 and assume you’ve created <a href="https://ionicframework.com/docs/angular/your-first-app/deploying-mobile">os specific folders</a>, naming android and ios.</p><p>1. Install the geolocation plugin, rxjs, <a href="https://leafletjs.com/reference.html">leaflet</a> and lastly an Angular specific component <a href="https://github.com/bluehalo/ngx-leaflet">@asymmetrik/ngx-leaflet</a> which makes the manipulation of map elements later a breeze.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae0f425edec66b4ae1af07b79df6b9ce/href">https://medium.com/media/ae0f425edec66b4ae1af07b79df6b9ce/href</a></iframe><p>2. For Android and iOS, we need to adjust some system files for location service permissions and configurations for capacitor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b989d3f2bf7f88ffeca339b530fede74/href">https://medium.com/media/b989d3f2bf7f88ffeca339b530fede74/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecdfbed6e5f343a7a179089a2b093602/href">https://medium.com/media/ecdfbed6e5f343a7a179089a2b093602/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95c0726428aed13edff0a2ba0f2e2729/href">https://medium.com/media/95c0726428aed13edff0a2ba0f2e2729/href</a></iframe><p>📍 <strong>Third</strong> | <em>Build Your Background Geolocation Service</em></p><p>1. Import packages from the geolocation plugin and declare a const for the access to its functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fafa98cc91c5a165f23b99d08076848d/href">https://medium.com/media/fafa98cc91c5a165f23b99d08076848d/href</a></iframe><p>2. Build your background geolocation configuration (find <a href="https://haylltd.github.io/cordova-background-geolocation-plugin/api.html#configureoptions-success-fail">Doc</a> here). Below is an example which I often use with of course some worth-noting explanations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23999d99339a7673880def4f265f5556/href">https://medium.com/media/23999d99339a7673880def4f265f5556/href</a></iframe><p>⚙️ <strong>[desiredAccuracy]</strong><br>The lower the accuracy is, the less power it consumes, which is a crucial factor for the battery life!<br>⚙️ <strong>[locationProvider]</strong><br>By default, it uses DISTANCE_FILTER_PROVIDER, in which the distance required to generate a location update increases alongside the increase of the moving speed. This is NOT optimal, if you aim for accuracy. RAW_PROVIDER generates however a location update exactly according to your defined distanceFilter. In the example, every 50 meters will generate a location update.<br>⚙️ <strong>[distanceFilter]</strong><br>The moving distance required for a location update.<br>⚙️ <strong>[interval]</strong><br>The minimum time between two location updates. <strong>Important</strong>: the default is 1 minute meaning you have to wait for 1 minute to get the next update.<br>⚙️ <strong>[notificationTitle]</strong> <strong>[notificationText]</strong><br>On Android, there will be a sticky notification that keeps the geolocation service continue to work in the background. You can freely configure its title and text.<br>⚙️ <strong>[notificationIconSmall]</strong><br>The icon of the sticky notification. This icon file should be placed in android/app/src/main/res/drawable in a xml format, namely ic_location.xml in this example.</p><p>3. Create a listener for geolocation updates which contain <a href="https://haylltd.github.io/cordova-background-geolocation-plugin/events.html#location-event">attributes</a> you can use to build your own functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a23ef386d43057880de1072a85eb0c7/href">https://medium.com/media/9a23ef386d43057880de1072a85eb0c7/href</a></iframe><p>📍 <strong>Last</strong> | <em>Create Your Map to Show the Movement!</em></p><p>The <a href="https://github.com/bluehalo/ngx-leaflet">@asymmetrik/ngx-leaflet</a> we just installed simplifies the use of leaflet significantly. Its directives help us build an ever-changing map like a usual angular component.</p><p>1. Create a div container with a leaflet directive and add the basic configurations for the base layer of the map. Any groups, layers and markers can be easily added and controlled by inner containers with [leafletLayer] as input.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddc828bc19d78419f8f7b9cd3d1972c4/href">https://medium.com/media/ddc828bc19d78419f8f7b9cd3d1972c4/href</a></iframe><p>2. Here, we create an observable [marker$] that extends the [position$] and transform it to the updated [marker], which is then used by [leafletLayer]. An advantage of using async directly in the template is that it gets unsubscribed automatically when the element is removed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55d2cdcb39db5f3453dd22cafc8b5757/href">https://medium.com/media/55d2cdcb39db5f3453dd22cafc8b5757/href</a></iframe><p>Cheers! You just got a marker that shows where the users are and keeps track of their movements even when the app is in the <strong>background 📡</strong>.</p><p><strong>🌻 Get Connected<br></strong>If you enjoyed this content, please feel free to share, leave a comment or just say hello :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*gI-ZGosYLpZFBXjTevrXWg.png" /></figure><p>🦉 <strong>Quirks </strong>| <em>Something to be Aware of?</em></p><p>1. <strong>Permissions</strong>:<br>Don’t forget to handle rejected permission requests properly. I suggest using <a href="https://capacitorjs.com/docs/apis/geolocation">official geolocation API</a> to smoothen this process. However, this API is only designed for the foreground location tracking.</p><p>2. <strong>Leaflet Stylesheet</strong>:<br>The map might look disoriented with grey blocks not being loaded. If this is the case, try importing its stylesheet in global.scss.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b1d8e9c41c686217bbbc71c298d8f12/href">https://medium.com/media/6b1d8e9c41c686217bbbc71c298d8f12/href</a></iframe><p>3. <strong>Leaflet Initialisation</strong>:<br>Ionic has its own component <a href="https://ionicframework.com/docs/angular/lifecycle">life cycle</a> that might cause your leaflet not initialised correctly. Using ionViewDidEnter() with invalidateSize() guarantees the load of map.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1df2ca2df7c1346b64cb4eed1fd2689/href">https://medium.com/media/a1df2ca2df7c1346b64cb4eed1fd2689/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ccbc315ec31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick Fix to CORS Errors in Angular]]></title>
            <link>https://medium.com/@tsung-wei_hsu/quick-fix-to-cors-errors-in-angular-569c411787a0?source=rss-ae777bb3a6bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/569c411787a0</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[proxy]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[production]]></category>
            <dc:creator><![CDATA[Tsung-Wei Hsu]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 21:50:36 GMT</pubDate>
            <atom:updated>2022-10-27T22:10:22.557Z</atom:updated>
            <content:encoded><![CDATA[<p>It is a common scenario in Angular, where your application requests data from a server and throw you up with red CORS errors all over the place.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9gclQ9QyQiXT8TIDbWbd5A.png" /><figcaption>Why doesn’t it just work…?!</figcaption></figure><p>No time to waste, deadlines await. Let’s get directly to the quick fix provided by Angular following the explanations, if you want to dive more into the nerdy stuff.</p><p>1. | Create a proxy.config.json in a place where you desire. Just make sure you find it easily! I would go with src/environments/proxy.config.json</p><p>2. | Modify angular.json located in the root of your project, so that Angular can find your new configuration. Navigate to “architect” → “serve”, and add options with your proxy file location you just created above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b0541d3ab0623ee7c2c06957da3dcf1/href">https://medium.com/media/8b0541d3ab0623ee7c2c06957da3dcf1/href</a></iframe><p>3. | Add settings to your proxy.config.json according to your needs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5a4d9c6ee4197008401077dc4070999/href">https://medium.com/media/d5a4d9c6ee4197008401077dc4070999/href</a></iframe><p><strong>/api/*</strong>: keyword that Angular checks for, whenever a HTTP request is made. * means any pattern behind.<br><strong>target</strong>: the target origin to be called. For instance, when /api/page/12 request is made, http://localhost:3000/api/page/12 is called.<br><strong>pathRewrite</strong>: the pattern to be replaced, if needed. For instance, when /api-rewrite/article/12 request is made, https://medium.com/article/12 is called.</p><p>4. | In your API service, set up HTTP requests regularly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59194caeb4f47d3cb9232ae93eefee30/href">https://medium.com/media/59194caeb4f47d3cb9232ae93eefee30/href</a></iframe><p>5. | Restart your application and PROFIT!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*lhGaW0MEa0Kxs5nUiMh97w.png" /></figure><p><strong>🚨 Very Important Fact<em><br></em></strong>This method although standardised should ONLY be used in the develop environment, which others omit to mention. This proxy config will be ignored by Angular in production. If you want to retrieve data from a server of a <strong>different</strong> origin, it should be handled from the backend.</p><p><strong>⚠️ Why Config Does Not Work in Production?<em><br></em></strong>This config is designed originally for development. Under the hood, Angular basically creates a dev proxy server that retrieves data for your web application. In development, it enables easy change of your desired API locations for debugging purposes. However, In production, your backend server should handle data retrieval outside your web origin.</p><p>💡 <strong>What Causes CORS Errors in the First Place?<br></strong>CORS stands for Cross-Origin Resource Sharing. The errors occur when the requests are blocked due to the unequal supplied origin. In plain words, the API your web application is communicating with is located in a different domain, scheme or port (origin as a whole). For instance:</p><p>Web App: http://localhost:3000/<br>API Server: http://localhost:8800/ or http://medium.com/api/</p><p>This blockage serves actually as a critical security mechanism called <strong>same-origin policy</strong>, which restricts scripts loaded by one origin to interact with resources of another origin. So, it’s definitely here to stay.</p><p>💡<strong> Any Fixes for Production?<br></strong>1. | The official way to handle such API requests is to configure your backend server to allow resources sharing for the origin of your choice. By adding Access-Control-Allow-Origin to the response header such as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44ff1bf90c67d6aa8ddc01674e749a94/href">https://medium.com/media/44ff1bf90c67d6aa8ddc01674e749a94/href</a></iframe><p>Note that you can only allow <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin">one origin</a> at a time by default. For multiple origins, the quickest way is to set the header to *, which also means accepting requests from any origins and raising security issues.</p><p>2. | Last but not least, by placing your frontend and backend in the same domain, the requests will not be considered as cross-origin. Thus, the context of CORS doesn’t even exist. However, this single domain setup requires a routing architecture on the front, which is a topic of another day.</p><p><strong>🌻 Get Connected<br></strong>If you enjoyed this content, please feel free to share, leave a comment or just say hello :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=569c411787a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>