<?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 Romain Rastel on Medium]]></title>
        <description><![CDATA[Stories by Romain Rastel on Medium]]></description>
        <link>https://medium.com/@romain-rastel?source=rss-59ebc97c6bb2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1nFnHsA7pndHqUvCrmTyhw.jpeg</url>
            <title>Stories by Romain Rastel on Medium</title>
            <link>https://medium.com/@romain-rastel?source=rss-59ebc97c6bb2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:30:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@romain-rastel/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[Everything is a Widget, but don’t put everything in a Widget!]]></title>
            <link>https://romain-rastel.medium.com/everything-is-a-widget-but-dont-put-everything-in-a-widget-32f89b5c8bdb?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/32f89b5c8bdb</guid>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 11:59:29 GMT</pubDate>
            <atom:updated>2020-06-17T07:02:46.781Z</atom:updated>
            <content:encoded><![CDATA[<h3>Everything is a Widget, but don’t put everything in one Widget!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WIsGiJKodm5Q6jEY" /><figcaption>Photo by <a href="https://unsplash.com/@torybgd?utm_source=medium&amp;utm_medium=referral">Tory Bishop</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As a Flutter developer I’m sure you heard about this popular sentence: “<strong>Everything’s a Widget</strong>” at least once in your dev life. It’s kind of the Flutter’s catch phrase and it reveals the inner force of this extraordinarily good SDK!</p><p>When we dive in the <a href="https://flutter.dev/docs/development/ui/widgets">Widget catalog</a>, we can see a lot of small widgets doing only one job such as Padding, Align, SizedBox, and so on. We create other widgets by composing them and I find this approach scalable, powerful and easy to understand.</p><p>But when I read some source code I find on the Internet or written by newly adopters, there is one thing that shock me a lot: <strong>the tendency to have huge </strong><strong>build methods, instantiating a lot of widgets</strong>! I find this difficult to read, understand and maintain.</p><p>As software developers, we have to remember that the real life of a software starts the first time it is released to its users. The source code of this software will be read and maintain by other people (including your future you), this is why it’s very important to keep our code simple, easy to read and to understand.</p><p>An example of “Everything in a Widget” can be found in the <a href="https://flutter.dev/docs/development/ui/layout/tutorial">Flutter documentation itself</a>. The goal of this tutorial is to show how to build this layout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*19boaSDDHcbNycqVj5Za_w.jpeg" /></figure><p>The final code serves its purpose: to show how to simply create the above layout. As we can see, there are even variables and methods to give semantics to the parts of this layout. This is a good point, as it makes the code easier to understand.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d02a946b989654ef1e45e16668b631ec/href">https://medium.com/media/d02a946b989654ef1e45e16668b631ec/href</a></iframe><p>In fact it could be worse. This is the typical all in one widget version of this code I dislike:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf638558124f5681ec4ab14ede64ee73/href">https://medium.com/media/bf638558124f5681ec4ab14ede64ee73/href</a></iframe><p>In the second version, we have this widget with a big build method and it’s difficult to read, understand and maintain.</p><p>Now let’s see how I would rewrite this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2db23845eb4c6ff947c9486412dca7a5/href">https://medium.com/media/2db23845eb4c6ff947c9486412dca7a5/href</a></iframe><p>Don’t you find this more readable?</p><h3>🤔 What are the benefits?</h3><p>I understand why tutorials don’t often do that: It takes more lines (100 lines in my example) and people might wonder why we are creating so many other widgets. Since tutorials aim to focus on one concept, it might be counter-productive to write them like this. But as a result, newly adopters may tend to put a big widget tree in their build methods.<br>Let’s see what are the benefits of having a unique widget for each part of the layout:</p><h4>Readability</h4><p>We create one widget for each semantic part of the layout. Each widget has therefore a smaller build method. It’s easier to read since you don’t have to scroll to get to the end of the widget.</p><h4>Understandability</h4><p>Each widget has a name matching its role, this is called <strong>semantic naming</strong>. By doing this, when we read the code it’s easier to map in our head what part of the code matches what we see on the app. I see two improvements in terms of understandability here:<br>1. When we read such a widget referenced elsewhere, we pretty much know what it does without having to see its implementation.<br>2. Before reading the build method of a widget with semantic naming, we already have in our mind, a rough idea of its contents.</p><h4>Maintainability</h4><p>If you have to replace a component, or change a part, it will be at one place only, separated from the rest of the other widgets. It will be less error-prone thanks to this practice, since the role of each widget is well defined. It will also be easier to share a part of the layout in another page within your app or even in another app.</p><h4>Performances</h4><p>All the previous reasons should be sufficient for you to adopt this way of creating Flutter apps, but there is another one advantage for doing this: We improve the performance of the app because each widget can rebuild separately from the others (and it’s not the case if we use methods to separate our layout parts). For example, let’s say we have to increment the number next to the red star when we click on it. In this version we could make _Likes a StatefulWidget and handle the increment here. When a user clicks on the star, only the _Likes widget would be rebuild. In the first version, all the MyApp widget would be rebuilt if we would have made it a StatefulWidget.</p><p>This best practice is also explained in the <a href="https://flutter.dev/docs/perf/rendering/best-practices#controlling-build-cost">Flutter documentation</a>:</p><blockquote>When setState() is called on a State, all descendent widgets will rebuild. Therefore, localize the setState() call to the part of the subtree whose UI actually needs to change. Avoid calling setState() high up in the tree if the change is contained to a small part of the tree.</blockquote><p>Another advantage is the ability to use the const keyword more often.Widgets can then be cached and re-used. As the <a href="https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html#performance-considerations">Flutter documentation</a> states:</p><blockquote>It is massively more efficient for a widget to be re-used than for a new (but identically-configured) widget to be created.</blockquote><h3>⚡️ How to be more productive?</h3><p>As you saw, by creating one widget per semantic part of the layout we write more code. We can use the stless and stful snippets provided by the <a href="https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code">Dart</a> extension in Visual Studio Code, but they don’t generate a const constructor.</p><p>For my own needs, I created new snippets, called sless and sful, so that I’m more productive than ever. If you want them in Visual Studio Code, you’ll have to follow this <a href="https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets">doc</a> and add this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b531ace12fc759f3ccd65e0a7da7be6/href">https://medium.com/media/5b531ace12fc759f3ccd65e0a7da7be6/href</a></iframe><h3>💪 How to efficiently couple this practice with state management?</h3><p>As you may know, there are <strong>a lot</strong> of state management solutions for Flutter. I will not list those that work well with this way of coding, but only the key concepts you should know to choose the state management that best suits your needs.</p><ol><li>The state needs to be accessible from one widget without having to provide it through its constructor. Otherwise you would have to pass the state through some widgets that don’t need to be aware of it.</li><li>A widget should be able to rebuild, only when the part of the state that concerns it, changes. If it’s not the case, the widget will rebuild too many times and it can hurt the app performances.</li></ol><p>In my opinion, the solutions that work the best are those based on <a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html">InheritedWidgets</a> or the same concept. You can have a look to <a href="https://github.com/rrousselGit/provider">Provider </a>+ X (X being a class able to notify state changes) or <a href="https://github.com/letsar/maestro/tree/master/packages/maestro">Maestro</a> for example.</p><h3>📕 Conclusion</h3><p>I’m convinced this is a good way to write Flutter applications, and I hope you’re convinced too. If it’s not the case, I’m interested in your opinion 😉!</p><p>From now on, remember this catch phrase: “<strong>Everything’s a widget but don’t put everything in one widget!</strong>”.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32f89b5c8bdb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maestro  Freezed]]></title>
            <link>https://romain-rastel.medium.com/maestro-freezed-ea91b08f7a2?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea91b08f7a2</guid>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 16:41:44 GMT</pubDate>
            <atom:updated>2020-06-03T09:16:47.368Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gg1MWRxtiLNnSWOX" /><figcaption>Photo by <a href="https://unsplash.com/@polarmermaid?utm_source=medium&amp;utm_medium=referral">Anne Nygård</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a <a href="https://medium.com/@lets4r/maestro-a-flutter-experiment-for-orchestrating-your-app-state-6391f56f0391">previous blog post</a> I introduced <a href="https://github.com/letsar/maestro/tree/master/packages/maestro"><strong>Maestro</strong></a> which is for now an experiment to manage you app state in a Flutter application.<br>Today I will show you how to use the power of <a href="https://github.com/rrousselGit/freezed"><strong>Freezed</strong></a> ⚡️ to create the models and the states. I will not explain in details how this library works but I will show you how easily we can create immutable objects with it!</p><p><strong>Freezed</strong> is a great library created by <a href="https://medium.com/u/c0530952c459">Remi Rousselet</a> for generating all the boilerplate code you want for immutable objects and even more!</p><p>The source code of today is heavily inspired by an <a href="https://github.com/felangel/Bloc/tree/master/examples/flutter_infinite_list">example</a> for another great app state management library called <a href="https://github.com/felangel/bloc/tree/master/packages/flutter_bloc"><strong>flutter_bloc</strong></a><strong> </strong>created by <a href="https://medium.com/u/bd78eebef416">Felix Angelov</a>.</p><p>The goal of this app is to show a list of posts which fetches new posts when the user scrolls.<br>We will focus on 4 components of this app:<br>- The model (A post)<br>- The different states of the home page<br>- The composer which represents the logic<br>- The home page and its sub widgets</p><h3>The Model</h3><p>We have only one model on this app, it’s called Post. A post contains only an id, a title and a body:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8bbad779e18940067c6d1de2846d6f6/href">https://medium.com/media/c8bbad779e18940067c6d1de2846d6f6/href</a></iframe><p>By writing this, we tell <strong>freezed</strong> to generate the boilerplate code for our immutable object, it will also use <a href="https://github.com/dart-lang/json_serializable"><strong>json_serializable</strong></a> to generate the code we need to serialize/deserialize into JSON.</p><p>We just have to execute the following command in a terminal, and all the code for comparing instances, and deserialize from JSON will be generated:</p><pre>flutter pub run build_runner build</pre><h3>The States</h3><p>Our app has three states: an empty <strong>initial</strong> <strong>state</strong>, a <strong>success</strong> <strong>state</strong> containing the posts we were able to fetch and a boolean indicating whether there is more posts or not, and a <strong>failure state </strong>in case a problem occurred while fetching the posts.</p><p>With <strong>freezed</strong> we just have to write this and the above command to generate all we want:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/630e5845296e3dca21570b6812ed285b/href">https://medium.com/media/630e5845296e3dca21570b6812ed285b/href</a></iframe><h4>The Composer</h4><p>It contains all our logic, this is the component which does the actual fetch to the server (for simplicity I did not used a Repository and a Data Source, which I would have in a real app) and change the state of our app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e990aa76ea763d059b20e0298c705785/href">https://medium.com/media/e990aa76ea763d059b20e0298c705785/href</a></iframe><p>We override the method called play to automatically fetch the first data when the composer is attached to the tree.</p><p>The fetch method will change the state according to the current one:<br>- If the current state is the initial one, then we have to load the first twenty posts and change the state accordingly<br>- If the current state is a success one, we have to load twenty more posts, and also change the state to a new success one with the posts from the previous state and the twenty more we got. We use here the copyWith method generated by <strong>freezed</strong> to copy the previous state and change only some data.<br>- If there is an error somewhere, we write a failure state.</p><h3>The Page</h3><p>We have only one page in our application. I’m used to separate all the part of my layout in different widgets so that there is one widget by semantical part of the view:</p><h4>HomePage</h4><p>This widget is responsible for creating the maestros and the components which don’t need the values of these maestros.</p><p>We create a Maestro for the HomeState, and we initialize it to the initial state. We also create a Maestro for the composer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0962363010cd2e08acdcf86371fe1ae/href">https://medium.com/media/e0962363010cd2e08acdcf86371fe1ae/href</a></iframe><h4>_Page</h4><p>This widget is responsible for listening to the changes of the HomeState and for returning the right widget according to the type of the current state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bfb46697bf44de15ce6ab28e197271b/href">https://medium.com/media/7bfb46697bf44de15ce6ab28e197271b/href</a></iframe><p>We use another feature of <strong>freezed</strong>, called map, which, you can see, is very convenient.</p><h4>_Initial</h4><p>This is the widget for the initial state. During this state we want to show the user that we’re loading data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a547bef80b9d9a1c808a8b4c10fff5de/href">https://medium.com/media/a547bef80b9d9a1c808a8b4c10fff5de/href</a></iframe><h4>_Success</h4><p>This is the main widget, which is responsible for displaying successful data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6f89ff2954745429e13bff3b2eb1213/href">https://medium.com/media/f6f89ff2954745429e13bff3b2eb1213/href</a></iframe><p>When there are no posts, we simply show the user an explanatory text, otherwise the app shows a special list which is able to fetch new data, by calling the fetch method of our composer, when the user scrolls.</p><h4>_Failure</h4><p>This is the widget to display when there is a problem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98616a984eb2c1382957621f2c8a704a/href">https://medium.com/media/98616a984eb2c1382957621f2c8a704a/href</a></iframe><h4>_Post</h4><p>This is the widget for an item of the list, responsible for displaying a single Post.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16e7abdb61c3700381a87450cd547545/href">https://medium.com/media/16e7abdb61c3700381a87450cd547545/href</a></iframe><h3>Conclusion</h3><p>As you can see, <strong>Freezed</strong> and <strong>Maestro</strong>, work very well together. If you want to dive in all the source code, you can find it <a href="https://github.com/letsar/maestro/tree/master/examples/infinite_list">here</a>. As I explained <strong>Maestro</strong> is still an experiment, but if you’re interested don’t hesitate to try it and tell me what you think about it. If you find some use cases you don’t find how to manage with <strong>Maestro</strong>, fill an issue on the repo and we will try to find a solution 😉.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea91b08f7a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maestro: A Flutter experiment for orchestrating your app state]]></title>
            <link>https://romain-rastel.medium.com/maestro-a-flutter-experiment-for-orchestrating-your-app-state-6391f56f0391?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6391f56f0391</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <category><![CDATA[flutter-state-management]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Fri, 29 May 2020 11:40:48 GMT</pubDate>
            <atom:updated>2020-05-29T11:40:48.563Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L688zxiT6zLYXqxl" /><figcaption>Photo by <a href="https://unsplash.com/@dtopkin1?utm_source=medium&amp;utm_medium=referral">Dayne Topkin</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We already have a huge bunch of <a href="https://flutter.dev/docs/development/data-and-backend/state-mgmt/options">options</a> for managing our app state in Flutter, so you could wonder why spend some time on another one?<br>The reason is simple:<strong> I love creating things, I can’t stop</strong>. Sometimes I find myself thinking about what I could make when I rock my daughter to sleep. Sometimes it can be useful, other times it’s not but I still have learned something in the end.<br>Today I want to tell you the story of <a href="https://github.com/letsar/maestro"><strong>Maestro</strong></a> and you will decide whether it can help you or not. If it does, it will be music to my ears 🎵!</p><p>All of this started after using two packages I like a lot: <a href="https://github.com/rrousselGit/provider"><strong>Provider</strong></a> and <a href="https://github.com/rrousselGit/freezed"><strong>Freezed</strong></a> (kudos to <a href="https://medium.com/u/c0530952c459">Remi Rousselet</a> if you’re reading this 😉). I use <strong>Freezed</strong> to generate the classes of the data coming from the APIs and <strong>Provider</strong> + ChangeNotifier for managing my app state. It’s great but the mutability of ChangeNotifier bother me. So I started to think about a clear separation between the app state and the components that perform actions on it, but I didn’t want to end up with something too verbose either. One night before going to bed, I opened VSCode created a new Flutter project and started to prototype something. In only 300 lines of code I ended up with something I liked and found pleasant to use. I like to share, so instead of keeping this to myself I decided to create a new package I called <strong>Maestro</strong> and here we are 😅!</p><h3>🏗 A global app architecture</h3><p>Before going further on the package let me show you how I separate an application in three layers, each one built upon the previous one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*RevbNaskDmtDje6L1vJ-ZQ.png" /><figcaption>Application layers</figcaption></figure><p>Many of you may already be familiar with this kind of separation, if not this is a little explanation of each layer:</p><h4>Data layer</h4><p>This layer is responsible for fetching and manipulate the data from one or more sources.</p><p>There are two main components in this block:</p><p><strong>Data sources</strong> which are providers of raw data (like a web service client)<br><strong>Repositories</strong> that use one or more data sources to provide data understandable by the rest of the app (we can call them <strong>models</strong>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c22107a00c43796fe455248c8150ad8/href">https://medium.com/media/8c22107a00c43796fe455248c8150ad8/href</a></iframe><h4>Logic Layer</h4><p>This layer is the glue between the UI layer and the Data layer. It takes orders given by the user input and then communicates with repositories in order to modify a part of the app state the UI will display.</p><h4>UI Layer</h4><p>This layer is responsible for providing something to the user so that they can interact with the data the app produces and consumes.</p><h3>🗂 App state management with Maestro</h3><p>In Flutter, you structure your app with widgets. A widget is a declaration of something in your app, it can be a visual element, a way to access your data, just a log writer and all that jazz. Remember: <strong>Everything is a widget!</strong> Therefore in my vision, the data sources, the repositories and the objects in the logic layer should be encapsulated in widgets. In <strong>Maestro</strong> the widget responsible for storing these objects is called… Maestro!</p><p>Let’s say you want to expose the current user to a subtree, with <strong>Maestro</strong> you’ll just have to write this:</p><pre>Maestro(<br>  User(),<br>  child: SubTree(),<br>)</pre><p>If you want to expose the current user and the current cart, you can nest the Maestro widgets:</p><pre>Maestro(<br>  User(),<br>  child: Maestro(<br>    Cart(),<br>    child: SubTree(),<br>  ),<br>)</pre><p>If you have a lot of elements to expose, it’s more convenient to use another widget called Maestros. This widget takes a list as parameter and recreate a nested tree of Maestro widgets:</p><pre>Maestros(<br>  [<br>    Maestro(User()),<br>    Maestro(Cart()),<br>  ],<br>  child: SubTree(),<br>)</pre><p>In any widget built by SubTree you will be able to read and write the current user and the current cart. For example you may use Maestro.read&lt;Cart&gt;(context); to read the current cart and Maestro.write(context, User(firstName: ‘Anakin’, lastName: ‘Skywalker’)); to write the current user. Like <strong>Provider</strong>, you need to provide a BuildContext to these methods.</p><p>With <strong>Maestro</strong>, the logic layer is composed essentially by objects I called Composers. The composers know how to read and write the models declared before them.</p><p>Let’s see a simple Composer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c7e1a07a4ba6d282ece2cf29a18b3d8/href">https://medium.com/media/7c7e1a07a4ba6d282ece2cf29a18b3d8/href</a></iframe><p>A composer is a class which apply the mixin called Composer. By applying this mixin, you’ll get access to the methods read and write that you can use to get and set objects exposed before the composer. As you can see, you don’t need to pass any BuildContext here, <strong>Maestro</strong> will do that for you 😉.</p><p>This composer depends on a UserRepository and gets it with a call to the read method.<br>In the authenticate method we delegate the authentication to the repository and our job is to handle errors (we don’t do it in the example) and to update the app state with a call to the write method.</p><p>These are the few components you need to know to use <strong>Maestro</strong> for managing the app state.</p><h3>🎼 Let’s play with Maestro!</h3><p>This was the theory, now I will show you how to do the Flutter Counter app with <strong>Maestro</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb7966260bdd794183ac15190808b68c/href">https://medium.com/media/cb7966260bdd794183ac15190808b68c/href</a></iframe><p>We encapsulate the MaterialApp with a Maestros widget in which we expose the Counter with an initial value of 0 and the CounterComposer. By doing so, the entire app can access the current counter and the composer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/161a08d439b37e8fedea2ef75312744a/href">https://medium.com/media/161a08d439b37e8fedea2ef75312744a/href</a></iframe><p>In the _HomePage you can see a specific widget _CounterText (we will see it in a few seconds) and the call to the increment method of the composer when the user taps on the floating action button. You can see that instead of Maestro.read&lt;CounterComposer&gt;(context) we used an extension method on BuildContext provided by <strong>Maestro</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48b9473867086fe8c098bc627e3758cd/href">https://medium.com/media/48b9473867086fe8c098bc627e3758cd/href</a></iframe><p>This widget use another method of <strong>Maestro</strong> called listen. This method is able to read the value of the nearest Maestro ancestor of the given type and tells the Flutter to rebuild this widget when the value changes.</p><p>We have other options than creating a dedicated widget in order to listen for changes but this is how I like to do.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae9c6d133a2ce1b5b52966743a06c13c/href">https://medium.com/media/ae9c6d133a2ce1b5b52966743a06c13c/href</a></iframe><p>Here we have the implementation of the model called Counter, and its composer called CounterComposer.<br>The model should be immutable, for doing it, we use another great package called <a href="https://github.com/felangel/equatable"><strong>Equatable</strong></a>. You can also use <a href="https://github.com/rrousselGit/freezed"><strong>Freezed</strong></a> to achieve the same result.</p><h3>🎉 Conclusion</h3><p><strong>Maestro</strong> is for now an experiment. I don’t know yet if there are cases where it does not fit, but I find it very promising. The Fluture will tell 😜!</p><p>You can download it on <a href="https://pub.dartlang.org/packages/maestro">pub.dev</a>, the source code is on <a href="https://github.com/letsar/maestro">this GitHub repository</a> along with more information on the package.</p><p>Let me know what you think about it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6391f56f0391" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gap: A simple example to create your own RenderObject]]></title>
            <link>https://romain-rastel.medium.com/gap-a-simple-example-to-create-your-own-renderobject-88eacca2a4?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/88eacca2a4</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <category><![CDATA[flutter-ui]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Wed, 06 May 2020 22:03:44 GMT</pubDate>
            <atom:updated>2020-11-19T21:02:26.776Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uY7Deas34pmpVgEf9gx9JA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alexradelich?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alex Radelich</a> on <a href="https://unsplash.com/s/photos/gap?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I love how Flutter is designed. The combination of immutables objects, the Widgets and mutables ones called RenderObjects is very powerful although not super intuitive for beginners.</p><p>We can do a lot just by composing widgets with StatelessWidgets and StatefulWidgets, but we cannot do everything, or not in an efficient way. That’s why you need to understand how to create your own RenderObject when it’s necessary.<br>In this post I will not explain what is the difference between a Widget and a RenderObject but I will show you how I created a simple RenderObject in order for you to do the same when you need it. If you want to know more about Widgets, Elements and RenderObjects before reading this, I advise you to read this excellent <a href="https://medium.com/flutter-community/flutter-what-are-widgets-renderobjects-and-elements-630a57d05208">article</a> of <a href="https://medium.com/u/aca5101434f">Norbert</a>.</p><h4>Some context</h4><p>Last week I was fed up with adding gaps between my widgets in some Columns and Rows. I use SizedBoxes for that, but we need to specify the width or the height depending on whether we are inside a Column or a Row. I don’t find this very productive so I ended up creating my own RenderObject for that. I called the underlaying widget <strong>Gap</strong>!</p><p><em>Fun fact </em>🙃<em>: Three days after I started to work on this widget, I saw this tweet in my TL:</em></p><h3>r/FlutterDev on Twitter</h3><p>Adding a Gap to Flutter - an alternative to all those SizedBox widgets we like to use https://t.co/jW7yvynuOz https://t.co/OmUM8bqXy2 @flutterdev #flutterdev</p><p><em>I was very amused by the timing! I read the </em><a href="https://medium.com/@eibaan_54644/adding-a-gap-to-flutter-e4277715b6a5"><em>article</em></a><em> of </em><a href="https://medium.com/u/38d0d8a11fad"><em>Stefan Matthias Aust</em></a><em> where he explains how he created a Gap widget with pretty much the same implementation than mine. I will try to make a different analyze </em>😉<em>.</em></p><p>The following code is the prototype of my widget. I think it’s simple enough to explain how to easily create a new Widget and its associatedRenderObject.</p><h4>The RenderObject</h4><p>The RenderObject is the piece of code responsible of computing the layout and paint it to the screen. I start with the render layer, because it’s completely independent from the widget layer.</p><p>There are (for the moment) two kinds of RenderObjects: RenderBoxes and RenderSlivers.</p><p>To know which of them you must extend, it’s pretty simple: If your component needs to be aware of the scroll constraints (for a scroll effect or because it needs to be in a scroll container), the RenderObject must extend RenderSliver, otherwise it must extend RenderBox. <br><strong>Pro tip </strong>💪: In most of the cases you want a RenderBox 😉.</p><p>In our context, the parent of our widget will be a Flex widget, which only understands RenderBoxes, so we don’t even have a choice, our RenderObject must extend RenderBox.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/302ead319e74e8b83b4eecac2fdbdfa8/href">https://medium.com/media/302ead319e74e8b83b4eecac2fdbdfa8/href</a></iframe><p>Our RenderGap have only one property called mainAxisExtent. This is the extent of our gap in the direction of its parent: if the widget’s parent is a Column, then this is the height and if it’s a Row then it represents the width.</p><p>We set the mainAxisExtent at the creation and we can update it with the associated setter. This setter makes a call to markNeedsLayout, which tells the framework that it needs to re-run the performLayout method before rendering the frame.</p><p>But what it this performLayout method? As its name indicates, it’s the method responsible for computing the layout of our component. In the case of a RenderBox, it does so by setting its size property. <br>In this method we know that the constraints are already computed and we can use them to determine our size.</p><p>As I mentioned before, the parent of our widget is either a Column or a Row. Both of them extend Flex and the associated RenderObject of a Flex is a RenderFlex.<br>We want our RenderGap to only work if the parent is a RenderFlex, so this is the first thing we do in the performLayout method. Then we look at the direction of its parent to know how to compute the size. If it’s Axis.horizontal, then we are in a Row, and we want the size to have a width of the value of our mainAxisExtent, and a height of 0. But we need to constrain our requested size with the constraints imposed by the parent. If the Gap is the direct child of a Row, it will not be constrained horizontally (maxWidth will have an infinite value), but if there is a Flexible between the Row and the Gap, then the maxWidth constrain will have a finite value, and thus we have to take into account this constraint. This is the meaning of constraints.constrain(Size(mainAxisExtent, 0));.</p><p>Oh wait, didn’t I tell you that the parent of a Gap must be a Flex widget before? Then how a Flexible can be between a Row and a Gap??<br>Well, I wasn’t totally precise. I should have written that the direct parent of a RenderGap must be a RenderFlex but the direct parent of a Gap is not necessarily a Flex widget.<br>This can be possible because there are widgets which are not visible and thus don’t need to be tied up with a RenderObject.<br>This is the case for example with the Flexible widget. If we look at the code we can see that Flexible extends ParentDataWidget&lt;Flex&gt; and it does not create any RenderObject.</p><p>This is all for the RenderObject. We don’t even need to override the paint method because we don’t want to paint anything, we just want to tell the framework that our component takes some place and that’s it.</p><p>To fully respect the RenderObject contract we should also implement other methods such as computeMinIntrinsicWidth, but we will stop here for now.</p><h4>The Widget</h4><p>We already have our RenderObject, now we need to create a specific widget in order to add it to the build method of another widget. Our widget must extend RenderObjectWidget since it’s associated with a RenderGap. If we don’t want to manage the children ourselves, we can use one of the three out-of-the-box classes depending on the number of children the widget can take:</p><ul><li>For zero children the widget must extends LeafRenderObjectWidget.</li><li>For one child the widget must extends SingleChildRenderObjectWidget.</li><li>For two or more children the widget must extends MultiChildRenderObjectWidget.</li></ul><p>In our case, the Gap widget has no children, that’s why Gap extends LeafRenderObjectWidget.</p><p>There are two methods to override when we extend RenderObjectWidget:</p><ul><li>createRenderObject: this method is called the first time the widget is added to the widget tree. It must return a new instance of the associatedRenderObject populated with the properties set or computed from the widget.</li><li>updateRenderObject: this method is called every time the widget is updated. It must update the previous instance of the associated RenderObject with the properties set or computed from the widget.</li></ul><p>In our case, the code is very simple since we have only one property to pass to the RenderObject.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42376752c4deae03b3146b0b134ee7e4/href">https://medium.com/media/42376752c4deae03b3146b0b134ee7e4/href</a></iframe><p>Our RenderGap needs a mainAxisExtent, so our widget also needs to have a mainAxisExtent property in order to be set within a build method.</p><p>And that’s it, this is the only things we have to do for the widget layer.</p><h4>Example</h4><p>This is a small example of this widget for testing it. Enjoy!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fletsar%2Fembed%2Fpreview%2FYzyeyod%3Fheight%3D600%26slug-hash%3DYzyeyod%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fletsar%2Fpen%2FYzyeyod&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/16d9e34e1af3de9f1d2bd2f0bb970b58/href">https://medium.com/media/16d9e34e1af3de9f1d2bd2f0bb970b58/href</a></iframe><h4>Conclusion</h4><p>This was a very simple widget which uses directly a RenderObject behind the scene. I made it a package downloadable on <a href="https://pub.dev/packages/gap">pub.dev</a> if you find it useful. The code of the package is a little more complex because I added some options to allow the Gap to set its own crossAxisExtent and a color. Feel free to look at the full <a href="https://github.com/letsar/gap/blob/master/lib/gap.dart">code</a> on GitHub.</p><p>The rendering world is beautiful, but it can be difficult to understand, you’ll have to start with simple use cases and add complexity step by step.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88eacca2a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Animating Flutter widgets with Sidekicks]]></title>
            <link>https://medium.com/flutter-community/animating-flutter-widgets-with-sidekicks-8c37e2a92d7f?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c37e2a92d7f</guid>
            <category><![CDATA[dartlang]]></category>
            <category><![CDATA[widget]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Sun, 04 Nov 2018 12:43:54 GMT</pubDate>
            <atom:updated>2018-11-04T14:18:54.237Z</atom:updated>
            <content:encoded><![CDATA[<h4>Every Hero needs a Sidekick</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Iela0YR5jhzM14VrqbAlw.png" /></figure><p>Animations can turn a great app into an astounding and unforgettable one. They are a big part of the user experience and without them, an application doesn’t really look attractive.</p><p>Thanks to <strong>Flutter</strong>, it’s easier than ever to animate the UI of your app in order to reinforce the user’s action. <br>For example, it’s pretty straightforward to create a transition between two screens by animating a common element with the <a href="https://docs.flutter.io/flutter/widgets/Hero-class.html">Hero</a> widget:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*g_P-z_N0Y5XLcScmVMu5UA.gif" /><figcaption>Hero transition from Reflectly app</figcaption></figure><p>Heroes are really great and helpful but I wanted to create such a transition within the same screen like this one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*xAfuWgqgAHzRBKKfzzs4VQ.gif" /><figcaption>Workout app concept by <a href="https://dribbble.com/Vitwai">Vitaly Rubtsov</a> on <a href="https://dribbble.com/shots/2146618-Add-Exercises-Interaction">Dribbble</a></figcaption></figure><p>But some heroes have a colossal ego and prefer big adventures, so they leave these kinds of missions to their <strong>sidekicks </strong>😃.</p><h3>🚀 From Hero to Sidekick</h3><p>Heroes can’t do these animations because they rely on route transitions to start them. But as Flutter is open source, I looked at the <strong>Hero</strong> source code to see how it was implemented and how I could reproduce the same kind of effect.</p><p>It turned out that<strong> a lot of code</strong> could be reused to do what I wanted to create.<br>So I implemented a new widget called <strong>Sidekick </strong>which is heavily based on the Hero widget code.</p><p><strong>Before reading further, I strongly suggest you learn how heroes work if you don’t know it already 😉.</strong></p><p><strong>You can find useful information </strong><a href="https://flutter.io/animations/hero-animations/"><strong>here</strong></a><strong> and </strong><a href="https://medium.com/flutter-community/flutter-heroes-and-villains-bringing-balance-to-the-flutterverse-2e900222de41"><strong>there</strong></a><strong>.</strong></p><p>The first difference you’ll notice between <strong>Hero </strong>and <strong>Sidekick </strong>is that a <strong>Sidekick</strong> has a targetTag property. We don’t have to specify it for Hero because the source and target heroes are implicitly linked by the route transition. But in the case of Sidekicks, the animation is not triggered automatically, and we need a way to link the source and the destination widgets.</p><p>The targetTag property must only be set on the source Sidekick, it’s how the system will distinguish the source from the destination.</p><p>To start the animation I created a SidekickController which is an Animation&lt;double&gt; so that you can listen to it.<br>To start an animation you will use one of the following method: move, moveToTarget or moveToSource.</p><p>The move method takes a direction and calls moveToTarget if the direction is the target otherwise it calls moveToSource.</p><h4>🔎 Example code</h4><p>It’s always simpler to understand with an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*kBqQyIRLJ9rzNr7eO0A6dQ.gif" /><figcaption>Simple Sidekick animation</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8455f5a57116e2a385ef71141ae29d4/href">https://medium.com/media/e8455f5a57116e2a385ef71141ae29d4/href</a></iframe><h3>⚡️ The true power of the Sidekick</h3><p>The previous example was really simple, and maybe you are wondering this:</p><blockquote>How can we do something like the workout app concept? It will involve a lot of <strong>Sidekicks</strong>, a lot of <strong>SidekickControllers</strong>, some state management and maybe headaches, no? <em>🤕</em>.</blockquote><p>Well, I’ve been there, and Sidekicks are better when they create a team and work together!</p><h4>💪 The SidekickTeamBuilder</h4><p>This is the widget you’ll need when you’ll want to create this kind of UI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*X5ti_FbNuEQj2MJUuACAKQ.gif" /></figure><p>This widget will take care of all the <strong>Sidekicks</strong>, the <strong>SidekickControllers</strong>, and even the state management implied by having two lists of elements and transferring elements from one list to the other.</p><p>Let’s have a look at the code used to create this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ae8e205623a82d98f36c0debb63d6d7/href">https://medium.com/media/8ae8e205623a82d98f36c0debb63d6d7/href</a></iframe><p>This code is pretty long, but it would have been even longer without the <strong>SidekickTeamBuilder </strong>😄.</p><h3>📦 The package</h3><p>These widgets are available under the <a href="https://pub.dartlang.org/packages/flutter_sidekick">flutter_sidekick</a> package. It’s open source, and you can find the source code <a href="https://github.com/letsar/flutter_sidekick">here</a>.</p><p>If this package is helping you, you can support me by ⭐️the <a href="https://github.com/letsar/flutter_sidekick">repo</a>, or 👏 this story.</p><p>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>:</p><ul><li><a href="https://twitter.com/lets4r">JavaScript is not available.</a></li><li><a href="https://twitter.com/FlutterComm">JavaScript is not available.</a></li></ul><p>Let me know if you built an app with this package 😃.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c37e2a92d7f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/animating-flutter-widgets-with-sidekicks-8c37e2a92d7f">Animating Flutter widgets with Sidekicks</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dart: What are mixins?]]></title>
            <link>https://medium.com/flutter-community/dart-what-are-mixins-3a72344011f3?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a72344011f3</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dartlang]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Sun, 16 Sep 2018 10:19:52 GMT</pubDate>
            <atom:updated>2023-08-04T08:38:08.899Z</atom:updated>
            <content:encoded><![CDATA[<h4>It’s a kind of magic ✨</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D3_l3AwiVO1Kl9OAJiQM5A.png" /><figcaption>Dart mixer (modified from <a href="https://www.shareicon.net/furniture-and-household-kitchen-pack-tools-and-utensils-kitchenware-mixing-mixer-807139">shareicon</a>)</figcaption></figure><p>When I started to learn <a href="https://www.dartlang.org/">Dart</a>, the concept of mixins was new for me.<br>I come from the C# world, where it does not exist (at least prior to C# 8.0, to the best of my knowledge).<br>At first, I found this concept somewhat difficult to understand, until I realized how powerful it was.</p><h3>🤔 Why do we need mixins?</h3><p>If languages such as C# do not have mixins, it probably isn’t that useful, is it?</p><p>Let’s have a look at the following class inheritance diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1We9auWtIuVsOH30d_pbpA.png" /></figure><p>We have here a superclass called <strong>Animal</strong> which<strong> </strong>has three subclasses (<strong>Mammal</strong>, <strong>Bird</strong>, and <strong>Fish</strong>). At the bottom, we have concrete classes.<br>The little squares represent behavior. For example, the blue square indicates that an instance of a class with this behavior can swim.</p><p>Some animals share common behavior: A cat and a dove can both walk, but the cat cannot fly (except for <a href="https://www.youtube.com/watch?v=QH2-TGUlwu4">Nyan Cat</a> 😀).<br>These kinds of behavior are orthogonal to this classification, so we cannot implement these behavior in the superclasses.</p><p>If a class could have more than one superclass, it would be easy, we could create three other classes: <strong>Walker</strong>, <strong>Swimmer</strong>, <strong>Flyer</strong>. After that, we would just have to inherit <strong>Dove</strong> and <strong>Cat</strong> from the <strong>Walker</strong> class. But in Dart, every class (except for Object) has exactly <strong>one </strong>superclass.</p><p>Instead of inheriting from the <strong>Walker</strong> class, we could implement it, as it if was an interface, but we should have to implement the behavior in multiple classes, so it’s not a good solution.</p><p>We need a way of reusing a class’s code in multiple class hierarchies.<br>You know what? Mixins are exactly that:</p><blockquote>Mixins are a way of reusing a class’s code in multiple class hierarchies.</blockquote><blockquote><strong>— </strong><a href="https://www.dartlang.org/guides/language/language-tour#adding-features-to-a-class-mixins"><strong>dartlang.org</strong></a></blockquote><p>Described like that, it sounds easy 😁.</p><h3>🔒 Constraints (as of Dart 3.0)</h3><p>The mixin feature comes with a few restrictions (from <a href="https://www.dartlang.org/articles/language/mixins"><strong>dartlang.org</strong></a>):</p><ul><li>Mixins cannot declare a constructor</li><li>Mixins can’t extend classes or other mixins</li></ul><h3>📝 Syntax</h3><p>We saw how mixins can be useful, let’s see how to create and use them.</p><p>To create a mixin we have to declare it using the mixin modifier:</p><pre>mixin Walker {<br>  void walk() {<br>    print(&quot;I&#39;m walking&quot;);<br>  }<br>}</pre><p>To use a mixin, use the with keyword followed by one or more mixin name:</p><pre>class Cat extends Mammal with Walker {}<br><br>class Dove extends Bird with Walker, Flyer {}</pre><p>Defining the Walker mixin on the Cat class, allows us to call the walk method but not the fly method (defined in Flyer).</p><pre>main(List&lt;String&gt; arguments) {<br>  Cat cat = Cat();<br>  Dove dove = Dove();<br><br>  // A cat can walk.<br>  cat.walk();<br><br>  // A dove can walk and fly.<br>  dove.walk();<br>  dove.fly();<br><br>  // A normal cat cannot fly.<br>  // cat.fly(); // Uncommenting this does not compile.<br>}</pre><h3>🔎 Details</h3><p>I told you I found this concept somewhat difficult to understand, but so far it’s not so difficult, is it?<br>Well, can you tell what the output of the following program is 😵?</p><pre>mixin A {<br>  String getMessage() =&gt; &#39;A&#39;;<br>}<br><br>mixin B {<br>  String getMessage() =&gt; &#39;B&#39;;<br>}<br><br>class P {<br>  String getMessage() =&gt; &#39;P&#39;;<br>}<br><br>class AB extends P with A, B {}<br><br>class BA extends P with B, A {}<br><br>void main() {<br>  String result = &#39;&#39;;<br><br>  final ab = AB();<br>  result += ab.getMessage();<br><br>  final ba = BA();<br>  result += ba.getMessage();<br><br>  print(result);<br>}</pre><p>Both, <strong>AB</strong> and <strong>BA</strong> classes extend the <strong>P</strong> class and apply <strong>A</strong> and <strong>B</strong> mixins but in a different order. Both <strong>A</strong>, <strong>B</strong> mixins and the <strong>P</strong> classe have a method called <strong>getMessage</strong>.</p><p>First, we call the <strong>getMessage</strong> method of the <strong>AB</strong> class, then the <strong>getMessage </strong>method of the <strong>BA</strong> class.</p><p><strong>So, what do you think the resulting output will be?<br></strong>I’m giving you five propositions:</p><p><strong>A.</strong> It does not compile<br><strong>B.</strong> BA<br><strong>C.</strong> AB<br><strong>D.</strong> BAAB<br><strong>E.</strong> ABBA</p><p>🥁🥁🥁The answer is the proposition <strong>B!</strong> The program prints BA.</p><p>I think you guessed that the order in which the mixins are declared is very important.</p><p>Why? How is it working?</p><h3>✨ Linearization</h3><p>When you apply a mixin to a class, keep in mind this:</p><blockquote>Mixins in Dart work by creating a new class that layers the implementation of the mixin on top of a superclass to create a new class — it is not “on the side” but “on top” of the superclass, so there is no ambiguity in how to resolve lookups.</blockquote><blockquote><strong>— </strong><a href="https://stackoverflow.com/users/2156621/lrn"><strong>Lasse R. H. Nielsen</strong></a><strong> on </strong><a href="https://stackoverflow.com/a/45903671"><strong>StackOverflow</strong></a><strong>.</strong></blockquote><p>In fact, the code</p><pre>class AB extends P with A, B {}<br><br>class BA extends P with B, A {}</pre><p>is semantically equivalent to</p><pre>class PA = P with A;<br>class PAB = PA with B;<br><br>class AB extends PAB {}<br><br>class PB = P with B;<br>class PBA = PB with A;<br><br>class BA extends PBA {}</pre><p>The final inheritance diagram can be represented like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*sayzomK1biB7iy5W8hrUSQ.png" /></figure><p>New classes are created between <strong>AB</strong> and <strong>P. </strong>These new classes are a mix-in between the superclass <strong>P</strong> and the mixins <strong>A</strong> and <strong>B</strong>.</p><p>As you can see, there is no multiple inheritance in there!</p><blockquote>Mixins is not a way to get multiple inheritance in the classical sense. Mixins is a way to abstract and reuse a family of operations and state. It is similar to the reuse you get from extending a class, but it is compatible with single-inheritance because it is linear.</blockquote><blockquote><strong>— </strong><a href="https://stackoverflow.com/users/2156621/lrn"><strong>Lasse R. H. Nielsen</strong></a><strong> on </strong><a href="https://stackoverflow.com/a/45903671"><strong>StackOverflow</strong></a><strong>.</strong></blockquote><p>One important thing to remember is that the order in which mixins are declared represents the inheritance chain, from the top superclass to the bottom one.</p><h3>📄 Types</h3><blockquote>What is the type of a mixin application instance? In general, it is a subtype of its superclass, and also a subtype of the type denoted by the mixin name itself, that is, the type of the original class.</blockquote><blockquote><a href="https://www.dartlang.org/articles/language/mixins#types"><strong>— dartlang.org</strong></a></blockquote><p>So it means that this program</p><pre>mixin A {<br>  String getMessage() =&gt; &#39;A&#39;;<br>}<br><br>mixin B {<br>  String getMessage() =&gt; &#39;B&#39;;<br>}<br><br>class P {<br>  String getMessage() =&gt; &#39;P&#39;;<br>}<br><br>class AB extends P with A, B {}<br><br>class BA extends P with B, A {}<br><br>void main() {<br>  AB ab = AB();<br>  print(ab is P);<br>  print(ab is A);<br>  print(ab is B);<br><br>  BA ba = BA();<br>  print(ba is P);<br>  print(ba is A);<br>  print(ba is B);<br>}</pre><p>will print six lines with true in the console.</p><h4>Detailed explanation</h4><p>Lasse R. H. Nielsen gave me a great explanation:</p><blockquote>Since each mixin application creates a new <em>class</em>, it also creates a new <em>interface</em> (because all Dart classes also define interfaces). As described, the new class extends the superclass and includes copies of the mixin class members, but it also <em>implements</em> the mixin class interface.</blockquote><blockquote>In most cases, there is no way to refer to that mixin-application class or its interface; the class for Super with Mixin is just an anonymous superclass of the class declared like class C extends Super with Mixin {}. If you name a mixin application like class CSuper = Super with Mixin {}, then you <em>can</em> refer to the mixin application class and its interface, and it will be a sub-type of both Super and Mixin.</blockquote><blockquote><strong>— Lasse R. H. Nielsen</strong></blockquote><h3>🎖 Specialized mixins</h3><p>What if you would want to be able to invoke a method defined in a superclass within a mixin?</p><p>This is possible with the on clause:</p><pre>abstract class Super {<br>  void method() {<br>    print(&quot;Super&quot;);<br>  }<br>}<br><br>class MySuper implements Super {<br>  @override<br>  void method() {<br>    print(&quot;MySuper&quot;);<br>  }<br>}<br><br>mixin Mixin on Super {<br>  @override<br>  void method() {<br>    super.method();<br>    print(&quot;Sub&quot;);<br>  }<br>}<br><br>class Client extends MySuper with Mixin {}<br><br>void main() {<br>  Client().method();<br>}</pre><p>The mixin declaration mixin Mixin on Super indicates a superclass constraint on Super. It means that in order to apply this mixin to a class, this class must extend or implementSuper because the mixin uses a feature provided by Super.</p><p>The output of this program would be:</p><blockquote>MySuper<br>Sub</blockquote><p>If you wonder why, keep in mind how mixins are linearized:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*TD6e15Gd_ZdBMD6vjjz5Jg.png" /></figure><p>In fact, the call to super.method() on <strong>line 17</strong> actually calls the method declared on <strong>line 9</strong>.</p><h3>🙄 When to use mixins?</h3><p>Mixins are very helpful when we want to share a behavior across multiple classes that don’t share the same class hierarchy, or when it doesn’t make sense to implement such a behavior in a superclass.</p><p>It’s typically the case for serialization (Take a look at <a href="https://github.com/Jaguar-dart/jaguar_serializer">jaguar_serializer</a> for example) or persistence. But you can also use mixins to provide some utility functions (like the <a href="https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/sliver.dart">RenderSliverHelpers</a> in Flutter).</p><p>Take a time to play with this feature, and I’m sure you’ll find new use cases 😉. Don’t restrict yourself to stateless mixins, you can absolutely store variables and use them!</p><h3>🐬Complete Animal example</h3><p>You can find below, the complete Animals example with which I introduced the mixins:</p><pre>abstract class Animal {}<br><br>abstract class Mammal extends Animal {}<br><br>abstract class Bird extends Animal {}<br><br>abstract class Fish extends Animal {}<br><br>mixin Walker {<br>  void walk() {<br>    print(&quot;I&#39;m walking&quot;);<br>  }<br>}<br><br>mixin Swimmer {<br>  void swim() {<br>    print(&quot;I&#39;m swimming&quot;);<br>  }<br>}<br><br>mixin Flyer {<br>  void fly() {<br>    print(&quot;I&#39;m flying&quot;);<br>  }<br>}<br><br>class Dolphin extends Mammal with Swimmer {}<br><br>class Bat extends Mammal with Walker, Flyer {}<br><br>class Cat extends Mammal with Walker {}<br><br>class Dove extends Bird with Walker, Flyer {}<br><br>class Duck extends Bird with Walker, Swimmer, Flyer {}<br><br>class Shark extends Fish with Swimmer {}<br><br>class FlyingFish extends Fish with Swimmer, Flyer {}</pre><p>We can see below how these mixins are linearized:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ntLhyV-KPs_mLNTXjm61IQ.png" /></figure><h3>📗 Conclusion</h3><p>We saw that mixins are a powerful concept that allows you to reuse code across multiple class hierarchies.<br>Flutter uses this feature a lot, and I found it important to understand it better, that’s why I shared my comprehension with you.</p><p>If you have any questions, feel free to post them 👇.</p><p>Any feedback is welcome 😃.</p><p>If you enjoyed this story, you can support me by 👏 this.<br>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>.</p><p>Thanks to <a href="https://medium.com/u/42f7c0123db5">Jeroen Meijer</a> for the proofreading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a72344011f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/dart-what-are-mixins-3a72344011f3">Dart: What are mixins?</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Announcing kiwi]]></title>
            <link>https://medium.com/flutter-community/announcing-kiwi-52ddb3935e6d?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/52ddb3935e6d</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[ioc]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Sun, 09 Sep 2018 15:43:21 GMT</pubDate>
            <atom:updated>2018-09-12T08:33:46.401Z</atom:updated>
            <content:encoded><![CDATA[<h4>Dependency injection for Dart and Flutter</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*M7mOrtkfYPoAXw9iDBIpXw.png" /></figure><p>Today, I’m very pleased to announce <a href="https://github.com/letsar/kiwi"><strong>kiwi</strong></a>, a dependency injection library for <a href="https://www.dartlang.org/">Dart</a> and <a href="https://flutter.io/">Flutter</a> which aims to be simple and fast.</p><h3>Why?</h3><p>Even if I’m writing a small application, I tend to separate the logic from the UI. Because projects grow, I think it’s a good practice to do this if the project is not a proof of concept.</p><p>I like to have small services with one responsibility. It’s easier to test, to maintain, to read, to understand. A problem with this principle is that you end up with services having a lot of dependencies. Creating such a service can be tedious if you have to instantiate all the dependencies before calling the constructor. In most of the cases, you can rely on IoC libraries to do this for you and most of them use reflection at runtime to create an object.</p><p>But since Flutter cannot run code that depends on dart:mirrors, we cannot use Dart libraries that lies on reflection to create objects.</p><p>So I looked for IoC libraries that we can use in Flutter. Here are the 3 most promising libraries (for me) and why I didn’t use them:</p><ul><li><a href="https://github.com/google/inject.dart">inject</a>: The package is offered as-is, and I found it too complicated for my needs (I never used Dagger).</li><li><a href="https://github.com/fluttercommunity/get_it">get_it</a>: It’s a really good Service Locator library, but it does not provide a generator in case we want to use it for constructor injection.</li><li><a href="https://github.com/aloisdeniel/dioc">dioc</a>: This is the one that almost fit my needs, but it didn’t support Dart 2 at the time I started my own package. Some other features I wanted were not implemented either.</li></ul><p>After this little research, I found the need to create my own library (which is heavily inspired by <em>dioc </em>and <em>get_it</em>) and I ended up sharing it with you now 🙂.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*54iUxdSD5SpfbsdJsj8spQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/1rafrfyrsZw?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Lesly Juarez</a> on <a href="https://unsplash.com/search/photos/kiwi?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>What is kiwi?</h3><p>At its very core, <strong>kiwi</strong> is simply a container of instances and factories you can name. It’s coupled with a powerful generator to allow the developer to write less code.</p><p>The generator provided by <strong>kiwi</strong> only supports constructor injection in order to minimize the code that depends on it.</p><p>Another important thing is that <strong>kiwi</strong> only works with Dart ≥ 2.0.0.</p><h3>Library usage</h3><p>The core of <strong>kiwi </strong>is the Container class. This is where all your instances and factories are stored.</p><p>The Container is implemented as a singleton. You can access the single instance like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1635d07921df105577ceac8af1ff4694/href">https://medium.com/media/1635d07921df105577ceac8af1ff4694/href</a></iframe><p><strong>Note:</strong> <em>I promise you, even if this is looking like a constructor, you will always end up with the same instance </em>😉<em>.</em></p><p>The container can store <strong>instances</strong>, <strong>factories, </strong>and <strong>singletons</strong>. What I name singleton here, is a factory that will be called only once, after which the same instance will be provided every time.</p><p>Instances can be registered like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/338dd5bab389aeb0599cc04e1c5efe3d/href">https://medium.com/media/338dd5bab389aeb0599cc04e1c5efe3d/href</a></iframe><p>Factories like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f70354c617022c9b2c10b8a9853c5d4d/href">https://medium.com/media/f70354c617022c9b2c10b8a9853c5d4d/href</a></iframe><p>And for singletons, you just have to change <strong>Factory </strong>by <strong>Singleton</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac9dee92de290b21a978d7d65275ad25/href">https://medium.com/media/ac9dee92de290b21a978d7d65275ad25/href</a></iframe><p><strong>Note</strong>: <em>The c parameter is the instance of the Container, we will saw later how it can be useful.</em></p><p>Then, you can get the value registered under a specified type like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42cff86839f5a06e85e484a284f41448/href">https://medium.com/media/42cff86839f5a06e85e484a284f41448/href</a></iframe><p>Now if you have a service that depends on another, add the dependency in the constructor. For registering this service, you can then use the c parameter of the factory to get the value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/397bcfcf049d9b69d65a6b95112e9113/href">https://medium.com/media/397bcfcf049d9b69d65a6b95112e9113/href</a></iframe><p>For services with a lot of dependencies, it can be tedious to write that sort of code. That’s why <strong>kiwi </strong>comes with a generator 😃!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EacvTf2l0i49nTvbBqacvg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/K-5P_QZfGKs?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">rawpixel</a> on <a href="https://unsplash.com/search/photos/kiwi?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Generator usage</h3><p>The generator let the developer write less code. By using the generator, you minimize also the maintenance cost. When a dependency is added to a service, you just have to run the generator.</p><p>The generator handles factories and singletons, but not instances. Only const instances can appear in metadata, so it would be awkward to support only const instances here.</p><p>Let’s see how to use it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35b42a3b3e9f197c5ae198bd2f66c84a/href">https://medium.com/media/35b42a3b3e9f197c5ae198bd2f66c84a/href</a></iframe><ul><li>l3: You have to create a part directive. The value of this directive is simply the name of your file with a .g before the dart extension.</li><li>l5: You have to create an abstract class where all abstract methods are annotated with at least one @Register metadata.</li><li>l6: Annotation for registering a singleton for the type ServiceA.</li><li>l7: Annotation for registering a factory that will create a Service from a ServiceB.</li><li>l8: Annotation for registering a factory for ServiceB with the name factoryB.</li><li>l9: Annotation for registering a factory for ServiceC and by specifying to use the factoryB factory for the ServiceB dependency.</li><li>l10: Annotation for registering a factory for ServiceC, and by specifying to use the ServiceC.other constructor.</li><li>l28: The call to the generated class to register all the types.</li></ul><p>The following code represents the generated file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd7c27a9dbc9742542550e0eb8b3e226/href">https://medium.com/media/cd7c27a9dbc9742542550e0eb8b3e226/href</a></iframe><p>You can set as many register methods as you want. For example, you can have a method per environment and one method for registering everything that is common:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54e6a754b5e1bf3831524d74f4cf1084/href">https://medium.com/media/54e6a754b5e1bf3831524d74f4cf1084/href</a></iframe><p>This is very simple and modular, isn’t it?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nRVwqBXqV3Und3-9NXokvw.jpeg" /><figcaption>Dash already loves kiwi 😄</figcaption></figure><h3>Conclusion</h3><p>This library contains two packages you can now download on pub: <a href="https://pub.dartlang.org/packages/kiwi">kiwi</a>, which contains the container and the annotations, and <a href="https://pub.dartlang.org/packages/kiwi_generator">kiwi_generator </a>which is, as indicated by its name, the generator. I hope it will be as useful to you as it is for me.</p><p>You’ll find more details in the <a href="https://github.com/letsar/kiwi">GitHub repository</a> (like how to configure your project to use the generator).</p><p>Feedbacks are welcome 😃. If you find bugs, please issue them in the <a href="https://github.com/letsar/kiwi/issues">GitHub issues page</a>.</p><p>If this package is helping you, you can support me by ⭐️the <a href="https://github.com/letsar/kiwi">repository</a>, or 👏 this story.<br>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>.</p><p><a href="https://twitter.com/lets4r">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52ddb3935e6d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/announcing-kiwi-52ddb3935e6d">Announcing kiwi</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create simple animations for your articles with Flutter.]]></title>
            <link>https://medium.com/flutter-community/create-simple-animations-for-your-articles-with-flutter-7769085108d1?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7769085108d1</guid>
            <category><![CDATA[animation]]></category>
            <category><![CDATA[dartlang]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Sat, 01 Sep 2018 09:12:03 GMT</pubDate>
            <atom:updated>2018-09-01T09:12:03.973Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DOHaYbnHRp84rjaQIO_Dvw.png" /></figure><p>In a <a href="https://medium.com/flutter-community/how-i-replicated-the-ios-row-animation-in-flutter-81dfe9b60040">previous article</a>, I explained <em>how I reproduced the iOS sliding row animation in Flutter</em>, through my <a href="https://github.com/letsar/flutter_slidable">Slidable</a> widget. In that article I used some animations to illustrate my words and today I’m going to show you how I created them.</p><h3>📐 The tools</h3><p>I first looked for animations tools, but I didn’t want anything complicated, nor invest money and time for such simple animations. So I thought “Why do not just use <strong>Flutter</strong>?” <br>I tried something and it was really easy to create the animations I wanted, so I entirely coded my animations in <strong>Dart</strong> using <strong>Flutter</strong> 😄, isn’t it beautiful? It was like using a compiler to compile the compiler.</p><p>Once I had the animation, I just had to recorded it and transform it into a GIF. I used an emulator and recorded the interesting part of the screen with <a href="https://www.screentogif.com/?l=fr_fr">ScreenToGif</a> for that. I find this tool quite simple for my needs.</p><p>The last part is the easy one, and I will not dwell on the subject. Let’s focus on the Flutter part 🔍.</p><h3>📄 The code</h3><p>If you want to create your own staggered animations, I suggest you to read this official tutorial which is <strong>excellent</strong>: <a href="https://flutter.io/animations/staggered-animations/">https://flutter.io/animations/staggered-animations/</a></p><p>The following content it’s based on this tutorial, so you will find some similarities.</p><h4>Animation n°1</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*zXmFAZvOAjM8WDKdp_Jx6A.gif" /></figure><p>I’m starting with the most complicated animation, but it’s also the only one I’m going to detail.</p><p>Before diving into the code, let me show you the different animations and intervals used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M4ovA4akr4zs0KL6FYrCaQ.png" /><figcaption>Animation timeline</figcaption></figure><p>◼️ Items move upwards and skew during the first 30% of the timeline.<br>◼ Items move to the right during the next 30%. <br>◼ Items move downwards and get their shape back during the next 30%.<br>◼ Nothing animates during the last 10% of the timeline.</p><p>To create staggered animations, the easiest way is to create one AnimationController that manages all of the animations and to animate them with a CurvedAnimation which takes a parent animation (the animation controller) and a Curve. The Curve, here, is a special one: the Interval. It allows us define the part of the parent timeline in which the child animation will be animated from beginning to end:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b7b67e1362fba79baf3e7cd0b4e513e/href">https://medium.com/media/4b7b67e1362fba79baf3e7cd0b4e513e/href</a></iframe><p>This is a quote from the <a href="https://flutter.io/animations/staggered-animations/#complete-staggered-animation">flutter.io</a> website that describes how I designed my example:</p><blockquote>Like all interactive widgets, the complete animation consists of a widget pair: a stateless and a stateful widget.</blockquote><blockquote>The stateless widget specifies the Tweens, defines the Animation objects, and provides a build() function responsible for building the animating portion of the widget tree.</blockquote><blockquote>The stateful widget creates the controller, plays the animation, and builds the non-animating portion of the widget tree. The animation begins when a tap is detected anywhere in the screen.</blockquote><p>In the stateless widget, the tweens are created in the constructor:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13906bb286c2f06d3a2dee4bbf44841b/href">https://medium.com/media/13906bb286c2f06d3a2dee4bbf44841b/href</a></iframe><p>The value of the intervals are stored in constants:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e27901da0ac87bf0b6bf10e20ec1f0b1/href">https://medium.com/media/e27901da0ac87bf0b6bf10e20ec1f0b1/href</a></iframe><p>The build() function creates an <a href="https://docs.flutter.io/flutter/widgets/AnimatedBuilder-class.html">AnimatedBuilder</a> that takes a builder function (named _buildAnimation() here) and calls it when the animation controller notifies its changes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95e6839fa7dd089254411f65e932d820/href">https://medium.com/media/95e6839fa7dd089254411f65e932d820/href</a></iframe><p>It’s the buildAnimation() function which does all the heavy work 💪 :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fb094cfa664761a180ccfa71ec967fc/href">https://medium.com/media/5fb094cfa664761a180ccfa71ec967fc/href</a></iframe><p>The SkewTransition is a custom animated widget which is heavily inspired by the <a href="https://docs.flutter.io/flutter/widgets/ScaleTransition-class.html">ScaleTransition</a> widget. It’s the widget which controls the skew of the child:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e07e634c1af313a08614ea66e48a319/href">https://medium.com/media/9e07e634c1af313a08614ea66e48a319/href</a></iframe><p>The <a href="https://docs.flutter.io/flutter/widgets/SlideTransition-class.html">SlideTransition </a>widgets control the positions of the different children.</p><p>The stateful widget looks a lot like the one you can found in the flutter.io tutorial:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34ca460c49f669d77d2a45a0f646ad05/href">https://medium.com/media/34ca460c49f669d77d2a45a0f646ad05/href</a></iframe><p>This is all the code to run this example (less than 350 lines 😄):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db2f4cf14ef450db4dea26ed6aaa4108/href">https://medium.com/media/db2f4cf14ef450db4dea26ed6aaa4108/href</a></iframe><h4>Animation n°2</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*4-2LBlsgjvXVeWCEMsu_Ww.gif" /></figure><p>For this simpler animation, we have to keep only one SlideTransition, removes the slide actions, and change the interval and total duration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d43353f594508dffe6858756869d96a7/href">https://medium.com/media/d43353f594508dffe6858756869d96a7/href</a></iframe><h4>Animation n°3</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*neMg9gICwO7uRVmpGpsOlg.gif" /></figure><p>For this animation, we have to keep only one SlideTransition, modify the cross axis alignment, inverse the order of the children, and change the interval and total duration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73ade0ab60aac477612fdac6e852247a/href">https://medium.com/media/73ade0ab60aac477612fdac6e852247a/href</a></iframe><h3>📕 Conclusion</h3><p>With Flutter, no need for an extra tool, we can create simple animations and post them in articles like this one 😎. Sharing knowledge with Flutter becomes so easier 👍.</p><p>For a developer I find this very interesting for prototyping, because thanks to hot reload and hot restart, it really fast to change something, see how it behaves and to start again until we get what we want.</p><p>If you have any questions / suggestions, leave a comment below ✏️.</p><p>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>:</p><p><a href="https://twitter.com/lets4r">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7769085108d1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/create-simple-animations-for-your-articles-with-flutter-7769085108d1">Create simple animations for your articles with Flutter.</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I replicated the iOS sliding row animation in Flutter]]></title>
            <link>https://medium.com/flutter-community/how-i-replicated-the-ios-row-animation-in-flutter-81dfe9b60040?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/81dfe9b60040</guid>
            <category><![CDATA[dartlang]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swipe]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Fri, 24 Aug 2018 15:14:56 GMT</pubDate>
            <atom:updated>2018-08-24T15:20:02.688Z</atom:updated>
            <content:encoded><![CDATA[<h4>Deep dive on the SlidableDrawerDelegate</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AAsvH5V80-wsdwpb6BoBmA.png" /></figure><p>In a <a href="https://medium.com/flutter-community/slidable-a-flutter-story-f4a5f55f6a96">previous article</a>, I explained how I created the <a href="https://github.com/letsar/flutter_slidable">Slidable </a>widget. Today we’re going to dissect an essential component of it: the <strong>SlidableDrawerDelegate</strong>.</p><h3>🤔 What is it?</h3><p>In the <strong>Slidable </strong>widget, the developer can choose how the slide actions animate through what I named <strong>SlidableDelegates</strong>. The <strong>SlidableDrawerDelegate </strong>is one of the built-in ones:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*OlubJ7rmOK5QgvsC3aVY8Q.gif" /><figcaption>The SlidableDrawerDelegate in action</figcaption></figure><p>The animation I tried to replicate is the one you can found in the iOS Reminder app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*83FEk6qM3W3ze2ExYkw29Q.gif" /><figcaption>Swipe actions in the iOS Reminder app</figcaption></figure><h3>🎓 The animation in depth</h3><p>We can describe this animation like this:</p><ul><li>The actions are stacked so that the element closest to the tile is the lowest in the stack.</li><li>Initially all actions are placed outside, right next to the tile.</li><li>The actions move, for the same duration, from their starting position to their final position.</li></ul><h4>😐 images speak louder than words 🎨 😁</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*zXmFAZvOAjM8WDKdp_Jx6A.gif" /><figcaption>SlidableDrawerDelegate animation in 3D</figcaption></figure><p>We laid out how the animation should work, now let’s see how we can do it in Flutter.</p><h3>🔧 How it works</h3><p>Before showing you some code, let me explain how I designed this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*-NaZy2ti-ttX97yxDTC6xQ.png" /><figcaption>The SlidableDrawerDelegate inheritance graph</figcaption></figure><p>As we can see above, the <strong>SlidableDrawerDelegate </strong>inherits from <strong>SlidableStackDelegate </strong>which inherits from the <strong>SlidableDelegate</strong>.</p><p>When the <strong>Slidable </strong>builds, it calls the buildActionsmethod of the SlidableDelegate. It’s an abstract method, so the buildActions method of the SlidableStackDelegate is called:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f555406d5c2632193eaeb4fd7eb8c28/href">https://medium.com/media/7f555406d5c2632193eaeb4fd7eb8c28/href</a></iframe><p>The purpose of this class is to create the animation used to slide the tile to the desired direction and delegate the animation of the actions to the subclasses (through the buildStackActions method).<br>To do that we create a Stack with two children: the first child represents the animation of the actions and the second child is the animation of the tile. So that the tile is always painted on top of the actions.</p><p>The animation of the tile is simply created with the <a href="https://docs.flutter.io/flutter/widgets/SlideTransition-class.html">SlideTransition </a>widget for which we set an animation that starts from the zero offset and ends at the desired offset (the total length of the actions in the desired direction).</p><p>We can represent this animation like this (actions are purposely removed):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*4-2LBlsgjvXVeWCEMsu_Ww.gif" /><figcaption>SlideTransition animation of the tile</figcaption></figure><p>Now let’s see how we animate the actions in the case of a SlidableDrawerDelegate.</p><p>We have seen earlier that the animation of the actions are delegated to the buildStackActions of the SlidableStackDelegate subclasses. So let’s see how this method is implemented for SlidableDrawerDelegate:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8849cbf2bcf90e355f141521fbc1c962/href">https://medium.com/media/8849cbf2bcf90e355f141521fbc1c962/href</a></iframe><p>It’s a little more complicated that the previous method, so let’s break down this one piece by piece.</p><h4>🌴 Widget Tree</h4><p>Let’s focus on how the actions are built by taking a look at the widget tree:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*IABb0ZfK7lei13RPuoydhQ.png" /><figcaption>Example of actions widget tree</figcaption></figure><p>◼️ The first <a href="https://docs.flutter.io/flutter/widgets/Positioned-class.html">Positioned </a>widget is used to constrain this component to take all the place in the Stack built by the SlidableStackDelegate.</p><p>◼️ The <a href="https://docs.flutter.io/flutter/widgets/LayoutBuilder-class.html">LayoutBuilder</a> allows us to know the parent Stack extent. We use it then to compute the actual extent of an action.</p><p>◼️ The <a href="https://docs.flutter.io/flutter/widgets/AnimatedBuilder-class.html">AnimatedBuilder</a> builds its child whenever the actionsMoveAnimation changes (which is the animation used to slide the tile).</p><p>◼️ The <a href="https://docs.flutter.io/flutter/widgets/Stack-class.html">Stack </a>is then used to produce as many layers as actions. So, every child in the Stack is the widget of an action.</p><p>◼️ Finally each action has a Positioned widget as a parent in order to be placed at the desired position.</p><h4>💡 Connecting the dots</h4><p>Now that we saw the widget structure, let’s focus on the code:</p><p>◼️ <em>Lines 9–10,</em> we get the extent of an action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25803ee5b3b610d464be7d322f56bc38/href">https://medium.com/media/25803ee5b3b610d464be7d322f56bc38/href</a></iframe><p><em>Note</em>: The getMaxExtent is just a utility method used to get either the width or the heigth, depending on the direction the widget can slide.</p><p>◼️ <em>Lines 13–18,</em> we compute the position animation for each action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1801f3406168c14b67a56d62703d563/href">https://medium.com/media/c1801f3406168c14b67a56d62703d563/href</a></iframe><p>As we saw earlier, <em>initially all actions are placed outside, right next to the tile. </em>That means here that all actions start to the position -actionExtent (Line 3 above).<br>The end position for each action is then determined by its index in the action list (Line 4 above). For secondary actions, the lowest the index, the closest the action is to the tile at the end (inverted for actions).</p><p>◼️ <em>Lines 25–32,</em> we create the action widget with the desired position:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ffce36ca52200a56210867759abc9da/href">https://medium.com/media/4ffce36ca52200a56210867759abc9da/href</a></iframe><p>As mentioned before, actions and secondary actions are laid out in a different order. The last action will be the closest to the tile, whereas it’s the first for secondary actions.<br>The displayIndex is used for that.</p><p><em>Note</em>: The createPositioned is a utility method which creates a Positioned widget depending on the direction the widget can slide and the type of actions shown (secondary or not).</p><p>Finally we use the build method of the actionDelegate to get the actual action.</p><p>In the below gif, the <strong>Archive </strong>action is the first declared in the action list.<br>We can see that it starts at the same position as the <strong>Share </strong>action but end up the furthest to the tile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*neMg9gICwO7uRVmpGpsOlg.gif" /><figcaption>SlidableDrawerDelegate animations separated</figcaption></figure><h3>📕 Conclusion</h3><p>I explained here how I replicated the iOS sliding row animation in Flutter, through the <a href="https://github.com/letsar/flutter_slidable"><strong>Slidable</strong></a> widget and its <strong>SlidableDrawerDelegate</strong>. Now feel free to create your own SlidableDelegates 😄.</p><p>If you have any questions / suggestions, leave a comment below ✏️.</p><p>If this package is helping you, you can support me by ⭐️the <a href="https://github.com/letsar/flutter_slidable">repo</a>, or 👏 this article.<br>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>:</p><p><a href="https://twitter.com/lets4r">JavaScript is not available.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81dfe9b60040" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/how-i-replicated-the-ios-row-animation-in-flutter-81dfe9b60040">How I replicated the iOS sliding row animation in Flutter</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Slidable: A Flutter story]]></title>
            <link>https://medium.com/flutter-community/slidable-a-flutter-story-f4a5f55f6a96?source=rss-59ebc97c6bb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4a5f55f6a96</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[dartlang]]></category>
            <dc:creator><![CDATA[Romain Rastel]]></dc:creator>
            <pubDate>Sat, 21 Jul 2018 08:08:22 GMT</pubDate>
            <atom:updated>2018-07-21T23:38:55.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBp8dGLaZ8v8IHvXUYoZng.png" /></figure><h3>Synopsis</h3><p>This is the story behind the creation of the <strong>Slidable </strong>widget (available <a href="https://pub.dartlang.org/packages/flutter_slidable">here</a>). A widget that let you add contextual actions on your list items when you are sliding it to the left or to the right.</p><h3>How everything started</h3><p>I’m a passionate developer. This is what I do for living, but it’s mostly my main hobby ❤️. Some express themselves through words, drawings, music, I express myself through code. I’m more comfortable with variables and functions than with a ball or a racket. This is who I am.</p><p>We are in July 2018. It’s sunny ☀️ and kinda hot here in Brittany, France, but instead of enjoying the sun and going to the beach, I’m craving to learn something new and to code.</p><p>I’m a huge fan of Flutter and I already published some packages (<a href="https://github.com/letsar/flutter_staggered_grid_view">flutter_staggered_grid_view</a>, <a href="https://github.com/letsar/flutter_parallax">flutter_parallax</a>, <a href="https://github.com/letsar/flutter_sticky_header">flutter_sticky_header</a>). All of them have something in common: <strong>Slivers</strong>.<br>I want to learn something new, remember? So I picked a new subject: animations!</p><p>Now that I have something to learn, I need an idea to create something with this knowledge. I remember then, when I discovered Flutter, I thought about 3 widgets that didn’t exist at the moment: A staggered grid view, sticky headers, and a widget allowing the user to reveal contextual menus on the sides of a list item while sliding it to the left or to the right. I didn’t work on the last one, so the idea was found 💡.</p><h3>Where to start</h3><p>It’s always easier to have an example to build on. That’s why every time I want to create something, I start by researching if there is something similar I can tweak.</p><p>I started by searching on Pub Dart to see if someone had not already published exactly that, if it had been the case I would have stopped here and I would have looked for a new idea.</p><p>I didn’t find what I looked for, so I searched on StackOverflow and find this <a href="https://stackoverflow.com/questions/46651974/swipe-list-item-for-more-options-flutter/46662914">question</a>. Remi Rousselet, a top user, gave this really good <a href="https://stackoverflow.com/a/46662914/3241871">answer</a>.<br>I read his code, understand it, and it helped me to build a first prototype. So Remi, if you are reading me, thanks a lot 👏.</p><h3>From prototype to first release</h3><p>After I developed a prototype working with one animation, I immediately thought of letting the developer create it’s own. I remembered of <a href="https://docs.flutter.io/flutter/rendering/SliverGridDelegate-class.html">SliverDelegate,</a> which let the developer controls the layout of tiles in a grid, and decided to create something similar.</p><p>Let the developer customize the animation it’s great, but I have to provide some built-in animations so that any developer could use them, or tweak my animations to create theirs.</p><p>That’s why I firstly created 3 delegates:</p><h4>SlidableBehindDelegate</h4><p>With this delegate, the slide actions stay behind the list item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*-lxI0VkO5MCC3PW74VaLWA.gif" /><figcaption>Example of SlidableBehindDelegate</figcaption></figure><h4>SlidableScrollDelegate</h4><p>With this one, the slide actions scroll in the same direction as the list item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*KW9wXmgPGHbCV24gGIl8ZA.gif" /><figcaption>Example of SlidableScrollDelegate</figcaption></figure><h4>SlidableStrechDelegate</h4><p>With this delegate, the slide actions are growing while the list item is sliding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*lwGjFSE0--Ij7U5YbvOiSQ.gif" /><figcaption>Example of SlidableStrechDelegate</figcaption></figure><h4>SlidableDrawerDelegate</h4><p>And with this one, the slide actions are showing with a sort of parallax effect, like in iOS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*OlubJ7rmOK5QgvsC3aVY8Q.gif" /><figcaption>Example of SlidableDrawerDelegate</figcaption></figure><p>For the story, when I showed the first 3 delegates to my colleague, <a href="https://github.com/clovisnicolas">Clovis Nicolas</a>, he told me that it would be great to have one like in iOS. As I am not an iOS user, I thought it was more like SlidableStrechDelegate, but no.<br>This is how the SlidableDrawerDelegate was born.</p><h3>Animations in Flutter</h3><p>I didn’t wrote about what I learned about animations in Flutter, because there are other contents which explain it well, like <a href="https://proandroiddev.com/animations-in-flutter-6e02ee91a0b2">this one</a>.</p><p>But I can share my feeling about animations in Flutter: they are so great and easy to deal with 😍!</p><p>I just regret not having played with them before 😃.</p><h3>The end</h3><p>After finishing these built-in delegates, I thought it would be a good initial release. So I made my <a href="https://github.com/letsar/flutter_slidable">GitHub repository</a> public, and I published it on <a href="https://pub.dartlang.org/packages/flutter_slidable">Dart Pub</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*FXzo-qRHkPFTZ-hiQwb_gQ.gif" /><figcaption>Slidable widget overview</figcaption></figure><p>This is how the <strong>Slidable </strong>widget was born. Now it has to grow. If you want some new feature, you are welcome to create an <a href="https://github.com/letsar/flutter_slidable/issues">issue on GitHub</a> and explain what you want. If it’s align with my vision of this package, I will be happy to implement it!</p><p>You can find some documentation in the <a href="https://github.com/letsar/flutter_slidable">repository</a>, and the above example <a href="https://github.com/letsar/flutter_slidable/blob/master/example/lib/main.dart">here</a>.</p><p>If this package is helping you, you can support me by ⭐️the <a href="https://github.com/letsar/flutter_slidable">repo</a>, or 👏 this story.<br>You can also follow me on <a href="https://twitter.com/lets4r">Twitter</a>.</p><p>Let me know if you build an app with this package 😃.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4a5f55f6a96" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/slidable-a-flutter-story-f4a5f55f6a96">Slidable: A Flutter story</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>