<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Rafael Delos Santos</title>
    <subtitle>Software engineer</subtitle>
    <link rel="self" type="application/atom+xml" href="https://rafaelds.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://rafaelds.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-04-24T00:00:00+00:00</updated>
    <id>https://rafaelds.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Simpler and Better Flutter State Management with Cubit</title>
        <published>2024-04-24T00:00:00+00:00</published>
        <updated>2024-04-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/simpler-and-better-flutter-state-management-with-cubit/"/>
        <id>https://rafaelds.com/posts/simpler-and-better-flutter-state-management-with-cubit/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/simpler-and-better-flutter-state-management-with-cubit/">&lt;p&gt;In this article, we will learn how to use &lt;code&gt;Cubit&lt;&#x2F;code&gt; to cleanly organise our codebase to be more readable, scalable, and testable. This is also one of the better ways in Flutter to separate the UI logic and business logic.&lt;&#x2F;p&gt;
&lt;p&gt;Note that &lt;code&gt;Cubit&lt;&#x2F;code&gt; is only a piece of a bigger package called &lt;code&gt;Bloc&lt;&#x2F;code&gt; that provides a complete state management solution for &lt;code&gt;Dart&lt;&#x2F;code&gt; and &lt;code&gt;Flutter&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;why-do-we-need-to-handle-states&quot;&gt;Why do we need to handle states?&lt;&#x2F;h2&gt;
&lt;p&gt;State management and app architecture in Flutter has been and still is an on-going hot topic across different developers.  Flutter developers have been given many options on which one to learn and use to start developing apps with.&lt;&#x2F;p&gt;
&lt;p&gt;It is important to use state management because each application needs to persist information and update the view with it. Without states (or technically, just one state), apps will look static and will not be very useful.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at a simple app that displays a counter with only one state.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; main.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:cubit_tutorial&#x2F;counter_screen.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      title&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;Counter app&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      debugShowCheckedModeBanner&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;fromSeed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seedColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;deepPurple)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        useMaterial3&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; counter_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatefulWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; createState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;          &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;_count&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextStyle&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            fontSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 86.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;simpler-and-better-flutter-state-management-with-cubit&#x2F;fig-1-one-state-app.png&quot; alt=&quot;fig-1-one-state-app&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 1. One-state counter app&lt;&#x2F;p&gt;
&lt;p&gt;This is an app and it works. However, being an app that only has one state is as good as an image.&lt;&#x2F;p&gt;
&lt;p&gt;If we want to add features such as entering text, downloading images from a server, or in our example, displaying a counter and updating it, then we always need to show different states in our app. The question is how we do state management properly.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-do-we-need-state-management&quot;&gt;Why do we need state management?&lt;&#x2F;h2&gt;
&lt;p&gt;The simplest way to change the state in Flutter is to use the &lt;code&gt;setState&lt;&#x2F;code&gt; method within a &lt;code&gt;StatefulWidget&lt;&#x2F;code&gt;. Using &lt;code&gt;setState&lt;&#x2F;code&gt; can change the information that we display in our app. In our simple example, we can add an &lt;code&gt;onTapListener&lt;&#x2F;code&gt; to our screen that increases the value of the &lt;code&gt;counter&lt;&#x2F;code&gt; whilst updating the UI that we display in the app.&lt;&#x2F;p&gt;
&lt;p&gt;For a quick refresher on &lt;code&gt;setState&lt;&#x2F;code&gt;, have a look at https:&#x2F;&#x2F;rafaelds&#x2F;flutter-setstate-the-simplest-state-management-in-flutter.&lt;&#x2F;p&gt;
&lt;p&gt;If we update our example to increment the counter when tapping the screen using &lt;code&gt;setState&lt;&#x2F;code&gt;,  the code will look something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; counter_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatefulWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; createState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; GestureDetector&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;        &#x2F;&#x2F;Update the state and refresh the UI&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;        setState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          _count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _count &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;            &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;_count&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextStyle&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              fontSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 86.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And sure enough, the counter app example works by tapping anywhere in the app and updating the counter using the &lt;code&gt;setState&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;simpler-and-better-flutter-state-management-with-cubit&#x2F;compressed-vid-1-counter-set-state-example.mov.gif&quot; alt=&quot;compressed-vid-1-counter-set-state-example.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 1. Counter app example using setState&lt;&#x2F;p&gt;
&lt;p&gt;However, once we start developing a more complex user-interface that has multiple widgets, our code can grow significantly in size. Having many &lt;code&gt;setState&lt;&#x2F;code&gt; calls can be difficult to track when they are being called from different parts of our widgets.&lt;&#x2F;p&gt;
&lt;p&gt;We also do not want all our business logic to be part of the UI class which would make these cases hard to test.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;cubit-basics&quot;&gt;Cubit Basics&lt;&#x2F;h1&gt;
&lt;p&gt;A &lt;code&gt;Cubit&lt;&#x2F;code&gt; is a class from the package &lt;code&gt;Bloc&lt;&#x2F;code&gt; that can expose functions and hold state. What this means is that &lt;code&gt;Cubit&lt;&#x2F;code&gt; can be used to manage the states of our app.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;simpler-and-better-flutter-state-management-with-cubit&#x2F;fig-2-cubit-architecture.webp&quot; alt=&quot;fig-2-cubit-architecture&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 2. Cubit architecture (https:&#x2F;&#x2F;bloclibrary.dev&#x2F;bloc-concepts&#x2F;#cubit)&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Cubit&lt;&#x2F;code&gt; class contains a &lt;code&gt;state&lt;&#x2F;code&gt; which can be any type that we define. This &lt;code&gt;state&lt;&#x2F;code&gt; can be changed through the functions that we define and expose back to our Flutter widgets.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Cubit&lt;&#x2F;code&gt; can also interact with the other layers of your app such as database, network, and even other &lt;code&gt;Cubit&lt;&#x2F;code&gt; instances.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-flutter-bloc&quot;&gt;Installing flutter_bloc&lt;&#x2F;h2&gt;
&lt;p&gt;To be able to use &lt;code&gt;Cubit&lt;&#x2F;code&gt;, we need to install the &lt;code&gt;flutter_bloc&lt;&#x2F;code&gt; package.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add flutter_bloc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another way is to manually add the &lt;code&gt;flutter_bloc&lt;&#x2F;code&gt; library in the &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; file. Make sure to check the latest version of the package.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter_bloc&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^8.1.5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;creating-a-cubit&quot;&gt;Creating a Cubit&lt;&#x2F;h2&gt;
&lt;p&gt;A &lt;code&gt;Cubit&lt;&#x2F;code&gt; can represent the state of a whole app at the most (for really simple apps), or a single widget at the very least. When creating a &lt;code&gt;Cubit&lt;&#x2F;code&gt;, we need to define the functions to expose to the clients, and the type of state that we want the &lt;code&gt;Cubit&lt;&#x2F;code&gt; to represent.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s create a &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; for our sample app. This &lt;code&gt;Cubit&lt;&#x2F;code&gt; has type &lt;code&gt;int&lt;&#x2F;code&gt; as a state, and exposes a function to increment this state.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;counter_cubit.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_bloc&#x2F;flutter_bloc.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Cubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F; Default state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; increment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Update the state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    emit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(state &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s look at the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; example above.&lt;&#x2F;p&gt;
&lt;p&gt;When creating a &lt;code&gt;Cubit&lt;&#x2F;code&gt;, this class should extend the &lt;code&gt;Cubit&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; abstract class. Doing so will inherit many of the functions and properties that we will use later on, one of which is the &lt;code&gt;state&lt;&#x2F;code&gt;. We also need to define the initial state of the cubit, which in our example is &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;state&lt;&#x2F;code&gt; property represents the current state of type &lt;code&gt;T&lt;&#x2F;code&gt;. In the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; example, this state is represented by an integer. This type can be primitive types such as &lt;code&gt;float&lt;&#x2F;code&gt;, &lt;code&gt;string&lt;&#x2F;code&gt;, &lt;code&gt;datetime&lt;&#x2F;code&gt;, as well as customised data classes.&lt;&#x2F;p&gt;
&lt;p&gt;Calling the &lt;code&gt;emit&lt;&#x2F;code&gt; function updates the current state of the &lt;code&gt;Cubit&lt;&#x2F;code&gt;. In our example, we want to increment the current value of the state by &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;basic-usage-of-a-cubit&quot;&gt;Basic usage of a Cubit&lt;&#x2F;h2&gt;
&lt;p&gt;We can use the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; above in different ways. Let&#x27;s keep it simple first and start with an example that does not involve Flutter widgets.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:cubit_tutorial&#x2F;counter_cubit.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; cubit &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  print&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(cubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;state)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; Prints `0`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  cubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  print&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(cubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;state)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; Prints `1`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our first example, we created an instance of the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt;. We know that the initial state is going to be &lt;code&gt;0&lt;&#x2F;code&gt;, and calling the &lt;code&gt;increment()&lt;&#x2F;code&gt; function will increase it by &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This simple example however does not represent the real usage of cubits in a Flutter application. In order to fully utilise this state management, we will need to use the other classes found in the &lt;code&gt;Bloc&lt;&#x2F;code&gt; library.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s see how we can use cubit in our own counter app.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;using-cubit-in-flutter&quot;&gt;Using Cubit in Flutter&lt;&#x2F;h1&gt;
&lt;p&gt;I have mentioned before that &lt;code&gt;Cubit&lt;&#x2F;code&gt; is part of a bigger state management solution called &lt;code&gt;Bloc&lt;&#x2F;code&gt;. In order to use &lt;code&gt;Cubit&lt;&#x2F;code&gt; with Flutter, we need to set up our app to respond to state changes in our &lt;code&gt;Cubit&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing we need to learn is how to provide our &lt;code&gt;Cubit&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-blocprovider-widget&quot;&gt;The BlocProvider widget&lt;&#x2F;h2&gt;
&lt;p&gt;We can think of the &lt;code&gt;BlocProvider&lt;&#x2F;code&gt; widget as a form of dependency injection tool for our &lt;code&gt;Cubit&lt;&#x2F;code&gt;. In a normal app, it is expected to have different cubits that have different representations of states across the system.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BlocProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	create&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;Rest of the app&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A &lt;code&gt;BlocProvider&lt;&#x2F;code&gt; requires the type of the &lt;code&gt;Cubit&lt;&#x2F;code&gt; that we want to be available to the children widgets. It has a &lt;code&gt;create&lt;&#x2F;code&gt; parameter where we initialise the &lt;code&gt;Cubit&lt;&#x2F;code&gt;, as well as the &lt;code&gt;child&lt;&#x2F;code&gt; property which contains the widget tree that will potentially access this &lt;code&gt;Cubit&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we want to provide multiple blocs or cubits, we can use the &lt;code&gt;MultiBlocProvider&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MultiBlocProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	providers&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    BlocProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;			create&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    BlocProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;AnotherBloc&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    	create&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; AnotherBloc&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(userRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; UserRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;Rest of the app&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Any widget in our app that needs access to a particular &lt;code&gt;Cubit&lt;&#x2F;code&gt; should have one of the parents that provides this &lt;code&gt;Cubit&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;simpler-and-better-flutter-state-management-with-cubit&#x2F;fig-3-cubit-scope.png&quot; alt=&quot;fig-3-cubit-scope&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 3. Widget tree to show where the provided &lt;code&gt;Cubit&lt;&#x2F;code&gt; or &lt;code&gt;Bloc&lt;&#x2F;code&gt; can be referenced&lt;&#x2F;p&gt;
&lt;p&gt;This is very important because trying to retrieve access to a &lt;code&gt;Cubit&lt;&#x2F;code&gt; that has not been &quot;provided&quot; will result in an app crash.&lt;&#x2F;p&gt;
&lt;p&gt;A good place to put the &lt;code&gt;BlocProvider&lt;&#x2F;code&gt; is at the topmost level of the app, which is usually the &lt;code&gt;MaterialApp&lt;&#x2F;code&gt; or &lt;code&gt;CupertinoApp&lt;&#x2F;code&gt;. This way, the cubits that you need will be readily accessible anywhere down the widget tree.&lt;&#x2F;p&gt;
&lt;p&gt;Here is our app wrapped with a &lt;code&gt;BlocProvider&amp;lt;CounterCubit&amp;gt;&lt;&#x2F;code&gt; so we can use the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; anywhere in the app.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; main.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:cubit_tutorial&#x2F;counter_cubit.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:cubit_tutorial&#x2F;counter_screen.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_bloc&#x2F;flutter_bloc.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BlocProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      create&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        title&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;Counter App&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        debugShowCheckedModeBanner&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;fromSeed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seedColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;deepPurple)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          useMaterial3&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;the-blocbuilder-widget&quot;&gt;The BlocBuilder widget&lt;&#x2F;h2&gt;
&lt;p&gt;We need to know how to use the &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; widget to use our counter cubit. A &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; widget is a widget that observes the changes of a &lt;code&gt;Bloc&lt;&#x2F;code&gt; and rebuilds the widget when it does.&lt;&#x2F;p&gt;
&lt;p&gt;An important thing to note here is that a &lt;code&gt;Cubit&lt;&#x2F;code&gt; is a &lt;code&gt;Bloc&lt;&#x2F;code&gt; as cubits extend the &lt;code&gt;BlocBase&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Given that we have already setup the app so that everything under the &lt;code&gt;MaterialApp&lt;&#x2F;code&gt; is wrapped with the correct &lt;code&gt;BlocProvider&lt;&#x2F;code&gt; for the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt;, then we can use the &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; as such:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BlocBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	bloc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;read&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;instance of CounterCubit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;   	&#x2F;&#x2F;Build the widget according to the value of `state`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;state&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; needs to be supplied by what type of &lt;code&gt;Bloc&lt;&#x2F;code&gt; it needs to observe and what type of &lt;code&gt;state&lt;&#x2F;code&gt; it reads. In our example, we want to observe a &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; type with the &lt;code&gt;state&lt;&#x2F;code&gt; as type int. With this requirement, we can instantiate the &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; to be &lt;code&gt;BlocBuilder&amp;lt;CounterCubit, int&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;bloc&lt;&#x2F;code&gt; parameter of &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; accepts an instance of &lt;code&gt;CounterCubit&lt;&#x2F;code&gt;. One way of providing one is by directly instantiating this as &lt;code&gt;CounterCubit()&lt;&#x2F;code&gt;. However, be warned that in doing so, we might refer to different instances of &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; which may not be the expected behaviour in our app.&lt;&#x2F;p&gt;
&lt;p&gt;Since we want to refer to a single instance of &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; which was provided by the &lt;code&gt;BlocProvider&lt;&#x2F;code&gt;, we can access this instance by using the extension function &lt;code&gt;context.read&amp;lt;CounterCubit&amp;gt;()&lt;&#x2F;code&gt;. This call tries to get the nearest &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; in the parent widget tree.&lt;&#x2F;p&gt;
&lt;p&gt;The last parameter of &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; is the &lt;code&gt;builder&lt;&#x2F;code&gt; itself in which we construct the widget to return. If we want to display the &lt;code&gt;state&lt;&#x2F;code&gt; of &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; in a &lt;code&gt;Text&lt;&#x2F;code&gt;, then we can do so by using the &lt;code&gt;state&lt;&#x2F;code&gt; parameter. Remember that this value in our example is of type &lt;code&gt;int&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;updating-the-state-through-the-cubit&quot;&gt;Updating the state through the Cubit&lt;&#x2F;h2&gt;
&lt;p&gt;Instead of using the &lt;code&gt;setState&lt;&#x2F;code&gt; function of a &lt;code&gt;StatefulWidget&lt;&#x2F;code&gt; to update the UI, we can now use the &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; that listens to the state changes  &lt;code&gt;CounterCubit&lt;&#x2F;code&gt;. As previously mentioned, we can access this &lt;code&gt;Cubit&lt;&#x2F;code&gt; through the extension function &lt;code&gt;context.read&amp;lt;CounterCubit&amp;gt;()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we update our &lt;code&gt;onTap&lt;&#x2F;code&gt; function to update the counter:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;GestureDetector&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Access the CounterCubit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;read&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  	&#x2F;&#x2F;Rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whenever we call the &lt;code&gt;increment()&lt;&#x2F;code&gt;, the state of the &lt;code&gt;CounterCubit&lt;&#x2F;code&gt; is updated. The function &lt;code&gt;emit&lt;&#x2F;code&gt; is called which in turn updates the state of &lt;code&gt;CounterCubit&lt;&#x2F;code&gt;. The &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; that observes this &lt;code&gt;Cubit&lt;&#x2F;code&gt; will then see that the state has been changed and will rebuild the widget using the new value.&lt;&#x2F;p&gt;
&lt;p&gt;Applying all these things, the &lt;code&gt;CounterScreen&lt;&#x2F;code&gt; widget that we have will be:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; counter_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:cubit_tutorial&#x2F;counter_cubit.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_bloc&#x2F;flutter_bloc.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatefulWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; createState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _CounterScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BlocBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      bloc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;read&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; GestureDetector&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;read&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;CounterCubit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;                &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;state&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextStyle&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  fontSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 86.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These changes did not really modify how the app behaves and will look exactly the same as Video 1. But with these changes, our app is much cleaner now because the logic is now separated from the UI.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;simpler-and-better-flutter-state-management-with-cubit&#x2F;compressed-vid-1-counter-set-state-example.mov.gif&quot; alt=&quot;compressed-vid-1-counter-set-state-example.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 2. The same Counter example result using Cubit.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;state-with-custom-data-type&quot;&gt;State with custom data type&lt;&#x2F;h1&gt;
&lt;p&gt;We have implemented a state management system using &lt;code&gt;Cubit&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; with a primitive data type as its state. The way &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; determines whether to rebuild the widget or not depends on the state changes.&lt;&#x2F;p&gt;
&lt;p&gt;Primitive data types such as &lt;code&gt;int&lt;&#x2F;code&gt; or &lt;code&gt;string&lt;&#x2F;code&gt; can simply be compared to see if they&#x27;re the same or not, but with custom data objects, it&#x27;s not as straightforward as primitive data types.&lt;&#x2F;p&gt;
&lt;p&gt;For optimisation, we do not want &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt; or other &lt;code&gt;Bloc&lt;&#x2F;code&gt; widgets to recreate the widgets even if the new state has the same values. In order to make sure that the equality of each state for custom data types is checked correctly, we can use the &lt;code&gt;equatable&lt;&#x2F;code&gt; package.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;install-equatable&quot;&gt;Install Equatable&lt;&#x2F;h2&gt;
&lt;p&gt;Install the &lt;code&gt;Equatable&lt;&#x2F;code&gt; package using:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add equatable&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;using-equatable-for-custom-data-types&quot;&gt;Using Equatable for custom data types&lt;&#x2F;h2&gt;
&lt;p&gt;If you have a custom data type say, &lt;code&gt;CustomDataType&lt;&#x2F;code&gt;, extend the &lt;code&gt;Equatable&lt;&#x2F;code&gt; class and add the properties that you want to include for data comparison.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CustomDataType&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Equatable&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CustomDataType&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;required&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; required&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;count})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; count&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Object&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;?&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; props &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; count]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, we want to make sure that the properties &lt;code&gt;name&lt;&#x2F;code&gt; and &lt;code&gt;count&lt;&#x2F;code&gt; are used to compare this object with another &lt;code&gt;CustomDataType&lt;&#x2F;code&gt; and not just the object reference. To do so, we need to override the &lt;code&gt;props&lt;&#x2F;code&gt; method and include the properties that we want to include for comparison.&lt;&#x2F;p&gt;
&lt;p&gt;Once the class implements the &lt;code&gt;props&lt;&#x2F;code&gt; method, it&#x27;s now ready to be used as the state type of our &lt;code&gt;Cubit&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;Using &lt;code&gt;Cubit&lt;&#x2F;code&gt; is a good way to implement a scalable state management framework in Flutter through the &lt;code&gt;bloc&lt;&#x2F;code&gt; package. In order to fully utilise &lt;code&gt;Cubit&lt;&#x2F;code&gt; in Flutter, it is best to use the Flutter widgets provided by the &lt;code&gt;bloc&lt;&#x2F;code&gt; library such as &lt;code&gt;BlocBuilder&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, make sure that the Cubit we use is readily provided using a &lt;code&gt;BlocProvider&lt;&#x2F;code&gt; that is usually initialised at the topmost widget of the app.&lt;&#x2F;p&gt;
&lt;p&gt;With &lt;code&gt;Cubit&lt;&#x2F;code&gt;, our Flutter code is much more readable, scalable, and testable.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;reference&quot;&gt;Reference&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;bloclibrary.dev&#x2F;bloc-concepts&#x2F;#cubit&quot;&gt;https:&#x2F;&#x2F;bloclibrary.dev&#x2F;bloc-concepts&#x2F;#cubit&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to use custom theme colours in Flutter without using Material or Cupertino Theme</title>
        <published>2024-04-05T00:00:00+00:00</published>
        <updated>2024-04-05T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/how-to-use-custom-theme-colours-in-flutter/"/>
        <id>https://rafaelds.com/posts/how-to-use-custom-theme-colours-in-flutter/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/how-to-use-custom-theme-colours-in-flutter/">&lt;p&gt;It is important to think about how to add structured colour theming to your design when building Flutter apps. The most popular way to do so is to use the Material Design&#x27;s &lt;code&gt;ColorScheme&lt;&#x2F;code&gt; class. The better approach is to use a design system that specifically works with your app&#x27;s design system such as the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pub.dev&#x2F;packages&#x2F;color_theme_provider&quot;&gt;color_theme_provider&lt;&#x2F;a&gt; package.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-material-design-colour-system&quot;&gt;Using the Material Design colour system&lt;&#x2F;h2&gt;
&lt;p&gt;In many examples, you will always see the following code snippet wherein you can define the Material design colour scheme.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;fromSeed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      seedColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;purple&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is not a bad way to start your app with proper colour schemes. Whenever you use widgets that extend the &lt;code&gt;Material&lt;&#x2F;code&gt; widget, then their colour properties will adhere to this theme. However, the downside of this approach is it lacks the customisation that you might need in your application.&lt;&#x2F;p&gt;
&lt;p&gt;A more customisable approach to this is to create your own &lt;code&gt;ColorScheme&lt;&#x2F;code&gt; and define each property with the appropriate colour.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      brightness&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; brightness&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      primary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; primary&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onPrimary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onPrimary&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      secondary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; secondary&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onSecondary&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onSecondary&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      error&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; error&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onError&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onError&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      background&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; background&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onBackground&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onBackground&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      surface&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; surface&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onSurface&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onSurface&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once you defined the colours, you can start using these properties in your widgets by getting the &lt;code&gt;ColorScheme&lt;&#x2F;code&gt; from the context.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorScheme &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(context)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;background&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;the-downside-of-using-colorscheme&quot;&gt;The downside of using ColorScheme&lt;&#x2F;h2&gt;
&lt;p&gt;The problem with using the Material design colour scheme is that we are restricted to using the following properties defined by this design system. If we are working with a team that has its design system, it might be difficult to match each property to a corresponding token.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s say in my app, I have the following design tokens:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;brandColor&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;backgroundColor&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;errorColor&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;successColor&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;textColor&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I can use the &lt;code&gt;ColorScheme&lt;&#x2F;code&gt; to apply our design tokens to match the &lt;code&gt;brandColor&lt;&#x2F;code&gt; to &lt;code&gt;primaryColor&lt;&#x2F;code&gt;, &lt;code&gt;backgroundColor&lt;&#x2F;code&gt; to &lt;code&gt;background&lt;&#x2F;code&gt;, and so on with the &lt;code&gt;errorColor&lt;&#x2F;code&gt;. But what about &lt;code&gt;successColor&lt;&#x2F;code&gt;? If I want to have a consistent colour theme across the app, then using the &lt;code&gt;ColorScheme&lt;&#x2F;code&gt; falls short. I will have to hard code the &lt;code&gt;successColor&lt;&#x2F;code&gt; somewhere in the app.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-color-theme-provider&quot;&gt;Using the color_theme_provider&lt;&#x2F;h2&gt;
&lt;p&gt;One way to solve this issue is to use the &lt;code&gt;color_theme_provider&lt;&#x2F;code&gt; package. This package lets you define your colour scheme according to your requirements using the &lt;code&gt;ColorTheme&lt;&#x2F;code&gt; interface which can be accessed by any widgets in your application as long as these widgets are part of the &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt; widget tree.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;color_theme_provider&lt;&#x2F;code&gt; has the following features:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Automatically changes the colour theme based on the system&#x27;s mode (light or dark)&lt;&#x2F;li&gt;
&lt;li&gt;Dynamically change the colour theme in runtime&lt;&#x2F;li&gt;
&lt;li&gt;Does not depend on Material or Apple design tokens&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;installation&quot;&gt;Installation&lt;&#x2F;h3&gt;
&lt;p&gt;You can install this using&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add color_theme_provider&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;or adding it directly to &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  color_theme_provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1.1.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;defining-the-colortheme-interface&quot;&gt;Defining the ColorTheme interface&lt;&#x2F;h3&gt;
&lt;p&gt;Once the package has been imported into the project, we can start defining the colours that we need in our app. Let&#x27;s use the colours that we defined above and make an interface that extends &lt;code&gt;ColorTheme&lt;&#x2F;code&gt;. Let&#x27;s call it &lt;code&gt;MyTheme&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:color_theme_provider&#x2F;color_theme_provider.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;abstract class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; implements&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; brandColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; errorColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; successColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; get&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; textColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We are using an &lt;code&gt;abstract&lt;&#x2F;code&gt; class or an interface so that we can define a colour theme for light mode and another for dark mode. We can also define other colour themes if we want to have multiple colour themes in the app. The only requirement is that these classes should extend &lt;code&gt;MyTheme&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;final class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; LightMyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; implements&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; brandColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; backgroundColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;white&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; errorColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; successColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; textColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can also define another theme for dark mode as such. In this example, we just changed the &lt;code&gt;backgroundColor&lt;&#x2F;code&gt; to a dark colour and its inverse for the &lt;code&gt;textColor&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;final class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; DarkMyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; implements&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; brandColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; backgroundColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; errorColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; successColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; textColor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;white&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;wrapping-your-app-with-colorthemeprovider&quot;&gt;Wrapping your app with ColorThemeProvider&lt;&#x2F;h3&gt;
&lt;p&gt;Once we have defined the themes, we can wrap our app with the &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt;. This is necessary so that all the widgets under this widget tree can access the theme. If you have a top-level widget such as &lt;code&gt;MaterialApp&lt;&#x2F;code&gt;, then we can wrap this widget with &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorThemeProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; LightMyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      darkTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; DarkMyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Don&#x27;t forget to define the generic &lt;code&gt;MyTheme&lt;&#x2F;code&gt; when using the &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt;. All the widgets under the &lt;code&gt;HomePage&lt;&#x2F;code&gt; can access the &lt;code&gt;MyTheme&lt;&#x2F;code&gt; instance and apply the colours defined in there.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;applying-the-colour-theme&quot;&gt;Applying the colour theme&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we have set up the app, we can start using these colours. We will need access to the widget&#x27;s &lt;code&gt;BuildContext&lt;&#x2F;code&gt; to get the theme from the &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;color_theme_provider&lt;&#x2F;code&gt; has an extension function to get a colour theme instance. To get the colour theme, use the &lt;code&gt;context.colorTheme&amp;lt;MyTheme&amp;gt;()&lt;&#x2F;code&gt; extension function. Make sure to include the generic type of your theme. In our example, it&#x27;s &lt;code&gt;MyTheme&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Using the colours&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;brandColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;successColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; ..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;example-of-using-the-color-theme&quot;&gt;Example of using the color theme&lt;&#x2F;h3&gt;
&lt;p&gt;If we apply all these concepts, we can simply use these colours in our widgets. As an example, let&#x27;s display the different colours from the theme. When using the &lt;code&gt;colorTheme&lt;&#x2F;code&gt;, we just need to define which colour property we want to use. The &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt; takes care of applying these colours, whether it is in light or dark mode.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Figure 1. Using ColorThemeProvider for Light and Dark theme.&quot; title=&quot;Figure 1. Using ColorThemeProvider for Light and Dark theme.&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;theme-sample.1f7ec2dadf198b9b.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;theme-sample.43e38b366554ee0b.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;theme-sample.1f7ec2dadf198b9b.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;            Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              height&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              width&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;brandColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;            Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              height&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              width&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;successColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;            Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              height&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              width&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 64.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;errorColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;            Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;              &amp;#39;Sample Text&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextStyle&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;textColor)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the example above, we can see that the background and the text colours change according to the device&#x27;s system settings.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dynamically-changing-the-theme&quot;&gt;Dynamically changing the theme&lt;&#x2F;h3&gt;
&lt;p&gt;We can also change the theme whilst the app is running using the &lt;code&gt;ColorThemeManager&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colorThemeMananger &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;colorThemeManager&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;colorThemeMananger&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;setTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;NewLightTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;colorThemeMananger&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;setDarkTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;NewDarkTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is useful if your app offers multiple colour schemes to users.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;color_theme_provider&lt;&#x2F;code&gt; package is an alternative approach to using colour themes. With this package, we are not restricted to using the Material design or Cupertino colour properties. We can easily define the colours that we need according to the app&#x27;s requirements.&lt;&#x2F;p&gt;
&lt;p&gt;First, create an interface that extends &lt;code&gt;ColorTheme&lt;&#x2F;code&gt;. Then, define a light and dark mode extending the created theme. Once the themes have been defined, wrap your application with &lt;code&gt;ColorThemeProvider&lt;&#x2F;code&gt;. To access the theme, use &lt;code&gt;context.themeColor&amp;lt;YourTheme&amp;gt;()&lt;&#x2F;code&gt; and start applying the colours.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, you can change the colour theme in runtime using the &lt;code&gt;ColorThemeManager&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pub.dev&#x2F;packages&#x2F;color_theme_provider&quot;&gt;https:&#x2F;&#x2F;pub.dev&#x2F;packages&#x2F;color_theme_provider&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to use PageView in Flutter</title>
        <published>2024-03-14T00:00:00+00:00</published>
        <updated>2024-03-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/how-to-use-pageview-in-flutter/"/>
        <id>https://rafaelds.com/posts/how-to-use-pageview-in-flutter/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/how-to-use-pageview-in-flutter/">&lt;p&gt;There are many ways to display content in a mobile app. One of the most popular approach to show tabs and pages in Flutter is to use the &lt;code&gt;PageView&lt;&#x2F;code&gt; widget.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;PageView&lt;&#x2F;code&gt; widget allows us to display sliding and swipeable pages in our Flutter app. This is usually used to display multiple pages where we display one screen at time. This ranges from viewing full screen photos to watching short videos one at a time.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-pageview-in-flutter&#x2F;page-view-figure.png&quot; alt=&quot;page-view-figure&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 1. PageView example.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;simple-pageview-example&quot;&gt;Simple PageView example&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s start with a simple example of using &lt;code&gt;PageView&lt;&#x2F;code&gt;. Let&#x27;s say we have a photo gallery app where we can view different photos. Using &lt;code&gt;PageView&lt;&#x2F;code&gt; is a perfect approach to display these photos one at a time. Let&#x27;s use a &lt;code&gt;Container&lt;&#x2F;code&gt; with different colours to represent a photo.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageView&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, we have four swipeable screens of different colours. We define them in the &lt;code&gt;children&lt;&#x2F;code&gt; parameter of &lt;code&gt;PageView&lt;&#x2F;code&gt;. Running this code will look something like this.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-pageview-in-flutter&#x2F;compressed-figure1-swipe-page-view.mov.gif&quot; alt=&quot;compressed-figure1-swipe-page-view.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 1. Swiping pages horizontally with &lt;code&gt;PageView&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You can also change the direction of the swipe by changing the property &lt;code&gt;scrollDirection&lt;&#x2F;code&gt; to &lt;code&gt;Axis.vertical&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageView&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        scrollDirection&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Axis&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;vertical&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-pageview-in-flutter&#x2F;compressed-figure2-swipe-page-view-vertical.mov.gif&quot; alt=&quot;compressed-figure2-swipe-page-view-vertical.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 2. Swiping pages vertically with &lt;code&gt;PageView&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-pageview-with-dynamic-number-of-pages&quot;&gt;Using PageView with dynamic number of pages&lt;&#x2F;h2&gt;
&lt;p&gt;In our previous example, we already know the number of screens that our &lt;code&gt;PageView&lt;&#x2F;code&gt; will display. In real apps, it is common to display an unknown number of screens such as full screen photos or content that your app gets remotely.&lt;&#x2F;p&gt;
&lt;p&gt;To display an unknown number of items, we should use &lt;code&gt;PageView.builder&lt;&#x2F;code&gt; and the &lt;code&gt;itemBuilder&lt;&#x2F;code&gt; instead of the &lt;code&gt;children&lt;&#x2F;code&gt; property. For this example, this will be an infinite amount of pages that shows random colours every time the &lt;code&gt;PageView&lt;&#x2F;code&gt; builds the screen.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageView&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        itemBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; index) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;primaries[&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Random&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;nextInt&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;primaries&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;length)]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-pageview-in-flutter&#x2F;compressed-figure3-infinite-pages.mov.gif&quot; alt=&quot;compressed-figure3-infinite-pages.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 3. Displaying infinite number of screens with &lt;code&gt;PageView&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we know before hand the number of items that we want to display, we can use the &lt;code&gt;itemCount&lt;&#x2F;code&gt; parameter so that the &lt;code&gt;PageView&lt;&#x2F;code&gt; will stop building new pages past that count.&lt;&#x2F;p&gt;
&lt;p&gt;In the next example, let&#x27;s pretend that we&#x27;re getting the &quot;photos&quot; from an API. Once we get these items, we&#x27;ll use the itemBuilder&#x27;s index to build the page with the correct content. We also set the &lt;code&gt;itemCount&lt;&#x2F;code&gt; based on the number of items we get.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _photosRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageView&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        itemCount&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;length&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        itemBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; index) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors[index]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _photosRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With these changes, the app would look the same as Video 1.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;navigating-to-different-pages-using-pagecontroller&quot;&gt;Navigating to different pages using &lt;code&gt;PageController&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Now that we know the basics of using a &lt;code&gt;PageView&lt;&#x2F;code&gt;, we can now proceed on learning how to have more control with it. With &lt;code&gt;PageController&lt;&#x2F;code&gt;, we can programmatically move from one page to another without swiping the widget. To illustrate this feature, we will add a left and right button on the app and programatically move the pages.&lt;&#x2F;p&gt;
&lt;p&gt;For this example, I have to convert our &lt;code&gt;PageViewScreen&lt;&#x2F;code&gt; to be a &lt;code&gt;StatefulWidget&lt;&#x2F;code&gt; as we need to keep reference to our &lt;code&gt;PageController&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;cupertino.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatefulWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; createState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _PageViewScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _PageViewScreenState&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;PageViewScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _pageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; initState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _pageController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; PageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;initState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; dispose&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _pageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;dispose&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;dispose&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _photosRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Stack&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          PageView&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            controller&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _pageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            itemCount&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;length&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            itemBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; index) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;              return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; colors[index]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Align&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            alignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Alignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;centerLeft&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                _pageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;previousPage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 250&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  curve&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Curves&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;easeIn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;                CupertinoIcons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;chevron_left&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                size&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 32.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          Align&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            alignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Alignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;centerRight&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                _pageController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;nextPage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 250&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  curve&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Curves&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;easeIn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;                CupertinoIcons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;chevron_right&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                size&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 32.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _photosRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;red&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I added the two buttons by wrapping the &lt;code&gt;PageView&lt;&#x2F;code&gt; under a &lt;code&gt;Stack&lt;&#x2F;code&gt; and aligning the buttons to center left and right. If we want to navigate the screens to the previous or next page, we can use the &lt;code&gt;previousPage()&lt;&#x2F;code&gt; and &lt;code&gt;nextPage()&lt;&#x2F;code&gt; accordingly.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-pageview-in-flutter&#x2F;compressed-figure4-page-controller.mov.gif&quot; alt=&quot;compressed-figure4-page-controller.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 4. Transition using &lt;code&gt;PageController&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Calling these two methods requires a &lt;code&gt;Duration&lt;&#x2F;code&gt; and &lt;code&gt;Curve&lt;&#x2F;code&gt; arguments as &lt;code&gt;PageController&lt;&#x2F;code&gt; will animate the transition. You can also use &lt;code&gt;animateToPage()&lt;&#x2F;code&gt; if you know specifically which screen you want to navigate to. And if you want to set the page without animation, you can use the &lt;code&gt;jumpToPage()&lt;&#x2F;code&gt; method of the controller.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;PageView&lt;&#x2F;code&gt; widget offers an alternative way of displaying content in your app. With &lt;code&gt;PageView&lt;&#x2F;code&gt;, you can choose whether the pages are swipeable from left to right or top to bottom.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;PageView&lt;&#x2F;code&gt; also lets you add more controls by using &lt;code&gt;PageController&lt;&#x2F;code&gt;, as well as different options in building your pages depending on the number of items that you have.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;PageView-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;PageView-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Flutter navigation with go_router package</title>
        <published>2024-02-15T00:00:00+00:00</published>
        <updated>2024-02-15T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/flutter-navigation-with-go-router-package/"/>
        <id>https://rafaelds.com/posts/flutter-navigation-with-go-router-package/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/flutter-navigation-with-go-router-package/">&lt;p&gt;Apps of different sizes and scale have more than one screen to display from the main screen to the other parts of the app. One of the most used libraries for navigation in Flutter is &lt;code&gt;go_router&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In this tutorial, we will learn how to use &lt;code&gt;go_router&lt;&#x2F;code&gt; in our Flutter app to move from one screen to another.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;installation&quot;&gt;Installation&lt;&#x2F;h1&gt;
&lt;p&gt;To add the &lt;code&gt;go_router&lt;&#x2F;code&gt; package, use the command:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add go_router&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another way to install manually is to add the following to your &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;    sdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; flutter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  cupertino_icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^1.0.2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  go_router&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^13.2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; # &amp;lt; Add this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that everything has been set up, let&#x27;s go with the configuration.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;define-our-screens&quot;&gt;Define our Screens&lt;&#x2F;h1&gt;
&lt;p&gt;In our example, we can have three hypothetical screens: &lt;code&gt;Splash screen&lt;&#x2F;code&gt;, &lt;code&gt;Home screen&lt;&#x2F;code&gt;, and  &lt;code&gt;Settings screen&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;screen-flow-navigation.png&quot; alt=&quot;screen-flow-navigation&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 1. Screens to use in our navigation example.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; splash_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;amber[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Splash&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;home_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      appBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; AppBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        actions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;              &#x2F;&#x2F;TODO:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;settings)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Home&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; settings_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;              const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Settings&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;              OutlinedButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;                  &#x2F;&#x2F;TODO:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Go Back&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;route-configuration&quot;&gt;Route Configuration&lt;&#x2F;h1&gt;
&lt;p&gt;We&#x27;ll start with the basics first. Let&#x27;s say we have an app with multiple screens. In this case, we have a splash screen, a home screen, and a settings screen.&lt;&#x2F;p&gt;
&lt;p&gt;Whenever we launch our application, we see the settings screen first and should navigate next to the home screen.&lt;&#x2F;p&gt;
&lt;p&gt;To set this up, we need to create a &lt;code&gt;GoRouter&lt;&#x2F;code&gt; configuration class.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;router_config.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:navigation_with_go_router&#x2F;home_screen.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:navigation_with_go_router&#x2F;settings_screen.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:navigation_with_go_router&#x2F;splash_screen.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;GoRouter&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; routerConfig &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; GoRouter&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  routes&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;: &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;RouteBase&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;        return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;home&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;        return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;settings&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;        return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can put this anywhere in the project, but I would personally put this on a separate file such as &lt;code&gt;router_config.dart&lt;&#x2F;code&gt; for code separation.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;GoRouter&lt;&#x2F;code&gt; class has a parameter &lt;code&gt;routes:&lt;&#x2F;code&gt; where we can define a list of paths or destinations to our app navigation. The &lt;code&gt;path&lt;&#x2F;code&gt; with a &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; value means that this is the starting screen of the app. In this case, we show the &lt;code&gt;SplashScreen&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The other destinations are &lt;code&gt;HomeScreen&lt;&#x2F;code&gt; and &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt; which are reached through paths &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;settings&lt;&#x2F;code&gt; respectively.&lt;&#x2F;p&gt;
&lt;p&gt;The next thing to do is to set up our app to use this route configuration. This is done by changing either &lt;code&gt;MaterialApp&lt;&#x2F;code&gt; or &lt;code&gt;CupertinoApp&lt;&#x2F;code&gt; to use the router constructors: &lt;code&gt;MaterialApp.router()&lt;&#x2F;code&gt;  or &lt;code&gt;CupertinoApp.router()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In the following example, &lt;code&gt;MyApp&lt;&#x2F;code&gt; is using a &lt;code&gt;MaterialApp&lt;&#x2F;code&gt;, so I will use its equivalent constructor for router.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;my_app.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:navigation_with_go_router&#x2F;router_config.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;router&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      debugShowCheckedModeBanner&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      routerConfig&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; routerConfig&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lastly, make sure that  &lt;code&gt;main.dart&lt;&#x2F;code&gt; is loading up &lt;code&gt;MyApp&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;main.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:navigation_with_go_router&#x2F;my_app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running the app will default to showing our splash screen.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;splash-home-screen.png&quot; alt=&quot;splash-home-screen&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 2. The first screen of the app (Splash screen).&lt;&#x2F;p&gt;
&lt;h1 id=&quot;navigating-from-one-screen-to-another&quot;&gt;Navigating from one screen to another&lt;&#x2F;h1&gt;
&lt;p&gt;There are two basic ways to navigate from one screen to another. One is using &lt;code&gt;context.push()&lt;&#x2F;code&gt; and the other is &lt;code&gt;context.go()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-context-push&quot;&gt;Using context.push()&lt;&#x2F;h2&gt;
&lt;p&gt;Using the &lt;code&gt;context.push()&lt;&#x2F;code&gt; API is straightforward. The destination is added on top of the current screen. Let&#x27;s see what it looks like if we navigate from &lt;code&gt;SplashScreen&lt;&#x2F;code&gt; to &lt;code&gt;HomeScreen&lt;&#x2F;code&gt; using this method.&lt;&#x2F;p&gt;
&lt;p&gt;To use this method, we need to have access to &lt;code&gt;context&lt;&#x2F;code&gt; and the path of the destination screen. To go to &lt;code&gt;HomeScreen&lt;&#x2F;code&gt;, we need to use &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; as defined in our &lt;code&gt;routes_config&lt;&#x2F;code&gt;. Let&#x27;s add a tap capability in our screen to trigger a navigation using the &lt;code&gt;TapRegion&lt;&#x2F;code&gt; widget .&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; splash_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapRegion&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onTapInside&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;push&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&#x2F;home&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;amber[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Splash&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we tap on any part of the the &lt;code&gt;SplashScreen&lt;&#x2F;code&gt;, it will trigger &lt;code&gt;context.push(&#x27;&#x2F;home&#x27;)&lt;&#x2F;code&gt; and navigate to &lt;code&gt;HomeScreen&lt;&#x2F;code&gt;. Swiping back or tapping the back button navigates back to the &lt;code&gt;SplashScreen&lt;&#x2F;code&gt;. It works like this because using the &lt;code&gt;context.push()&lt;&#x2F;code&gt; method retains the stack order of the screens.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;compressed-splash-to-home-navigation.mov.gif&quot; alt=&quot;compressed-splash-to-home-navigation.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 1. Navigating to Home Screen and back to Splash Screen.&lt;&#x2F;p&gt;
&lt;p&gt;But splash screens are not supposed to be viewable again after being shown to the users. For use cases like this, we should use &lt;code&gt;context.go()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-context-go&quot;&gt;Using context.go()&lt;&#x2F;h2&gt;
&lt;p&gt;The difference between &lt;code&gt;context.go()&lt;&#x2F;code&gt; and &lt;code&gt;context.push()&lt;&#x2F;code&gt; is that &lt;code&gt;context.go()&lt;&#x2F;code&gt; clears the stack of screens while &lt;code&gt;context.push()&lt;&#x2F;code&gt; does not.&lt;&#x2F;p&gt;
&lt;p&gt;In our example, since we want to clear the &lt;code&gt;SplashScreen&lt;&#x2F;code&gt; after navigating out of it, then we should be using &lt;code&gt;context.go(&#x27;&#x2F;home&#x27;)&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; splash_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SplashScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapRegion&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onTapInside&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;go&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&#x2F;home&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; &amp;lt;&amp;lt; Use &amp;#39;go&amp;#39; in here instead of &amp;#39;push&amp;#39;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;amber[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Splash&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running this change should not let you go back to the previous screen.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;home-screen-without-stack.png&quot; alt=&quot;home-screen-without-stack&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 3. Navigating to the home screen using &lt;code&gt;context.go()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Note that the &lt;code&gt;leadingWidget&lt;&#x2F;code&gt; of the &lt;code&gt;AppBar&lt;&#x2F;code&gt; disappears as &lt;code&gt;MaterialApp&lt;&#x2F;code&gt; knows that there&#x27;s no screen to come back to when using &lt;code&gt;context.go()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;going-back-to-the-previous-screen&quot;&gt;Going back to the previous screen&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s complete the app by adding an option to navigate to the &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt; from the &lt;code&gt;HomeScreen&lt;&#x2F;code&gt;. This time, we should be able to go back to the previous screen.&lt;&#x2F;p&gt;
&lt;p&gt;Add the &lt;code&gt;context.push()&lt;&#x2F;code&gt; to the  &lt;code&gt;onPressed&lt;&#x2F;code&gt; callback of the settings icon of &lt;code&gt;HomeScreen&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;home_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HomeScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      appBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; AppBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        actions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;          IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;push&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&#x2F;settings&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;settings)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;green[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Home&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To go back to the previous screen, we can either use a swipe back gesture or tap the back button similar to Video 1. You can also do this programmatically using the &lt;code&gt;context.pop()&lt;&#x2F;code&gt; button.&lt;&#x2F;p&gt;
&lt;p&gt;Make sure that there is someplace to go back to. It means that the current screen should have been navigated by using &lt;code&gt;context.push()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; settings_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            mainAxisSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;              const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Settings&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;              OutlinedButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pop&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; &amp;lt;&amp;lt; Add this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Go Back&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run the app, we should see the expected behaviour of the navigation. The &lt;code&gt;SplashScreen&lt;&#x2F;code&gt; should only be displayed once and not be navigated back again. &lt;code&gt;HomeScreen&lt;&#x2F;code&gt; should be able to open the &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt; and go back.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;compressed-splash-to-home-to-settings.mov.gif&quot; alt=&quot;compressed-splash-to-home-to-settings.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 2. Navigating from Splash screen to Home screen, Settings screen, and back.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;passing-parameters&quot;&gt;Passing parameters&lt;&#x2F;h1&gt;
&lt;p&gt;With &lt;code&gt;go_router&lt;&#x2F;code&gt;, we can pass parameters when navigating to different screens. There are different ways to do this.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;path-parameters&quot;&gt;Path parameters&lt;&#x2F;h2&gt;
&lt;p&gt;Using path parameters is a straightforward way of passing data from one screen to another. This is done by appending a path variable to our &lt;code&gt;path&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Say we want to pass an ID from &lt;code&gt;HomeScreen&lt;&#x2F;code&gt; to the &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt;. We can achieve this by adding a &lt;code&gt;:id&lt;&#x2F;code&gt; variable to our path in our &lt;code&gt;routerConfig&lt;&#x2F;code&gt;. Let&#x27;s also update &lt;code&gt;HomeScreen&lt;&#x2F;code&gt; to pass a value for the id (e.g. 42). Finally, we also update &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt; to accept an &lt;code&gt;id&lt;&#x2F;code&gt; String value and display it once loaded.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;part of router_config.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;   path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;settings&#x2F;:id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;   builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;     final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;pathParameters[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;??&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;     return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;       id&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;     )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;   }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; part of home_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;push&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&#x2F;settings&#x2F;42&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;settings)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; settings_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:go_router&#x2F;go_router.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; required&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;id})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Container&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blue[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            mainAxisSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;              Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Settings - ID: $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;id&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;              OutlinedButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pop&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Go Back&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To get the value of the parameter from the path, we use the &lt;code&gt;state&lt;&#x2F;code&gt; parameter of the &lt;code&gt;GoRoute&lt;&#x2F;code&gt; builder function. This &lt;code&gt;GoRouterState&lt;&#x2F;code&gt; has a property called &lt;code&gt;pathParameters&lt;&#x2F;code&gt; which returns a map of parameters. In our example, we use &lt;code&gt;id&lt;&#x2F;code&gt; to retrieve this value.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;pass-parameter-path.png&quot; alt=&quot;pass-parameter-path&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 4. Passing data to another screen with path parameters.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;query-parameters&quot;&gt;Query parameters&lt;&#x2F;h2&gt;
&lt;p&gt;We can also use query parameters for passing data in between screens. The approach however is different with path parameters. With query parameters, we do not need to update the path of a &lt;code&gt;GoRoute&lt;&#x2F;code&gt;. Instead, we can just check whether there is a value passed with a given key using &lt;code&gt;uri.queryParameters&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using our current example, let&#x27;s include a query parameter called &lt;code&gt;queryParam&lt;&#x2F;code&gt; when navigating to &lt;code&gt;SettingsScreen&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;part of home_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;push&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&#x2F;settings&#x2F;42?queryParam=mango&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;settings)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;part of router_config.dart    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;settings&#x2F;:id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;		final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;pathParameters[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;??&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;		final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; queryParam &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;uri&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;queryParameters[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;queryParam&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;		return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      id&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;id&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;queryParam&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this update, we should see the &lt;code&gt;queryParam&lt;&#x2F;code&gt; with the value &lt;code&gt;mango&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;pass-parameter-query.png&quot; alt=&quot;pass-parameter-query&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 5. Passing data to another screen with query parameters.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;object-parameter&quot;&gt;Object parameter&lt;&#x2F;h2&gt;
&lt;p&gt;It is best practice to only pass data between screens using data type &lt;code&gt;String&lt;&#x2F;code&gt;. However, if your app requires passing an object, you can use the &lt;code&gt;extra&lt;&#x2F;code&gt; argument when navigating to another screen.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;part of home_screen.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;	IconButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    	context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;push&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;      	&amp;#39;&#x2F;settings&#x2F;42?queryParam=mango&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        extra&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 30.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; &amp;lt;&amp;lt; Extra object&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;settings)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;part of router_config.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;GoRoute&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	path&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&#x2F;settings&#x2F;:id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;		final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;pathParameters[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;id&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;??&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;		final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; queryParam &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;uri&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;queryParameters[&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;queryParam&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; value &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; state&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;extra &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; double&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; &amp;lt;&amp;lt; Cast the extra with the right class&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SettingsScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    	id&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;id&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;queryParam&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;value&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-navigation-with-go-router-package&#x2F;pass-parameter-extra.png&quot; alt=&quot;pass-parameter-extra&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 6. Passing non-String data to another screen&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;That&#x27;s all the basic things that you need to learn in order to start using &lt;code&gt;go_router&lt;&#x2F;code&gt; as your navigation library. There are many other features that can be used from this library such as &lt;code&gt;ShellRoutes&lt;&#x2F;code&gt; for multiple navigation trees, as well as custom animations in-between screens.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pub.dev&#x2F;packages&#x2F;go_router&quot;&gt;https:&#x2F;&#x2F;pub.dev&#x2F;packages&#x2F;go_router&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Build a simple game with Flutter and Flame</title>
        <published>2024-02-06T00:00:00+00:00</published>
        <updated>2024-02-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/build-a-simple-game-with-flutter-and-flame/"/>
        <id>https://rafaelds.com/posts/build-a-simple-game-with-flutter-and-flame/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/build-a-simple-game-with-flutter-and-flame/">&lt;p&gt;Flutter is a powerful development framework in building applications across different platform. It is very versatile that we can even make a game with it.&lt;&#x2F;p&gt;
&lt;p&gt;In this tutorial, we will learn how to build a very basic game in Flutter using the Flame game engine. We will go through each step in building this 2D game called Bug Squash, in which you try to squash as many bugs as you can.&lt;&#x2F;p&gt;
&lt;p&gt;We will start by setting up the project, add game sprites to represent the bugs, build the user-interface, and eventually, add the game logic. This article should help you learn the basics of Flame so that you can make more complex games moving forward.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-final-app-animation.mov.gif&quot; alt=&quot;compressed-final-app-animation.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-is-flame&quot;&gt;What is Flame?&lt;&#x2F;h1&gt;
&lt;p&gt;Flame is a game engine built for Flutter that provides a complete set of solutions for building different types of games. It contains all the necessary features to build a game such as its own game loop, sprites, animations, input, and collision detection.&lt;&#x2F;p&gt;
&lt;p&gt;The Flame engine follows a component system (similar to Flutter&#x27;s widget tree) called &lt;code&gt;Flame Component System (FCS)&lt;&#x2F;code&gt;. While this sounds like a lot, we will only be using a few of the components in this system to build our game.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-bug-squash-game&quot;&gt;The Bug Squash game&lt;&#x2F;h1&gt;
&lt;p&gt;I was inspired to build this simple game when the summer season arrived here in Australia. This warm weather comes with a bunch of real-life bugs crawling in our home trying to escape the heat outside. So instead of removing real life bugs (and software bugs), let&#x27;s build a game where we can squash bugs.&lt;&#x2F;p&gt;
&lt;p&gt;Before we write our code, let&#x27;s first think about what our game should be, how it should work, and how it would look like. I want this game to be really simple so that we can understand how to use Flame in Flutter.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;bug-squash-game-design.png&quot; alt=&quot;image-20240128133436276&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 1. Bug squash game design.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This game is a straight forward squash as many bugs as you can type of game. These critters will appear in the screen and to get points, you need to click or tap the bugs.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;project-setup&quot;&gt;Project Setup&lt;&#x2F;h1&gt;
&lt;p&gt;Create the project by either using the terminal with &lt;code&gt;flutter create bug_squash --org com.yourname&lt;&#x2F;code&gt; or using Visual Studio Code&#x27;s &lt;code&gt;Flutter: New Project &amp;gt; Empty Application&lt;&#x2F;code&gt;. Regardless, we want a clean starter project to begin with.&lt;&#x2F;p&gt;
&lt;p&gt;&#x2F;&#x2F;Insert link to setting up a Flutter project blog post&lt;&#x2F;p&gt;
&lt;h2 id=&quot;install-flame&quot;&gt;Install Flame&lt;&#x2F;h2&gt;
&lt;p&gt;Add the &lt;code&gt;Flame&lt;&#x2F;code&gt; library by doing running &lt;code&gt;flutter pub add flame&lt;&#x2F;code&gt; command in the terminal to get the latest version. Doing so will add the dependency in your project&#x27;s &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;. Another option is by directly updating the &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; and adding Flame as part of the dependencies, which at the time of writing is at version &lt;code&gt;1.14.0&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add flame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# pubspec.yaml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flame&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^1.14.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;    sdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; flutter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you run into some version conflict problems, you may run &lt;code&gt;flutter pub upgrade --major-versions&lt;&#x2F;code&gt; to upgrade, and then try adding the Flame library again.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clean-up-main-dart&quot;&gt;Clean up main.dart&lt;&#x2F;h2&gt;
&lt;p&gt;It is much easier to learn Flame if we remove the clutter in our app. Start by making sure that &lt;code&gt;main.dart&lt;&#x2F;code&gt; only has the &lt;code&gt;runApp()&lt;&#x2F;code&gt; function just to be sure that we are starting out from scratch. Then, let&#x27;s add the minimum requirements for building a game using Flame: The &lt;code&gt;GameWidget&lt;&#x2F;code&gt; and the &lt;code&gt;FlameGame&lt;&#x2F;code&gt;. I will discuss these two components later but for now, make sure you have your &lt;code&gt;main.dart&lt;&#x2F;code&gt; written up like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;main.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    GameWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      game&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;run-the-app-on-your-device&quot;&gt;Run the app on your device&lt;&#x2F;h2&gt;
&lt;p&gt;You can use whichever device you want to test your game. I will be using an iPhone simulator and make sure that our code builds successfully.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;hello-world-bug-squash.png&quot; alt=&quot;hello-world-bug-squash&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 2.1 Hello world of Bug Squash game&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Alright! We have a running Flutter app using Flame as its game engine. It does not show anything, but we will eventually build our way up to our beautiful game.&lt;&#x2F;p&gt;
&lt;p&gt;Now that our app is running, let&#x27;s start building Bug Squash!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;gamewidget-and-flamegame&quot;&gt;GameWidget and FlameGame&lt;&#x2F;h1&gt;
&lt;p&gt;The fundamental widget used in building a game on Flutter with Flame is the &lt;code&gt;GameWidget&lt;&#x2F;code&gt;. Because it is a &lt;code&gt;Widget&lt;&#x2F;code&gt;, we can insert a Flame game anywhere in the widget tree of any Flutter app. We can think of &lt;code&gt;GameWidget&lt;&#x2F;code&gt; as a container for another important component of Flame called &lt;code&gt;Game&lt;&#x2F;code&gt;. A &lt;code&gt;Game&lt;&#x2F;code&gt; is an interface in which the game itself gets rendered. A ready made implementation of &lt;code&gt;Game&lt;&#x2F;code&gt; that we will use in this project is the &lt;code&gt;FlameGame&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;building-our-own-game-instance&quot;&gt;Building our own Game instance&lt;&#x2F;h2&gt;
&lt;p&gt;We want to add bugs, animations, and game logic in Bug Squash. The best way to do it is to create a class that extends from &lt;code&gt;FlameGame&lt;&#x2F;code&gt;. Let&#x27;s create a class called &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; that extends &lt;code&gt;FlameGame&lt;&#x2F;code&gt;. Replace the &lt;code&gt;game:&lt;&#x2F;code&gt; instance in our &lt;code&gt;main.dart&lt;&#x2F;code&gt; and run the app.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; main.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug_squash_game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    GameWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      game&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; Use our BugSquashGame&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once we have this set up, we can start adding our game logic within the &lt;code&gt;BugSquashgame&lt;&#x2F;code&gt; class. If we run the app again, we will see the same screen as seen in Figure 1.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;changing-the-background-colour&quot;&gt;Changing the background colour&lt;&#x2F;h2&gt;
&lt;p&gt;We can change the default background colour by overriding the &lt;code&gt;backgroundColor&lt;&#x2F;code&gt; property of &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We should see our game change its background to green.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;bug-squash-with-background-color.png&quot; alt=&quot;bug-squash-with-background-color&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 2.2. Updated background colour.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;loading-components&quot;&gt;Loading components&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;code&gt;Components&lt;&#x2F;code&gt; in Flame represent objects that we can either see or interact within the game. In Bug Squash, we will be dealing with &lt;code&gt;components&lt;&#x2F;code&gt; such as sprites that are moving around the screen in which we can squash when we tap on them. In order to load components in our game, we have to be familiar with Flame&#x27;s lifecycle.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;flame-game-loop.png&quot; alt=&quot;flame-game-loop&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 3. The Game class lifecycle. &lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;All instances of &lt;code&gt;Game&lt;&#x2F;code&gt; follows the lifecycle represented in Figure 3. For now, let&#x27;s focus on the &lt;code&gt;onLoad&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;onLoad&lt;&#x2F;code&gt; method is where we initialise the components that we want to see in our game. If we want to add a sprite of a bug in our game, the &lt;code&gt;onLoad&lt;&#x2F;code&gt; method is the best place to do so.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;set-up-the-assets-directory&quot;&gt;Set up the assets directory&lt;&#x2F;h2&gt;
&lt;p&gt;Flame has a proposed file structure in organising our game&#x27;s assets such as videos, images, and audio. In the root directory of your project, create a folder called &lt;code&gt;assets&lt;&#x2F;code&gt; and make sure you have the following structure:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── assets&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── audio&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    │   └── squash.mp3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── images&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    		└── bug.png&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    		└── squashed_bug.png&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can download the assets that I used for this game using this link, or feel free to use your own sound effects and images.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t forget to add these in your &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; to make sure that these assets are accessible from your app.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# pubspec.yaml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  uses-material-design&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  assets&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;    -&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; assets&#x2F;images&#x2F;bug.png&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;    -&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; assets&#x2F;images&#x2F;squashed_bug.png&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;    -&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; assets&#x2F;audio&#x2F;squash.mp3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;adding-sprites&quot;&gt;Adding sprites&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s add a bug component in our game. We will be using a component called &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; to represent our bug. A &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; lets us use an image which will be rendered in our game. We learned that we can initialise and load this image in the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method of our &lt;code&gt;BugSquash&lt;&#x2F;code&gt; game, so let&#x27;s do that.&lt;&#x2F;p&gt;
&lt;p&gt;We override the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method in our &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; so we can load the bug sprite. Since we want to make sure that the image &lt;code&gt;bug.png&lt;&#x2F;code&gt; has been loaded correctly, we need to make the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method an &lt;code&gt;async&lt;&#x2F;code&gt; method and wait for the image to load first.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We first instantiate a &lt;code&gt;Component&lt;&#x2F;code&gt;. A &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; requires a &lt;code&gt;Sprite&lt;&#x2F;code&gt; instance, therefore we instantiate the &lt;code&gt;bugSprite&lt;&#x2F;code&gt; first before building the &lt;code&gt;Component&lt;&#x2F;code&gt;. It can be added into the game by using the &lt;code&gt;Game&lt;&#x2F;code&gt; interface method called &lt;code&gt;add()&lt;&#x2F;code&gt;. Take note that the &lt;code&gt;add()&lt;&#x2F;code&gt; method can be called anywhere in the lifecycle of the &lt;code&gt;Game&lt;&#x2F;code&gt;, and will be displayed on the screen once the &lt;code&gt;render()&lt;&#x2F;code&gt; method of the &lt;code&gt;Game&lt;&#x2F;code&gt; and &lt;code&gt;Component&lt;&#x2F;code&gt; has been called.&lt;&#x2F;p&gt;
&lt;p&gt;If we run the updated &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;, we should be able to see our bug sprite on the screen.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;sprite-component-rendered.png&quot; alt=&quot;sprite-component-rendered&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 4. Loading bug image in the game with SpriteComponent. &lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So now we have successfully added the bug sprite. It may or may not be the position where we want to put this bug, but at least it&#x27;s there.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;position-of-components&quot;&gt;Position of Components&lt;&#x2F;h1&gt;
&lt;p&gt;A &lt;code&gt;Component&lt;&#x2F;code&gt; can be set to a particular location of the screen as long as it is a type of &lt;code&gt;PositionComponent&lt;&#x2F;code&gt;. A &lt;code&gt;PositionComponent&lt;&#x2F;code&gt; represents an object that can be be moved around the screen. It can also be rotated, translated, and scaled accordingly.&lt;&#x2F;p&gt;
&lt;p&gt;Many of the &lt;code&gt;Components&lt;&#x2F;code&gt; in Flame including the &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; extends &lt;code&gt;PositionComponent&lt;&#x2F;code&gt;, which means that we can move this bug around.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;x-y-axis&quot;&gt;X-Y Axis&lt;&#x2F;h2&gt;
&lt;p&gt;Any position in the screen can be represented by a point defined by its x and y coordinates. The &lt;code&gt;0&lt;&#x2F;code&gt; position of the X-axis starts from the left, whilst the &lt;code&gt;0&lt;&#x2F;code&gt; of Y-axis starts at the top.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;coordinate-system-in-flame.png&quot; alt=&quot;image-20240128193810091&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 5. Coordinate system of Flame&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;When we loaded the &lt;code&gt;bugComponent&lt;&#x2F;code&gt; in our game, its properties assumed the default values. The default position of a component is &lt;code&gt;(0,0)&lt;&#x2F;code&gt; or &lt;code&gt;x = 0&lt;&#x2F;code&gt; and &lt;code&gt;y = 0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setting-a-component-s-position&quot;&gt;Setting a component&#x27;s position&lt;&#x2F;h2&gt;
&lt;p&gt;To set the position of the &lt;code&gt;Component&lt;&#x2F;code&gt;, we update the &lt;code&gt;PositionComponent&#x27;s&lt;&#x2F;code&gt; property called &lt;code&gt;position&lt;&#x2F;code&gt; and assign a &lt;code&gt;Vector2&lt;&#x2F;code&gt; value on it where the first argument is the &lt;code&gt;x-coordinate&lt;&#x2F;code&gt; whilst the second is the &lt;code&gt;y-coordinate&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;100&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 300&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;Set the position&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we set the x-coordinate to be &lt;code&gt;100&lt;&#x2F;code&gt; and the y-coordinate to be &lt;code&gt;300&lt;&#x2F;code&gt;, then we expect the bug to be located at this part of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;sprite-component-updated-position.png&quot; alt=&quot;sprite-component-updated-position&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 6. Updating the position of the sprite component&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If the &lt;code&gt;GameWidget&lt;&#x2F;code&gt; is the root widget of your app, then the &lt;code&gt;Game&lt;&#x2F;code&gt; fills up the space that it can. In our &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;, we can determine the size of the screen by getting the value of &lt;code&gt;size&lt;&#x2F;code&gt; which is a type of &lt;code&gt;Vector2&lt;&#x2F;code&gt;. We can use this value as a reference to position any &lt;code&gt;component&lt;&#x2F;code&gt; anywhere within the bounds of the game&#x27;s size.&lt;&#x2F;p&gt;
&lt;p&gt;It means that if we want to position our component at the centre of the screen, we can set the position at the half point.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;size is a `Game` property&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if we update our code and run it, the bug is not quite at the centre as you can see in Figure 7.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;bug-not-in-the-centre.png&quot; alt=&quot;bug-not-in-the-centre&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 7. Bug not in the centre&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The reason is because we have to consider another property of a &lt;code&gt;component&lt;&#x2F;code&gt; called &lt;code&gt;anchor&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setting-a-component-s-anchor&quot;&gt;Setting a component&#x27;s anchor&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;anchor&lt;&#x2F;code&gt; of a &lt;code&gt;component&lt;&#x2F;code&gt; is where this component is referenced and measured from in terms of its position. We can also think of the anchor as the point where we can rotate it. By default, the anchor of a component is at &lt;code&gt;(0, 0)&lt;&#x2F;code&gt;, or &lt;code&gt;top-left&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;anchor-axis.png&quot; alt=&quot;image-20240128230754748&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 8. Anchor of a component&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It is common to always set the &lt;code&gt;anchor&lt;&#x2F;code&gt; of a component to &lt;code&gt;center&lt;&#x2F;code&gt; to make it more intuitive when positioning components across the screen. To do so, we can set it by using the provided &lt;code&gt;Anchor&lt;&#x2F;code&gt; constants.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run with this updated code, we should be able to see the component exactly in the middle of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;bug-in-the-centre.png&quot; alt=&quot;bug-in-the-centre&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;sub&gt;Figure 9. Bug in the center of the screen&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We should now know how to place any components in our game within the screen. Understanding both position and anchor should save you the headache in the long run when you are designing your own game later.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;adding-tap-capability&quot;&gt;Adding tap capability&lt;&#x2F;h1&gt;
&lt;p&gt;Our game is squashing bugs, let&#x27;s add a feature to handle tap events. By default, the &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; and other &lt;code&gt;Component&lt;&#x2F;code&gt; classes do not accept tap inputs. The way to add this capability is by creating our own &lt;code&gt;Component&lt;&#x2F;code&gt; and implementing the mixin called &lt;code&gt;TapCallbacks&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-your-own-components&quot;&gt;Creating your own Components&lt;&#x2F;h2&gt;
&lt;p&gt;First, let&#x27;s create a class that extends &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; and call it &lt;code&gt;Bug&lt;&#x2F;code&gt;. &lt;code&gt;Components&lt;&#x2F;code&gt; have similar lifecycle as a &lt;code&gt;Game&lt;&#x2F;code&gt;, in which we put initialisation of properties within the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of building the bug &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; from the &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;, we can contain this initialisation within the new &lt;code&gt;Bug&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we know that a &lt;code&gt;Bug&lt;&#x2F;code&gt;  will always have the &lt;code&gt;bug.png&lt;&#x2F;code&gt; sprite, we can initialise this sprite during the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method of  our &lt;code&gt;Bug&lt;&#x2F;code&gt; component. This &lt;code&gt;Bug&lt;&#x2F;code&gt; can now be instantiated anywhere in the app, but for now, let&#x27;s create one in &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;particles.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run the updated code, we should see the same result as Figure 9. Nothing has changed except that we have a more customisable &lt;code&gt;Bug&lt;&#x2F;code&gt; component.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementing-the-tapcallbacks-mixin&quot;&gt;Implementing the TapCallbacks mixin&lt;&#x2F;h2&gt;
&lt;p&gt;To make our component tappable, we need to implement the &lt;code&gt;TapCallbacks&lt;&#x2F;code&gt; mixin. This interface has few methods we can use to handle inputs such as tapping down, tapping up, long tap, and even cancelling the tap. In our Bug Squash game, we will just handle the event of tapping down the bug.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Write logic when the bug is tapped.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;adding-sound-effects&quot;&gt;Adding sound effects&lt;&#x2F;h1&gt;
&lt;p&gt;Now to the fun part, let&#x27;s add a sound effect whenever we tap the bug on the screen. Make sure that you have added the &lt;code&gt;squash.mp3&lt;&#x2F;code&gt; audio file in your project, as well as putting it in the correct directory. See [set up the assets section](#Set up the assets directory) to be sure.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, &lt;code&gt;Flame&lt;&#x2F;code&gt; does not have an audio player built in the same library. We have to add this separately in our project.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; pub add flame_audio&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This should add the dependency in your &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Then, all we need to do is to play this sound whenever we tap the bug.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    FlameAudio&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;play&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;squash.mp3&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you don&#x27;t hear any sound, try closing the app and then rebuild your code as &lt;code&gt;FlameAudio&lt;&#x2F;code&gt; requires some rebuilding when being added into a project.&lt;&#x2F;p&gt;
&lt;p&gt;You should now be able to hear the bug being squashed!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;displaying-the-bug-being-squashed&quot;&gt;Displaying the bug being squashed&lt;&#x2F;h1&gt;
&lt;p&gt;When we tap the bug, it is better to see the bug being squashed to inform us that we should squash the next bug. There are many ways we can do this, but I will show you one option.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;composition-of-components&quot;&gt;Composition of components&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;components&lt;&#x2F;code&gt; of Flame were designed to be composable just like Flutter widgets. A component can have multiple &lt;code&gt;children&lt;&#x2F;code&gt; components which can be grouped however it would make sense to you.&lt;&#x2F;p&gt;
&lt;p&gt;An example of this is the &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; that we have. This class extends from &lt;code&gt;FlameGame&lt;&#x2F;code&gt; which is, you guess it, is also a &lt;code&gt;component&lt;&#x2F;code&gt;. The &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; can contain multiple components such as &lt;code&gt;Bug&lt;&#x2F;code&gt;, &lt;code&gt;Score&lt;&#x2F;code&gt;, and other user interface that we want to add. Think of it as a container of components.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;components-children-diagram.png&quot; alt=&quot;components-children-diagram&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 10. Components having children components&lt;&#x2F;p&gt;
&lt;p&gt;We can use this idea to add an effect of the bug being squashed by adding a child component to the &lt;code&gt;Bug&lt;&#x2F;code&gt; component class. The &lt;code&gt;Bug&lt;&#x2F;code&gt; class can have a &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; child that has an image of a squashed bug. We use the  &lt;code&gt;add()&lt;&#x2F;code&gt; method of &lt;code&gt;Component&lt;&#x2F;code&gt; to add this sprite inside the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you run your project with the updated code above, you will see that the squashed bug is on top of our normal bug sprite. Adding a component always go on top of previously added components by default in that order. You can change this behaviour by updating the &lt;code&gt;priority&lt;&#x2F;code&gt; property of a &lt;code&gt;component&lt;&#x2F;code&gt; (the higher priority, the more front the component will be).&lt;&#x2F;p&gt;
&lt;p&gt;&amp;lt;img src=&quot;squashed-bug-over-bug.png&quot; alt=&quot;squashed-bug-over-bug&quot; style=&quot;zoom:100%;&quot; &#x2F;&amp;gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 11. Squashed bug&lt;&#x2F;p&gt;
&lt;p&gt;Our &lt;code&gt;Bug&lt;&#x2F;code&gt; component has a child &lt;code&gt;SpriteComponent&lt;&#x2F;code&gt; that shows a squashed bug. But this is not the behaviour that we want. We expect this sprite to only appear after we tap on the bug. That&#x27;s the game!&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s fix this by hiding the splatter, and only show it after tapping the bug. What we can do is to set the &lt;code&gt;opacity&lt;&#x2F;code&gt; of the splatter to be 0, so we don&#x27;t see it initially. Once we tap it, we can set the opacity to 100% so we can see it.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    FlameAudio&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;play&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;squash.mp3&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We made the &lt;code&gt;squashedBugComponent&lt;&#x2F;code&gt; to be a private variable inside the &lt;code&gt;Bug&lt;&#x2F;code&gt; class so that it can be accessed across different method. We initially set the &lt;code&gt;opacity&lt;&#x2F;code&gt; to 0 in &lt;code&gt;onLoad()&lt;&#x2F;code&gt; and set it to 100%  &lt;code&gt;opacity = 1&lt;&#x2F;code&gt; when we tap the bug. The visual effect should be better!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-tap-to-squish.mov.gif&quot; alt=&quot;compressed-tap-to-squish.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 1. The bug is squashed when tapped&lt;&#x2F;p&gt;
&lt;p&gt;As we can see in Video 1, the basic logic of our app is now working!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;animating-components-using-effects&quot;&gt;Animating Components using Effects&lt;&#x2F;h1&gt;
&lt;p&gt;A game is boring if nothing is moving around. Let&#x27;s solve this by using &lt;code&gt;Effects&lt;&#x2F;code&gt;. An &lt;code&gt;Effect&lt;&#x2F;code&gt; is a type of &lt;code&gt;Component&lt;&#x2F;code&gt; that changes its properties over time. Time moves forward when a game is running, and &lt;code&gt;Effects&lt;&#x2F;code&gt; use this to alter a &lt;code&gt;Component&lt;&#x2F;code&gt;&#x27;s properties such as angle, position, opacity, and more.&lt;&#x2F;p&gt;
&lt;p&gt;There are many types of &lt;code&gt;Effects&lt;&#x2F;code&gt;, but for this game, we will only use a few.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;moving-a-component-using-moveeffect&quot;&gt;Moving a Component using MoveEffect&lt;&#x2F;h2&gt;
&lt;p&gt;The simplest way to move a Component around is by using the &lt;code&gt;MoveEffect&lt;&#x2F;code&gt;.  This effect changes the position of a component in different ways. Let&#x27;s start with &lt;code&gt;MoveEffect.to()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;MoveEffect.to()&lt;&#x2F;code&gt; accepts two arguments;  a destination represented by a &lt;code&gt;Vector2&lt;&#x2F;code&gt; and &lt;code&gt;EffectController&lt;&#x2F;code&gt; which determines the time and approach a component would take to reach the destination.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try an example. Add the following code at the end of &lt;code&gt;onLoad()&lt;&#x2F;code&gt; function in &lt;code&gt;bug.dart&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Add move effect&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; destination &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;100&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 600&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; EffectController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      startDelay&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; moveEffect &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;to&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(destination&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(moveEffect)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;MoveEffect.to()&lt;&#x2F;code&gt; is a method to use if we want to change the position of a &lt;code&gt;Component&lt;&#x2F;code&gt; to a specific location on the screen using a &lt;code&gt;Vector2&lt;&#x2F;code&gt; type. The &lt;code&gt;EffectController&lt;&#x2F;code&gt; can be used to change how an &lt;code&gt;Effect&lt;&#x2F;code&gt; is done, whether it would repeat, reverse, how fast, and more.&lt;&#x2F;p&gt;
&lt;p&gt;Using the code above, the bug will start moving to position &lt;code&gt;x: 100 and y: 600&lt;&#x2F;code&gt; after &lt;code&gt;2 seconds&lt;&#x2F;code&gt;. The duration of this movement will last for 1 second.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-bug-movement.mov.gif&quot; alt=&quot;compressed-bug-movement.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 2. Bug movement (It&#x27;s stuttering due to .gif compression)&lt;&#x2F;p&gt;
&lt;p&gt;Of course, there are many other types of &lt;code&gt;MoveEffect&lt;&#x2F;code&gt; that you can use, so feel free to find out what works best for your game.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;changing-the-angle-of-a-component&quot;&gt;Changing the angle of a component&lt;&#x2F;h2&gt;
&lt;p&gt;The movement of the bug shown in Video 2 does not seem natural because the bug is facing up the entire time while moving.  Let&#x27;s change the angle of the bug to the direction of movement.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;relative-angles.png&quot; alt=&quot;relative-angles&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 12. Relative angles in radians&lt;&#x2F;p&gt;
&lt;p&gt;In Flame, the &lt;code&gt;angle&lt;&#x2F;code&gt; property of a &lt;code&gt;Component&lt;&#x2F;code&gt; is represented in &lt;code&gt;radians&lt;&#x2F;code&gt;. Since the bug is facing up, we can assume that the upwards direction is &lt;code&gt;0 radians&lt;&#x2F;code&gt;. Since it&#x27;s moving to the bottom-left, that direction is around &lt;code&gt;5 * pi &#x2F; 4&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Change the bug angle&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    angle &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; pi &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Add move effect&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; destination &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;100&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 600&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; EffectController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      startDelay&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; moveEffect &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;to&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(destination&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(moveEffect)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;removing-components&quot;&gt;Removing components&lt;&#x2F;h1&gt;
&lt;p&gt;Our game should have multiple bugs running around. Once they are squashed, they should disappear from the game. A convenient way to do this is to remove the bug &lt;code&gt;Component&lt;&#x2F;code&gt; from the game. This will also save your device&#x27;s memory in the long run the moment you have a lot of bugs running around the screen.&lt;&#x2F;p&gt;
&lt;p&gt;A parent &lt;code&gt;Component&lt;&#x2F;code&gt; has a method called &lt;code&gt;remove()&lt;&#x2F;code&gt; to dispose the a child &lt;code&gt;Component&lt;&#x2F;code&gt;. In our code, we can add a logic in our &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; to remove a &lt;code&gt;Bug&lt;&#x2F;code&gt; when tapped. We should also keep track of the &lt;code&gt;Bug&lt;&#x2F;code&gt; components that the game has added.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing we can change is how we set the &lt;code&gt;OnTapDown&lt;&#x2F;code&gt; method of &lt;code&gt;Bug&lt;&#x2F;code&gt;. We can keep the logic of playing the squash sound effect and making the visual effect appear. However, we need to set another logic to remove this &lt;code&gt;Bug&lt;&#x2F;code&gt; from the parent. We will do this by making the &lt;code&gt;onTapDown&lt;&#x2F;code&gt; method settable outside the class.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;effects.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Function&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Change the bug angle&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    angle &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; pi &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Add move effect&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; destination &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;100&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 600&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; EffectController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      startDelay&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _moveEffect &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;to&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(destination&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_moveEffect)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Stop the movement of the bug when tapped.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;_moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isPaused) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pause&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    FlameAudio&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;play&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;squash.mp3&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Call the onTap method set by the parent component&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;call&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;particles.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Remove the bug 500 ms after tapping&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;onTap &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 500&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;then&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        (value) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isRemoved) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;            remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We add a slight delay before removing the component so we can still see the squash effect and hear the sound before making the bug disappear from the screen. Also keep in mind that the bug should stop moving immediately after squashing the bug to make squashing the bug more believable.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-bug-squashed-and-removed.mov.gif&quot; alt=&quot;compressed-bug-squashed-and-removed.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 3. Bug stops and is removed after tapping.&lt;&#x2F;p&gt;
&lt;p&gt;The app should now behave as seen in Video 3. All the basic functions of our game has been implemented. We now know how to add, animate, and remove a component. For the next part of this tutorial, we will add the logic of adding multiple bugs on the screen and keeping a count of the number of bugs squashed.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;adding-more-bugs-to-squash&quot;&gt;Adding more bugs to squash&lt;&#x2F;h1&gt;
&lt;p&gt;To keep the game simple, we can add a timer function that will continuously add bugs in our game. Let&#x27;s add a limit so our game won&#x27;t be filled with thousands of bugs, unless that&#x27;s what you want in your game.&lt;&#x2F;p&gt;
&lt;p&gt;It is also important to note that we have not randomised the location where the bugs would appear, as well as their movement patterns. For now, let&#x27;s make the game easy and make the bugs appear from the bottom side, and will just walk straight to the top.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;move-the-bug-component-from-left-to-right&quot;&gt;Move the Bug Component from left to right&lt;&#x2F;h2&gt;
&lt;p&gt;If we want to make the &lt;code&gt;Bug&lt;&#x2F;code&gt; move across the screen from left to right, it needs to know the size of the game and how far it will travel. The &lt;code&gt;Bug&lt;&#x2F;code&gt;  component does not hold any information about its parent component. However, we know that &lt;code&gt;Bug&lt;&#x2F;code&gt; will always be part of our &lt;code&gt;BugSquash&lt;&#x2F;code&gt; game.&lt;&#x2F;p&gt;
&lt;p&gt;We can use a mixin called &lt;code&gt;HasGameReference&amp;lt;&amp;gt;&lt;&#x2F;code&gt; implemented by &lt;code&gt;Flame&lt;&#x2F;code&gt; to make our component know that it is part of a &lt;code&gt;Game&lt;&#x2F;code&gt; component. Let&#x27;s use this approach so that the &lt;code&gt;Bug&lt;&#x2F;code&gt; can access the &lt;code&gt;size&lt;&#x2F;code&gt; of the game from its properties.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;effects.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HasGameReference&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once &lt;code&gt;Bug&lt;&#x2F;code&gt; is implementing the &lt;code&gt;HasGameReference&amp;lt;BugSquashGame&amp;gt;&lt;&#x2F;code&gt;, we can use the reference &lt;code&gt;game&lt;&#x2F;code&gt; to update our &lt;code&gt;MoveEffect&lt;&#x2F;code&gt; to move by the game&#x27;s width. We should add the &lt;code&gt;Bug&lt;&#x2F;code&gt;&#x27;s width in this movement so that the bug disappears at the right side.&lt;&#x2F;p&gt;
&lt;p&gt;We will also have to clear this bug after the move effect is finished since they are not needed anymore.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug_squash_game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;effects.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HasGameReference&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Function&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;load&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;squashed_bug.png&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sprite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; squashedBugSprite)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_squashedBugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Set the bug angle to face right&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    angle &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; pi &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Set the bug position where it is not visible from the left&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; position&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Move the bug from left to right&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; destination &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(game&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; EffectController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      startDelay&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0.8&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _moveEffect &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MoveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;by&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(destination&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; effectController)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Remove this bug after movement is finished&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;onComplete &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      parent&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(this)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_moveEffect)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Stop the movement of the bug when tapped.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;_moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isPaused) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pause&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    FlameAudio&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;play&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;squash.mp3&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Call the onTap method set by the parent component&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;call&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; should be updated as well so that the bugs created are in a random y-position. The value of the y-position ranges from 0 to the game&#x27;s height. We set the angle of the &lt;code&gt;Bug&lt;&#x2F;code&gt; to be facing right so that the movement would look more natural.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;particles.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _createBug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Remove the bug 500 ms after tapping&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;onTap &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 500&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;then&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        (value) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isRemoved) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;            remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _createBug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; gameHeight &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; randomYPosition &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Random&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;nextDouble&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; gameHeight&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; randomYPosition)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;angle &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; pi &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we run the updated code, we should be seeing the bug crawling from left to right from a random y-position.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-bug-movement-left-to-right.mov.gif&quot; alt=&quot;compressed-bug-movement-left-to-right.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 4. Bug moving from left to right.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;adding-more-bugs&quot;&gt;Adding more bugs&lt;&#x2F;h2&gt;
&lt;p&gt;The last thing we want to do is to add a function that will continously add bugs. To keep it simple, we can add a bug every second until we close the app by using a &lt;code&gt;Timer&lt;&#x2F;code&gt; class from &lt;code&gt;Flame&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;Timer&lt;&#x2F;code&gt; class accepts an argument &lt;code&gt;limit&lt;&#x2F;code&gt; which defines the time interval in which function, &lt;code&gt;onTick&lt;&#x2F;code&gt;, will be called. For our use case, we can set the &lt;code&gt;limit&lt;&#x2F;code&gt; to 1.0 second. We should also set the &lt;code&gt;repeat&lt;&#x2F;code&gt; argument to &lt;code&gt;true&lt;&#x2F;code&gt;. The &lt;code&gt;onTick&lt;&#x2F;code&gt; function will be a function to add a &lt;code&gt;Bug&lt;&#x2F;code&gt; in the game.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-update-method&quot;&gt;Using the update() method&lt;&#x2F;h2&gt;
&lt;p&gt;Finally, we need to override the method &lt;code&gt;update()&lt;&#x2F;code&gt; from the &lt;code&gt;Game&lt;&#x2F;code&gt; class. The &lt;code&gt;update&lt;&#x2F;code&gt; method is called every game loop to give us an option on what to do with with the game based on the time elapsed. In this case, we want to create a &lt;code&gt;Bug&lt;&#x2F;code&gt; after 1 second has elapsed.&lt;&#x2F;p&gt;
&lt;p&gt;Since we are using the &lt;code&gt;Timer&lt;&#x2F;code&gt; class from &lt;code&gt;Flame&lt;&#x2F;code&gt;, all the implementation has been made for us, and we just need to call &lt;code&gt;Timer&lt;&#x2F;code&gt;&#x27;s &lt;code&gt;update()&lt;&#x2F;code&gt; function inside our &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;&#x27;s update method.&lt;&#x2F;p&gt;
&lt;p&gt;Following all these instructions, our new &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; will look like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:ui&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _interval&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _interval &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Timer&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; onTick&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _createBug&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; repeat&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; update&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;double&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; dt) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _interval&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;update&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(dt)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;update&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(dt)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;    return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Color&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0xFFEBFBEE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F; Function to be called in onTick() every 1 second&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _createBug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; bugComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; gameHeight &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; randomYPosition &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Random&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;nextDouble&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; gameHeight&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; randomYPosition)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;angle &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; pi &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;onTap &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 500&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;then&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        (value) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isRemoved) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;            remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that we have removed overriding the &lt;code&gt;onLoad()&lt;&#x2F;code&gt; function. We can do this because we create the &lt;code&gt;Bug&lt;&#x2F;code&gt; after the &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; has been created through the &lt;code&gt;_interval&lt;&#x2F;code&gt; timer variable. Now every second, a new &lt;code&gt;Bug&lt;&#x2F;code&gt; will be spawned randomly at the left side of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-many-bugs-moving.mov.gif&quot; alt=&quot;compressed-many-bugs-moving.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 5. Spawn bugs continuously.&lt;&#x2F;p&gt;
&lt;p&gt;Our game is now taking shape! There will be bugs crawling from left to right and it&#x27;s up to you to squash them as soon as you can.&lt;&#x2F;p&gt;
&lt;p&gt;The last thing that we will do is to add the number of bugs squashed somewhere on the screen.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;adding-the-number-of-bugs-squashed&quot;&gt;Adding the number of bugs squashed&lt;&#x2F;h1&gt;
&lt;p&gt;The last part of this tutorial is to add a counter for the number of bugs squashed. One way to do this is to use a &lt;code&gt;TextComponent&lt;&#x2F;code&gt; where you can render any text on your game. Since this is a &lt;code&gt;Component&lt;&#x2F;code&gt;, using it would be the same as how we did with our sprite and effects.&lt;&#x2F;p&gt;
&lt;p&gt;We add the scoring logic in the &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt; as this class has the general knowledge of how the game is played. The score can be stored in the &lt;code&gt;_score&lt;&#x2F;code&gt; variable, and is displayed by the &lt;code&gt;TextComponent&lt;&#x2F;code&gt; called &lt;code&gt;_scoreComponent&lt;&#x2F;code&gt;. Let&#x27;s bring the old &lt;code&gt;onLoad()&lt;&#x2F;code&gt; method to initialise the new component.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;initialising-the-textcomponent&quot;&gt;Initialising the TextComponent&lt;&#x2F;h2&gt;
&lt;p&gt;A &lt;code&gt;TextComponent&lt;&#x2F;code&gt; can be initialised by setting the text and its text style. It is good to note that it is a subclass of &lt;code&gt;PositionComponent&lt;&#x2F;code&gt;, which means that we can put this anywhere in the game by updating the &lt;code&gt;position&lt;&#x2F;code&gt; property.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;text.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FlameGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  late&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _scoreComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _score &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;	&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  FutureOr&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onLoad&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _scoreComponent &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      text&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;_score&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      textRenderer&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextPaint&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TextStyle&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(color&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;black&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; fontSize&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 24.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _scoreComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;anchor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Anchor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _scoreComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;position &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Vector2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(size&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    add&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_scoreComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;_scoreComponent&lt;&#x2F;code&gt; should be displayed at the top centre part of the game. Running this updated code should show you the default score of &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;game-with-default-score.png&quot; alt=&quot;game-with-default-score&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Figure 13. Game with default score of 0.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;updating-the-score-when-tapping-the-bug&quot;&gt;Updating the score when tapping the bug&lt;&#x2F;h2&gt;
&lt;p&gt;The last important bit that we have to do is to increment the score by &lt;code&gt;1&lt;&#x2F;code&gt; whenever we tap a bug. We can add this logic inside the &lt;code&gt;onTap&lt;&#x2F;code&gt; function that we set in the &lt;code&gt;Bug&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;bug_squash_game.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;		bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;onTap &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _scoreComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;text &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;${++&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;_score&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;}&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; Increment the score&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;      Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(milliseconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 500&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;then&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        (value) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;bugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isRemoved) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;            remove&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(bugComponent)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;rest of the code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will work fine. However, you will notice that if you tap on a &lt;code&gt;Bug&lt;&#x2F;code&gt; multiple times, the score increases as much. The expected behaviour should be to only increment the score by &lt;code&gt;1&lt;&#x2F;code&gt; per bug, not by the number of taps. The fix for this can be added inside the &lt;code&gt;Bug&lt;&#x2F;code&gt; class&#x27; &lt;code&gt;onTapDown&lt;&#x2F;code&gt; implementation.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; bug.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:async&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;dart:math&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:bug_squash_demo&#x2F;bug_squash_game.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;components.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;effects.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame&#x2F;events.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flame_audio&#x2F;flame_audio.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Bug&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SpriteComponent&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TapCallbacks&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; HasGameReference&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BugSquashGame&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  var&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _isAlive &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;status of the bug&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; onTapDown&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;TapDownEvent&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; event) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Stop the movement of the bug when tapped.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;_moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;isPaused) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _moveEffect&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pause&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    FlameAudio&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;play&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;squash.mp3&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    _squashedBugComponent&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;opacity &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Call the onTap method set by the parent component&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (_isAlive) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _isAlive &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      onTap&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;call&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;rest of the code..&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we tap the bug while it&#x27;s still alive, we should first set it to be dead by clearing the &lt;code&gt;_isAlive&lt;&#x2F;code&gt; flag. Only if it is still alive that we call the callback function &lt;code&gt;onTap()&lt;&#x2F;code&gt; that we set from the &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;. This way, the &lt;code&gt;onTap()&lt;&#x2F;code&gt; method won&#x27;t be spammed to inflate the score.&lt;&#x2F;p&gt;
&lt;p&gt;If I change the &lt;code&gt;_interval&lt;&#x2F;code&gt; of spawning bugs from 1 second to 0.4 seconds in &lt;code&gt;BugSquashGame&lt;&#x2F;code&gt;, the final result will look something like this.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;build-a-simple-game-with-flutter-and-flame&#x2F;compressed-final-app-animation.mov.gif&quot; alt=&quot;compressed-final-app-animation.mov&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Video 6. Final version of the app with spawn interval of 0.4 seconds&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;That&#x27;s all I have for this simple game of squashing bugs using &lt;code&gt;Flutter&lt;&#x2F;code&gt; and &lt;code&gt;Flame&lt;&#x2F;code&gt;. For now, we have a game where the goal is to squash as much bugs as you can as they appear from the left side of the screen.&lt;&#x2F;p&gt;
&lt;p&gt;One way to improve this game further is to have a more customised and randomised path for the bugs to crawl through. You can also add stages, timers, and even multiple screens if you want to.&lt;&#x2F;p&gt;
&lt;p&gt;I encourage you to learn more about &lt;code&gt;Flame&lt;&#x2F;code&gt; to make cross-platform games with &lt;code&gt;Flutter&lt;&#x2F;code&gt; and share it to the world.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.flame-engine.org&#x2F;latest&#x2F;&quot;&gt;https:&#x2F;&#x2F;docs.flame-engine.org&#x2F;latest&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>My 2024 plans and goals for rafaelds</title>
        <published>2024-01-30T00:00:00+00:00</published>
        <updated>2024-01-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/my-2024-plans-and-goals-for-rafaelds/"/>
        <id>https://rafaelds.com/posts/my-2024-plans-and-goals-for-rafaelds/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/my-2024-plans-and-goals-for-rafaelds/">&lt;p&gt;Some of you know that I started &lt;code&gt;rafaelds&lt;&#x2F;code&gt; as my outlet to learn (and share) anything about mobile app development with Flutter. I usually build Flutter apps to help myself automate repetitive tasks related to my work and life, and I usually write articles about the things I learn whilst building those apps.&lt;&#x2F;p&gt;
&lt;p&gt;I have received some feedback regarding the structure of rafaelds. The most impactful feedback that I got was about the website not having a direction. It would be nice if visitors to the website would find out how and where to start if they want to build apps with Flutter. Currently, rafaelds has just random articles about Flutter.&lt;&#x2F;p&gt;
&lt;p&gt;I want to acknowledge your feedback and suggestions by coming up with a plan of action and goals for 2024.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;2024-goals&quot;&gt;2024 Goals&lt;&#x2F;h1&gt;
&lt;p&gt;I only have 4 goals for rafaelds this year. I&#x27;ll be happy if I can do more, but I will try to prioritise these goals first.&lt;&#x2F;p&gt;
&lt;p&gt;📖  Free e-book to learn Flutter from scratch by building a full-fledged app
📑  Improve the free Flutter widget cheat sheet PDF
📝  At least 2 articles&#x2F;tutorials per month
📝  Write at least 15 articles&lt;&#x2F;p&gt;
&lt;h1 id=&quot;book-free-e-book-to-learn-flutter-from-scratch-by-building-a-full-fledged-app&quot;&gt;📖  Free e-book to learn Flutter from scratch by building a full-fledged app&lt;&#x2F;h1&gt;
&lt;p&gt;My main goal this year is to write up a zero-to-hero tutorial for learning Flutter in e-book format. This e-book will guide you to build an app whilst touching on the important aspects of app development.&lt;&#x2F;p&gt;
&lt;p&gt;Everything found in this e-book should be available online across different sources. The difference is I want to make it as accessible to everyone as I can. I believe that having most of the things we need to build an app compiled in one place will drastically speed up the learning process. The only thing to worry about is making the time to read it and build apps with Flutter.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;bookmark-tabs-improve-the-free-flutter-widget-cheat-sheet-pdf&quot;&gt;📑  Improve the free Flutter widget cheat sheet PDF&lt;&#x2F;h1&gt;
&lt;p&gt;I am currently in the works of building a tool (using Flutter) so I can easily update this PDF and add more common widgets. I made this PDF using &lt;code&gt;Sketch.app&lt;&#x2F;code&gt; but it proved to be time-consuming if I wanted to add more content.&lt;&#x2F;p&gt;
&lt;p&gt;If you have any suggestions on this, please let me know!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;memo-at-least-2-articles-tutorials-per-month&quot;&gt;📝  At least 2 articles &#x2F; tutorials per month&lt;&#x2F;h1&gt;
&lt;p&gt;I already failed this goal for January since it is almost February 😅. But this year, I plan to make more time for writing articles and be consistent with it. At the minimum, 1 article every fortnight should be enough.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;memo-write-at-least-15-articles-for-the-year&quot;&gt;📝  Write at least 15 articles for the year&lt;&#x2F;h1&gt;
&lt;p&gt;15 articles is not a lot, but having more Flutter content is definitely better. I started in June of 2023 last year and was able to write 14 articles until December. I plan to add more Flutter tutorials regarding architecture, patterns, best-practices, and the use of popular third-party libraries as these things change very frequently.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;To summarise, you can expect more Flutter articles and tutorials in 2024, as well as a free e-book on learning to build an app with Flutter 🔥.&lt;&#x2F;p&gt;
&lt;p&gt;Happy coding!
Raf&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Trying out Advent of Code 2023</title>
        <published>2023-12-03T00:00:00+00:00</published>
        <updated>2023-12-03T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/trying-out-advent-of-code-2023/"/>
        <id>https://rafaelds.com/posts/trying-out-advent-of-code-2023/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/trying-out-advent-of-code-2023/">&lt;p&gt;As per tradition, &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;was.tl&#x2F;&quot;&gt;Eric Wastl&lt;&#x2F;a&gt; has already started the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;adventofcode.com&#x2F;&quot;&gt;2023 Advent of Code&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;My goal is to solve as much problem as I can before giving up. Let&#x27;s see how far I&#x27;d get with this 😅.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;platform&quot;&gt;Platform&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;ll be using &lt;code&gt;Kotlin&lt;&#x2F;code&gt; using &lt;code&gt;Android Studio&lt;&#x2F;code&gt;. This would mean that I would heavily depend on the pre-written Kotlin extension functions and what not to make solving these exercises faster.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;my-solution&quot;&gt;My solution&lt;&#x2F;h2&gt;
&lt;p&gt;If you wanna see how awful and brute-forcey my solutions are (if they even run), come check it out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rafaelds&#x2F;Advent-of-Code-2023&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Have fun!&lt;&#x2F;p&gt;
&lt;p&gt;Ta.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to use Isolates for parallel processing in Flutter</title>
        <published>2023-11-07T00:00:00+00:00</published>
        <updated>2023-11-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/how-to-use-isolates-for-parallel-processing-in-flutter/"/>
        <id>https://rafaelds.com/posts/how-to-use-isolates-for-parallel-processing-in-flutter/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/how-to-use-isolates-for-parallel-processing-in-flutter/">&lt;p&gt;Every dart program and consequently, Flutter apps, run on a single-thread event loop called &lt;code&gt;Isolate&lt;&#x2F;code&gt;. This means that every asynchronous code we use, such as &lt;code&gt;Future&lt;&#x2F;code&gt; and  &lt;code&gt;Stream&lt;&#x2F;code&gt;, does not run on a separate thread when processed.&lt;&#x2F;p&gt;
&lt;p&gt;This is important to understand because when Flutter apps start doing some heavy work, whether synchronously or asynchronously, each work will take away valuable resources that will cause your app to slow down.&lt;&#x2F;p&gt;
&lt;p&gt;Dart does not support multiple threads but runs in an environment with its memory called &lt;code&gt;Isolate&lt;&#x2F;code&gt;. While technically not a multi-threaded or multi-process environment, we can use &lt;code&gt;Isolates&lt;&#x2F;code&gt; to run different workloads in parallel.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;dart-flutter-isolates&quot;&gt;Dart &#x2F; Flutter Isolates&lt;&#x2F;h2&gt;
&lt;p&gt;It might feel like the functions in our apps run in parallel when we use &lt;code&gt;Future&lt;&#x2F;code&gt; calls whilst still accepting input events and updating the UI being displayed. However, according to Dart&#x27;s documentation, this is not the case.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-main-isolate-graph-from-flutter.png&quot; alt=&quot;compressed-main-isolate-graph-from-flutter&quot; &#x2F;&gt;
&lt;sub&gt;From: https:&#x2F;&#x2F;dart.dev&#x2F;language&#x2F;concurrency&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;By default, Flutter apps run on a single &lt;code&gt;Isolate&lt;&#x2F;code&gt; called &lt;code&gt;Main isolate&lt;&#x2F;code&gt; which accepts input events and works on some computations while updating the UI. If we replace the computations with heavy computations, we will notice a significant difference.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Examples of heavy computations include image conversion, processing big data, complex mathematical functions, and more.  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I have made a simple app to demonstrate this behaviour. This app constantly updates the timer every ~60 milliseconds. The app also has two tasks triggered by button presses; one light process and a heavy process.&lt;&#x2F;p&gt;
&lt;p&gt;In this demo app, the light computation involves a delay to represent a light task. This is similar to a light process such as retrieving data from the internet. The heavy computation on the other hand is a function that loops 1 billion times.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;Light load process&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _lightComputation&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Light computation function that represents light processing such as fetching data from the internet.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;Heavy load process&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _heavyComputationWithoutIsolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  for&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1000000000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;++&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;value&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Heavy computation representing light processing such as image conversions and file manipulation.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Note that both functions are wrapped in a &lt;code&gt;Future&lt;&#x2F;code&gt;, so one might think that everything should be fine. Let&#x27;s observe this behaviour.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;running-light-computations-without-isolate&quot;&gt;Running light computations without Isolate&lt;&#x2F;h2&gt;
&lt;p&gt;Observe the timer in the app in the video below. The time value itself is irrelevant but focus on how it gets updated.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-light-process-no-isolate.mov.gif&quot; alt=&quot;compressed-light-process-no-isolate.mov&quot; &#x2F;&gt;
&lt;sub&gt;Light computation example.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;When it is a simple task, a normal asynchronous call works well. You will not notice any performance issues when you try to load something from the internet.&lt;&#x2F;p&gt;
&lt;p&gt;But what happens when we do a more complicated task?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;running-heavy-computations-without-isolate&quot;&gt;Running heavy computations without Isolate&lt;&#x2F;h2&gt;
&lt;p&gt;Looping through a task a billion times is a good representation of a heavy computation. As such, we need to run this on a background thread.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-heavy-process-no-isolate.mov.gif&quot; alt=&quot;compressed-heavy-process-no-isolate.mov&quot; &#x2F;&gt;
&lt;sub&gt;Heavy computation without Isolate example.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There is a noticeable jank in the timer display the moment we start running the heavy computation. Even if it runs asynchronously using &lt;code&gt;Future&lt;&#x2F;code&gt;, the speed still depends on the capacity of the main &lt;code&gt;Isolate&lt;&#x2F;code&gt; of the code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;running-one-off-computations-on-an-isolate&quot;&gt;Running one-off computations on an Isolate&lt;&#x2F;h2&gt;
&lt;p&gt;Heavy tasks need to be performed on an &lt;code&gt;Isolate&lt;&#x2F;code&gt;. The simplest way to do that is by using &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _heavyComputationUsingIsolateRun&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;run&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;      _heavyComputation&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1000000000&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _heavyComputation&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; count) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  for&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; count&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;++&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt; method accepts a &lt;code&gt;Function&lt;&#x2F;code&gt; as an argument that will run on a separate Isolate. This method allows you to offload a heavy task, runs it separately, and notifies you once it&#x27;s finished.&lt;&#x2F;p&gt;
&lt;p&gt;Using our simple example, the &lt;code&gt;Isolate.run&lt;&#x2F;code&gt; code will result in this behaviour.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-heavy-process-with-isolate-run.mov.gif&quot; alt=&quot;compressed-heavy-process-with-isolate-run.mov&quot; &#x2F;&gt;
&lt;sub&gt;Heavy computation using Isolate example.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We have now solved our issue with the app freezing when doing a for loop 1 billion times. It makes sense that this heavy workload was offloaded to the new &lt;code&gt;Isolate&lt;&#x2F;code&gt; that we just created. Happy days!&lt;&#x2F;p&gt;
&lt;p&gt;The next thing to consider is how the new &lt;code&gt;Isolate&lt;&#x2F;code&gt; communicates with the main &lt;code&gt;Isolate&lt;&#x2F;code&gt;. &lt;code&gt;Isolates&lt;&#x2F;code&gt; cannot simply access the memory space of other &lt;code&gt;Isolates&lt;&#x2F;code&gt; and will result in thrown errors if you attempt to. Calling &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt; can only return a value to the calling &lt;code&gt;Isolate&lt;&#x2F;code&gt; the moment the task has finished.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-main-isolate-with-new-isolate.png&quot; alt=&quot;compressed-main-isolate-with-new-isolate&quot; &#x2F;&gt;
&lt;sub&gt;Source: https:&#x2F;&#x2F;dart.dev&#x2F;language&#x2F;concurrency&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The solution is to communicate between &lt;code&gt;Isolates&lt;&#x2F;code&gt; through &lt;code&gt;ports&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;communication-between-isolates-through-isolate-spawn-and-ports&quot;&gt;Communication between Isolates through &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; and &lt;code&gt;ports&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;There are many reasons why some tasks performed in an &lt;code&gt;Isolate&lt;&#x2F;code&gt; should communicate with the &lt;code&gt;Main isolate&lt;&#x2F;code&gt;.
&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-isolate-communication-with-ports.png&quot; alt=&quot;compressed-isolate-communication-with-ports&quot; &#x2F;&gt;
&lt;sub&gt;Source: https:&#x2F;&#x2F;dart.dev&#x2F;language&#x2F;concurrency&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;One thing that comes to mind is informing the progress of a task. If we are converting some images, we may need to show our users the elapsed time of how long their image is being processed or how much data has been processed.&lt;&#x2F;p&gt;
&lt;p&gt;Another reason for communicating between &lt;code&gt;Isolates&lt;&#x2F;code&gt; is for tasks that run for a long time. The &lt;code&gt;Main isolate&lt;&#x2F;code&gt; may require some data from time to time. The same thing goes in which the new &lt;code&gt;Isolate&lt;&#x2F;code&gt; may need some commands from the &lt;code&gt;Main isolate&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;All of these can be implemented using &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; and &lt;code&gt;ports&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The common usage of &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; is to pass two arguments; the function to run and a &lt;strong&gt;single&lt;&#x2F;strong&gt; argument to that function. As a simple example, if we want to use &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; to run a function with an &lt;code&gt;int&lt;&#x2F;code&gt; argument, then we do:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;val isolate &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (integerArg) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Prints 1000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;argument: $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;integerArg&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;  1000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Isolate.spawn() code example.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;However, if we want these &lt;code&gt;Isolates&lt;&#x2F;code&gt; to communicate with each other, we need to pass a &lt;code&gt;SendPort&lt;&#x2F;code&gt;. A &lt;code&gt;SendPort&lt;&#x2F;code&gt; is a channel in which an &lt;code&gt;Isolate&lt;&#x2F;code&gt; can send messages that can be listened to by a &lt;code&gt;ReceivePort&lt;&#x2F;code&gt;. A &lt;code&gt;SendPort&lt;&#x2F;code&gt; is usually obtained by first creating a &lt;code&gt;ReceivePort&lt;&#x2F;code&gt; which has a &lt;code&gt;SendPort&lt;&#x2F;code&gt; field.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;val receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;val sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Defining receive and send ports.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;flutter-isolate-ports.png&quot; alt=&quot;flutter-isolate-ports&quot; &#x2F;&gt;
&lt;sub&gt;Different Isolates talking to each other through ports.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We can pass this &lt;code&gt;SendPort&lt;&#x2F;code&gt; instance the moment we create an &lt;code&gt;Isolate&lt;&#x2F;code&gt; using the &lt;code&gt;.spawn()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (sendPort) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Use sendPort to communicate with other isolates&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Passing the SendPort as an argument.&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Creating an &lt;code&gt;Isolate&lt;&#x2F;code&gt; using &lt;code&gt;.spawn()&lt;&#x2F;code&gt; won&#x27;t let you have more than one arguments for the function that you will run.&lt;&#x2F;p&gt;
&lt;p&gt;To get around this limitation, we can pass a &lt;code&gt;List&lt;&#x2F;code&gt; of arguments instead containing the &lt;code&gt;SendPort&lt;&#x2F;code&gt; as well as other arguments that you need to pass.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; iterationCount &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10000000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (args) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Cast the arguments inside the list of args&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; args[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; countIteration &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; args[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Use the arguments as you need&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  [sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; iterationCount]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;List argument&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Use a List of arguments to get around the single argument limitation&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next thing to do is to typecast these arguments in the function to use them correctly.&lt;&#x2F;p&gt;
&lt;p&gt;The last step is to listen to the messages sent by the child &lt;code&gt;Isolate&lt;&#x2F;code&gt; through the &lt;code&gt;ReceivePort&lt;&#x2F;code&gt; that we just created.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; iterationCount &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10000000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;((message) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;Process the message from the spawned isolate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;e.g. update the UI state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (args) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Cast the arguments inside the list of args&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; args[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; SendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; countIteration &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; args[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;] &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;as&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; int&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Use the arguments as you need&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; countIteration&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;++&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;send&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Processing ${&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; + &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;} of $&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;countIteration&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; x &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  [sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; iterationCount]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Going back to our example, we can now have the child &lt;code&gt;Isolate&lt;&#x2F;code&gt; send updates to the main one through these ports. The listener would get these messages and update the UI accordingly to show the progress of the task.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-use-isolates-for-parallel-processing-in-flutter&#x2F;compressed-heavy-process-with-ports-messaging.mov.gif&quot; alt=&quot;compressed-heavy-process-with-ports-messaging.mov&quot; &#x2F;&gt;
&lt;sub&gt; Displaying the message sent from the Worker Isolate. &lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;data-types-that-are-acceptable-as-arguments&quot;&gt;Data types that are acceptable as arguments&lt;&#x2F;h2&gt;
&lt;p&gt;Unfortunately, there are some restrictions as to what types of arguments you can pass through an &lt;code&gt;Isolate&lt;&#x2F;code&gt;. You can pass simple data types such as &lt;code&gt;int&lt;&#x2F;code&gt;, &lt;code&gt;float&lt;&#x2F;code&gt;, &lt;code&gt;String&lt;&#x2F;code&gt;, and &lt;code&gt;Lists&lt;&#x2F;code&gt;. However, you cannot pass objects such as &lt;code&gt;Sockets&lt;&#x2F;code&gt;, &lt;code&gt;closure functions&lt;&#x2F;code&gt;, or objects that can alter state across different &lt;code&gt;Isolates&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.dart.dev&#x2F;stable&#x2F;3.1.5&#x2F;dart-isolate&#x2F;SendPort&#x2F;send.html&quot;&gt;the documentation&lt;&#x2F;a&gt; for a complete list of objects that are allowed to be used as an argument.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;obtaining-the-result-of-an-isolate&quot;&gt;Obtaining the result of an Isolate&lt;&#x2F;h2&gt;
&lt;p&gt;There are cases where we need to know if the task running inside another &lt;code&gt;Isolate&lt;&#x2F;code&gt; has finished. It is also useful to know whether this task running in parallel successfully finished or failed due to an error.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-the-result-from-isolate-run&quot;&gt;Getting the result from &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;It is simple to get the result of a task that was executed using &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt;. This method returns a &lt;code&gt;Future&amp;lt;R&amp;gt;&lt;&#x2F;code&gt; where &lt;code&gt;R&lt;&#x2F;code&gt; is the return type of the function inside it. What it means is that we can simply return the result if the &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt; call as a &lt;code&gt;Future&amp;lt;R&amp;gt;&lt;&#x2F;code&gt; or with &lt;code&gt;async - await&lt;&#x2F;code&gt; approach.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;&quot;&gt;See how to use Future in Flutter&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getResultFromIsolateRun&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;run&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_processSomething)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _processSomething&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;Do some stuff&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;getting-the-result-from-isolate-spawn&quot;&gt;Getting the result from &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Calling &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; however does not return the data type of the function passed to it.  This method call returns a &lt;code&gt;Future&amp;lt;Isolate&amp;gt;&lt;&#x2F;code&gt; in which you can do additional things such as add error listeners, exit listeners, and even terminal this &lt;code&gt;Isolate&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we want to get the result of the task inside an &lt;code&gt;Isolate&lt;&#x2F;code&gt; created using the &lt;code&gt;spawn()&lt;&#x2F;code&gt; method, we would need to utilise the &lt;code&gt;onExit&lt;&#x2F;code&gt; and &lt;code&gt;onError&lt;&#x2F;code&gt; arguments. These two arguments require a &lt;code&gt;SendPort&lt;&#x2F;code&gt; which would get the event the moment the task has finished or produced an error.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;onexit-for-successful-result-and-onerror-for-errors&quot;&gt;&lt;code&gt;onExit&lt;&#x2F;code&gt; for successful result and &lt;code&gt;onError&lt;&#x2F;code&gt; for errors&lt;&#x2F;h2&gt;
&lt;p&gt;To determine if the &lt;code&gt;Isolate&lt;&#x2F;code&gt; finishes whether successfully or not, we can check when the message sent by the &lt;code&gt;SendPort&lt;&#x2F;code&gt; is either &lt;code&gt;null&lt;&#x2F;code&gt; or a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; for successful and failed results respectively.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;((message) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;Process the message from the spawned isolate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;==&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; null&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Task in the isolate has successfully finished&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;else if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;is&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Task threw an error&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Error: ${&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;message&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;]}&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (args) {}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  [sendPort]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  onExit&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  onError&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you want to have a more distinct message, you can use &lt;code&gt;addOnExitListener()&lt;&#x2F;code&gt; and &lt;code&gt;addErrorListener&lt;&#x2F;code&gt; as they let you define what message the &lt;code&gt;SendPort&lt;&#x2F;code&gt; will send when the task finishes. The caveat here is that you need get a reference of the &lt;code&gt;Isolate&lt;&#x2F;code&gt; before you can use these methods.&lt;&#x2F;p&gt;
&lt;p&gt;Errors will always have a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; as a message when it happens even if we use &lt;code&gt;addErrorListener()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;((message) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;Process the message from the spawned isolate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;==&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;EXIT_MESSAGE&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Task in the isolate has successfully finished&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;else if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;is&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F;Task threw an error&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Error: ${&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;message&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;]}&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (args) {}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  [sendPort]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;then&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (isolate) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;addOnExitListener&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      response&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;EXIT_MESSAGE&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;addOnErrorListener&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(sendPort)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;make-isolate-spawn-task-to-be-blocking-call&quot;&gt;Make &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; task to be blocking call&lt;&#x2F;h2&gt;
&lt;p&gt;The last thing that I want to discuss is the approach to make sure that the caller of our &lt;code&gt;Isolate&lt;&#x2F;code&gt; tasks gets the result of this run. This can be achieved by using a &lt;code&gt;Completer&lt;&#x2F;code&gt; object.&lt;&#x2F;p&gt;
&lt;p&gt;In our app example above, we want to reset the button states to be clickable again once the function within the &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; call is finished, whether successfully or not. We use a &lt;code&gt;Completer&lt;&#x2F;code&gt; object to block the call and only &lt;code&gt;complete&lt;&#x2F;code&gt; it when we get a successful or error result.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; doBackgroundWork&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ReceivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; completer &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Completer&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  receivePort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    (message) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;      &#x2F;&#x2F;Process the message from the spawned isolate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;      if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;==&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; null&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;        &#x2F;&#x2F;Task in the isolate has successfully finished, so unblock this function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        completer&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;complete&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;true&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;else if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (message &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;is&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; List&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;        &#x2F;&#x2F;Task in the isolate has an error, so unblock this function&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        completer&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;complete&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;false&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F; or use completer.completeError()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Isolate&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    (args) {}&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    [sendPort]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    onExit&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    onError&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sendPort&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;  &#x2F;&#x2F;Blcoked until completer.complete() is called&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; completer&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;sub&gt;Using Completer to block Isolate.spawn&lt;&#x2F;sub&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With this approach, any outside caller of the function &lt;code&gt;doBackgroundWork&lt;&#x2F;code&gt; can treat it as a normal function that returns a &lt;code&gt;Future&amp;lt;bool&amp;gt;&lt;&#x2F;code&gt; and can decide how to handle the results accordingly.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;That&#x27;s all the basic things that we need to understand to do parallel processing in Flutter through &lt;code&gt;Isolates&lt;&#x2F;code&gt;. We can use &lt;code&gt;Isolate.run()&lt;&#x2F;code&gt; for simple one-off tasks and use &lt;code&gt;Isolate.spawn()&lt;&#x2F;code&gt; for tasks that require constant updates and communication.&lt;&#x2F;p&gt;
&lt;p&gt;While this is a very basic introduction of the topic, there are still many problems that we might encounter when using &lt;code&gt;Isolates&lt;&#x2F;code&gt; that are not covered in this article. But I still hope that you can apply immediate improvements to your apps with this introductory article.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;dart.dev&#x2F;language&#x2F;concurrency&quot;&gt;https:&#x2F;&#x2F;dart.dev&#x2F;language&#x2F;concurrency&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;dart-isolate&#x2F;Isolate-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;dart-isolate&#x2F;Isolate-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;stable&#x2F;3.1.5&#x2F;dart-isolate&#x2F;Isolate&#x2F;spawn.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;stable&#x2F;3.1.5&#x2F;dart-isolate&#x2F;Isolate&#x2F;spawn.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>On the importance of abstraction when using third-party libraries</title>
        <published>2023-09-29T00:00:00+00:00</published>
        <updated>2023-09-29T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/on-the-importance-of-abstraction-when-using-third-party-libraries/"/>
        <id>https://rafaelds.com/posts/on-the-importance-of-abstraction-when-using-third-party-libraries/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/on-the-importance-of-abstraction-when-using-third-party-libraries/">&lt;p&gt;A good rule of thumb when using a third-party library is to use wrapper functions to abstract the implementation instead of calling their APIs directly. Doing so makes your code easily testable and flexible whenever you decide to use a different library.&lt;&#x2F;p&gt;
&lt;p&gt;This applies to third-party libraries that use databases, networking, hardware sensor events, and more.&lt;&#x2F;p&gt;
&lt;p&gt;The code samples here will be written in Kotlin but the idea can be applied to any programming language and framework.&lt;&#x2F;p&gt;
&lt;p&gt;I will share some insights on how abstraction allows us to move faster in developing new features while keeping our code testable. Imagine we are tasked to make a feature-rich Android app for taking card payments. This app will depend on a particular vendor that builds the device and provides us with an SDK (Software Development Kit) along with it.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Initial payment app architecture&quot; title=&quot;Initial payment app architecture&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;initial-payment-app-architecture.d85ec4b56bf94236.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;initial-payment-app-architecture.23e6a189ec6faacd.png 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;h2 id=&quot;directly-using-a-third-party-library-influences-the-app-s-architecture&quot;&gt;Directly using a third-party library influences the app&#x27;s architecture&lt;&#x2F;h2&gt;
&lt;p&gt;The quickest way to use a library is by calling its APIs directly. When building an app, it is straightforward to adapt to what the SDK interface gives you.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose the &lt;code&gt;VendorSdk&lt;&#x2F;code&gt; has an API for payment like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;interface&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; VendorSdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    enum class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; VendorPaymentStatus&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        SUCCESS,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        FAILED,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        CARD_DENIED&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; makePayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;, resultCallback: (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;VendorPaymentStatus&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Unit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this example, the vendor SDK provides a &lt;code&gt;makePayment&lt;&#x2F;code&gt; function that accepts an &lt;code&gt;amount&lt;&#x2F;code&gt; and informs the caller of the payment result through the &lt;code&gt;resultCallback&lt;&#x2F;code&gt; function argument.&lt;&#x2F;p&gt;
&lt;p&gt;If I implement a payment flow within &lt;code&gt;FeatureA&lt;&#x2F;code&gt;, it would look something like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; FeatureA&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; constructor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; vendorSdk: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;VendorSdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; analyticsService: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;AnalyticsService&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; triggerCardPayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;, paymentResultCallback: (&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;isSuccessful&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Unit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        vendorSdk.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;makePayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount) { vendorPaymentStatus: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;VendorSdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;.VendorPaymentStatus &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            when&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (vendorPaymentStatus) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                VendorSdk.VendorPaymentStatus.SUCCESS &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                    analyticsService.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;reportSuccessfulPayments&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;                    paymentResultCallback&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt;true&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-type&quot;&gt;                else -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                    analyticsService.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;reportUnsuccessfulPayments&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;                    paymentResultCallback&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt;false&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This implementation looks fine. The &lt;code&gt;FeatureA&lt;&#x2F;code&gt; module has access to &lt;code&gt;VendorSdk&lt;&#x2F;code&gt;, which in turn has access to the device&#x27;s hardware for making payments. But notice how &lt;code&gt;FeatureA&lt;&#x2F;code&gt; has to adapt &lt;code&gt;VendorSdk&lt;&#x2F;code&gt;&#x27;s interface which in this case is a callback function.&lt;&#x2F;p&gt;
&lt;p&gt;There is nothing wrong with defining the &lt;code&gt;triggerCardPayment()&lt;&#x2F;code&gt; method in &lt;code&gt;FeatureA&lt;&#x2F;code&gt; to accept a callback function. The problem is that we were somehow forced to include &lt;code&gt;paymentListener&lt;&#x2F;code&gt; because the SDK requires one. That is the only way we can handle the payment result of the &lt;code&gt;VendorSdk&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This might pose a problem because our entire app depends on how to use a specific vendor SDK and how it defines a particular way of getting the result. What if we decide to move to another vendor? Changing all the feature blocks to adhere to a new interface is very time-consuming. The ideal approach when migrating to another vendor&#x27;s SDK is only to change a single point in our app.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;create-a-wrapper-class-to-abstract-a-third-party-library-s-api&quot;&gt;Create a wrapper class to abstract a third-party library&#x27;s API&lt;&#x2F;h2&gt;
&lt;p&gt;A wrapper class (or function) in programming is a container whose purpose is to call another function. It might seem redundant, but this approach introduces a few advantages to improve our code.&lt;&#x2F;p&gt;




&lt;img alt=&quot;App architecture with PaymentWrapper included&quot; title=&quot;App architecture with PaymentWrapper included&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;architecture-with-payment-wrapper.1aeeb26e86391ebb.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;architecture-with-payment-wrapper.9e1ff4def24f1d74.png 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;With this approach, we can decide how we pass and receive data between the app and the &lt;code&gt;VendorSdk&lt;&#x2F;code&gt;. This extra layer will transform data and define the interface the app expects from third-party libraries.&lt;&#x2F;p&gt;
&lt;p&gt;In rare instances when a particular third-party library has been deprecated, moving to another library will only involve changing the &lt;code&gt;Wrapper&lt;&#x2F;code&gt; container whilst keeping the features of our app the same. This is in line with the principle of &lt;em&gt;coding against interfaces, not implementations&lt;&#x2F;em&gt; where the &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; is such an interface to one or more different third-party libraries.&lt;&#x2F;p&gt;
&lt;p&gt;Returning to our example, we might implement a &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; that looks like this.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;interface&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    suspend fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; pay&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) : &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentResult&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    enum class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; PaymentResult&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        SUCCESS,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        FAILED&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; VendorSdkPaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; constructor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; vendorSdk: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;VendorSdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;) : &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    override suspend fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; pay&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;): &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;PaymentResult&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        try&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; completableDeferred &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; CompletableDeferred&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentResult&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; withTimeout&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;5_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                vendorSdk.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;makePayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount) { result &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                    when&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (result) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                        VendorSdk.VendorPaymentStatus.SUCCESS &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                            completableDeferred.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;complete&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(PaymentWrapper.PaymentResult.SUCCESS)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-type&quot;&gt;                        else -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                            completableDeferred.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;complete&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(PaymentWrapper.PaymentResult.FAILED)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                completableDeferred.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;await&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;catch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (e: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Exception&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; PaymentWrapper.PaymentResult.FAILED&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As a team decision, we may have agreed to have our payment interface return the payment result from the function call instead of through a callback function. We can transform this approach in the &lt;code&gt;VendorSdkPaymentWrapper&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;FeatureA&lt;&#x2F;code&gt; module can adapt to this interface without knowing how the vendor implements their payment flow. We could potentially write the &lt;code&gt;FeatureA&lt;&#x2F;code&gt; as follows where the &lt;code&gt;triggerCardPayment()&lt;&#x2F;code&gt; can be run on a separate coroutine (asynchronously):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; FeatureA&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; constructor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; paymentWrapper: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; analyticsService: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;AnalyticsService&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    private val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; coroutineContext: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;CoroutineContext&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    suspend fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; triggerCardPayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;) : &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        val&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; result &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; paymentWrapper.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pay&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return when&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(result) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            PaymentWrapper.PaymentResult.SUCCESS &lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                analyticsService.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;reportSuccessfulPayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt;                true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-type&quot;&gt;            else -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                analyticsService.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;reportUnsuccessfulPayment&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt;                false&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this particular example, this is a better approach than using a callback function because there is a chance that the callback function may never be called, rendering your app to be stuck if there are no safeguards for timeouts.&lt;&#x2F;p&gt;
&lt;p&gt;The important thing here is that the &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; interface is dependent on your (or your team&#x27;s) decision and not decided by the third-party library that you&#x27;re using.&lt;&#x2F;p&gt;
&lt;p&gt;Now that there&#x27;s an abstraction on how we make payments, our code will be easier to change if the vendor SDK changes its implementation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;running-automated-tests-with-third-party-libraries&quot;&gt;Running automated tests with third-party libraries&lt;&#x2F;h2&gt;
&lt;p&gt;For the most part, running automated tests on an app that includes third-party libraries on emulators or simulators works fine as long as these libraries depend on common platform APIs such as Android or iOS frameworks. But what happens if a library is hardware-dependent?&lt;&#x2F;p&gt;




&lt;img alt=&quot;Running UI tests on an emulator breaks when a third-party library depends on specific hardware&quot; title=&quot;Running UI tests on an emulator breaks when a third-party library depends on specific hardware&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;ui-tests-break-on-emulator.72cdd5b7934fdf82.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;ui-tests-break-on-emulator.374b52ca5e7574d8.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;ui-tests-break-on-emulator.72cdd5b7934fdf82.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;If we want to run automation tests on an emulated device when a particular SDK only works on real devices, then using the third-party library directly will not work. In our payment app, these automation tests would fail as emulators do not have the necessary hardware for payment.&lt;&#x2F;p&gt;
&lt;p&gt;The good thing is that with the &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; we wrote, it is easier to make testing possible by changing a few bits in our implementation.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;use-interfaces-or-abstract-classes-when-writing-wrapper-classes&quot;&gt;Use interfaces or abstract classes when writing wrapper classes&lt;&#x2F;h3&gt;
&lt;p&gt;Use the abstract or interface construct when writing your wrapper functions or classes (as long as your framework supports it). This approach makes it easier to swap implementation whenever needed. Since we have already written &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; as an &lt;code&gt;interface&lt;&#x2F;code&gt;, we can simply write up new implementations according to the services we need.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Different implementation of PaymentWrapper through extension&quot; title=&quot;Different implementation of PaymentWrapper through extension&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;payment-wrapper-implementations.1ea877cc6bc8732d.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;payment-wrapper-implementations.32a75c16b841b501.png 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Our app will have two instances of &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt;, one for running the real app for production and the other for running UI tests on emulators.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; UiTestPaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; : &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier z-keyword&quot;&gt;    override suspend fun&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; pay&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(amount: &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;): &lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;PaymentWrapper&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;PaymentResult&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; PaymentWrapper.PaymentResult.SUCCESS&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The implementation of &lt;code&gt;UiTestPaymentWrapper&lt;&#x2F;code&gt; does not depend on the &lt;code&gt;VendorSdk&lt;&#x2F;code&gt;, so we are sure that this will run on UI tests. In this example, calling the &lt;code&gt;pay()&lt;&#x2F;code&gt; method always returns a successful result. To change this result, you can introduce a &lt;code&gt;setter()&lt;&#x2F;code&gt; function and change the return value according to different test cases.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;inject-the-correct-instance-of-paymentwrapper-according-to-use-case&quot;&gt;Inject the correct instance of PaymentWrapper according to use-case&lt;&#x2F;h3&gt;




&lt;img alt=&quot;Inject the correct instance of PaymentWrapper according to use-case&quot; title=&quot;Inject the correct instance of PaymentWrapper according to use-case&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;inject-payment-wrapper.7f9fde7dba27d298.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;inject-payment-wrapper.8ed95f8623928876.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;inject-payment-wrapper.7f9fde7dba27d298.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Swapping implementations of &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; when building the app depends on how you implement dependency management, and how these dependencies are injected into your wrappers. (For Android, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;developer.android.com&#x2F;training&#x2F;dependency-injection&#x2F;hilt-android&quot;&gt;Hilt&lt;&#x2F;a&gt; is a popular dependency injection library).&lt;&#x2F;p&gt;
&lt;p&gt;As mentioned before, this approach also solves the problem of migrating from one vendor to another. Instead of rewriting everything, we only need to add another implementation of &lt;code&gt;PaymentWrapper&lt;&#x2F;code&gt; that adheres to the interface of the new vendor SDK. Each feature&#x27;s existing implementation and tests will be untouched because the interface never changes.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Migrate to a new library by creating a new instance of PaymentWrapper while the rest of the app remains unchanged&quot; title=&quot;Migrate to a new library by creating a new instance of PaymentWrapper while the rest of the app remains unchanged&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;migrate-to-new-library.3b484719d0d87006.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;migrate-to-new-library.3cf45a9e1282aecb.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;migrate-to-new-library.3b484719d0d87006.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;Abstraction of third-party libraries can make your app more scalable, testable, and flexible when it comes to changes. Using wrapper classes as an abstraction can keep your features unchanged regardless of how a third-party library is implemented under the hood. This extra layer may seem like additional work, but the benefits of being resilient to dependency changes and testable code far outweigh the time cost.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Using Future in Flutter - Get comfortable with asynchronous data</title>
        <published>2023-09-19T00:00:00+00:00</published>
        <updated>2023-09-19T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/using-future-in-flutter-get-comfortable-with-asynchronous-data/"/>
        <id>https://rafaelds.com/posts/using-future-in-flutter-get-comfortable-with-asynchronous-data/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/using-future-in-flutter-get-comfortable-with-asynchronous-data/">&lt;p&gt;A &lt;code&gt;Future&lt;&#x2F;code&gt; refers to an object that can hold data at a point in time when it is ready to do so. This is used in cases where the app should not block or freeze other processes if the data needed has not been obtained yet.&lt;&#x2F;p&gt;
&lt;p&gt;Learning to use &lt;code&gt;Future&lt;&#x2F;code&gt; in Flutter is essential because many applications you build will rely on packages and APIs that use Futures extensively. It also makes your app responsive. An app without &lt;code&gt;Futures&lt;&#x2F;code&gt; is like an app without features (&lt;em&gt;get it?&lt;&#x2F;em&gt; 😇).&lt;&#x2F;p&gt;
&lt;h1 id=&quot;why-do-we-need-to-use-future-in-flutter&quot;&gt;Why do we need to use Future in Flutter?&lt;&#x2F;h1&gt;
&lt;p&gt;It is easier to understand the concept of &lt;code&gt;Future&lt;&#x2F;code&gt; with examples. Let&#x27;s look at a simple example: &lt;strong&gt;fetching data from the internet&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;download-internet-image-diagram.png&quot; alt=&quot;img&quot; &#x2F;&gt;Downloading an image through a slow network&lt;&#x2F;p&gt;
&lt;p&gt;For simplicity&#x27;s sake, let&#x27;s write up a simple function that gets data from the internet and returns this value as a &lt;code&gt;String&lt;&#x2F;code&gt;. Because we are not using a &lt;code&gt;Future&lt;&#x2F;code&gt;, the code will look like this:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;get-data-from-internet-future.png&quot; alt=&quot;img&quot; &#x2F;&gt;Blocking operation to download from the internet&lt;&#x2F;p&gt;
&lt;p&gt;This code is a straightforward approach to fetching data from a website, but what happens then if the internet connection of the device is slow?&lt;&#x2F;p&gt;
&lt;p&gt;The line of code that gets the data will operate for a few seconds before it returns the result. This will cause the app to block until the work is complete.&lt;&#x2F;p&gt;
&lt;p&gt;You will not have a chance to show a loading animation or do anything else because this line of code is &lt;code&gt;blocking&lt;&#x2F;code&gt; or &lt;code&gt;synchronous&lt;&#x2F;code&gt;, rendering your app unresponsive.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;use-future-for-operations-that-take-time-to-complete&quot;&gt;Use Future for operations that take time to complete&lt;&#x2F;h1&gt;
&lt;p&gt;If a function takes a long time to complete, then it is preferred to use &lt;code&gt;Futures&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Future&lt;&#x2F;code&gt; can execute operations without blocking. Once the data is ready, a callback will be executed with the data ready to be used.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use the &lt;code&gt;http&lt;&#x2F;code&gt; library as an example to get data from the internet. The &lt;code&gt;http&lt;&#x2F;code&gt; package has an API to make a &lt;code&gt;GET&lt;&#x2F;code&gt; request, which returns a &lt;code&gt;Future&lt;&#x2F;code&gt; of data type &lt;code&gt;Response&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;http-get-future-response-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Using &lt;code&gt;http&lt;&#x2F;code&gt; package to fetch data&lt;&#x2F;p&gt;
&lt;p&gt;Since the object that we get from the &lt;code&gt;http.get()&lt;&#x2F;code&gt; method is a &lt;code&gt;Future&lt;&#x2F;code&gt;, the &lt;code&gt;futureResponse&lt;&#x2F;code&gt; value does not necessarily contain the actual response of fetching the data from some website.&lt;&#x2F;p&gt;
&lt;p&gt;The good news is this call to fetch the data will not block your app. But how can we obtain and use the actual value of the &lt;code&gt;futureResponse&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h1 id=&quot;getting-the-value-from-future-asynchronously-using-callbacks&quot;&gt;Getting the value from Future asynchronously using callbacks&lt;&#x2F;h1&gt;
&lt;p&gt;One way of evaluating the &lt;code&gt;Future&lt;&#x2F;code&gt; is to use callbacks.&lt;&#x2F;p&gt;
&lt;p&gt;For successful evaluation, we can use the &lt;code&gt;Future&lt;&#x2F;code&gt; function &lt;code&gt;.then()&lt;&#x2F;code&gt; which accepts a callback function to consume the &lt;code&gt;Future&lt;&#x2F;code&gt; value.&lt;&#x2F;p&gt;
&lt;p&gt;In some cases, evaluating a &lt;code&gt;Future&lt;&#x2F;code&gt; may throw an error or an exception. We can use the &lt;code&gt;.onError()&lt;&#x2F;code&gt; function to add a callback for this situation.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;future-callback-main-example-2.png&quot; alt=&quot;img&quot; &#x2F;&gt;Use &lt;code&gt;.then()&lt;&#x2F;code&gt; and &lt;code&gt;.error()&lt;&#x2F;code&gt; to handle &lt;code&gt;Future&lt;&#x2F;code&gt; values with callback functions&lt;&#x2F;p&gt;
&lt;p&gt;Most use cases of using callbacks on &lt;code&gt;Future&lt;&#x2F;code&gt; values involved terminal operations such as saving to databases, sending analytics events, and other processes that do not require their results to be returned by the function.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;getting-the-value-from-future-synchronously-using-async-and-await&quot;&gt;Getting the value from Future synchronously using async and await&lt;&#x2F;h1&gt;
&lt;p&gt;There are instances when we want to use the value of a &lt;code&gt;Future&lt;&#x2F;code&gt; such as transforming this into another object. In cases like this, the callback approach will not work because callback values cannot be returned by the function.&lt;&#x2F;p&gt;
&lt;p&gt;To evaluate the value of the &lt;code&gt;Future&lt;&#x2F;code&gt;, we need to use the keywords &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-async-keyword&quot;&gt;The async keyword&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;async&lt;&#x2F;code&gt; keyword modifies a function so that we can evaluate a &lt;code&gt;Future&lt;&#x2F;code&gt; value in that function using the &lt;code&gt;await&lt;&#x2F;code&gt; keyword. Keep in mind that marking your function as &lt;code&gt;async&lt;&#x2F;code&gt; means that it can never return data types other than &lt;code&gt;void&lt;&#x2F;code&gt; and &lt;code&gt;Future&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; and will result in a compilation error if you try.&lt;&#x2F;p&gt;
&lt;p&gt;To define a function to be asynchronous, add the &lt;code&gt;async&lt;&#x2F;code&gt; keyword after the function name signature.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;future-async-examples.png&quot; alt=&quot;img&quot; &#x2F;&gt;The correct and incorrect ways of using &lt;code&gt;async&lt;&#x2F;code&gt; keyword&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-await-keyword&quot;&gt;The await keyword&lt;&#x2F;h2&gt;
&lt;p&gt;The key to evaluating a &lt;code&gt;Future&lt;&#x2F;code&gt; synchronously is by using the &lt;code&gt;await&lt;&#x2F;code&gt; keyword. When you add an &lt;code&gt;await&lt;&#x2F;code&gt; keyword before a &lt;code&gt;Future&lt;&#x2F;code&gt; value or a function that returns a &lt;code&gt;Future&lt;&#x2F;code&gt;, this line will block and &lt;strong&gt;wait&lt;&#x2F;strong&gt; until the value has been evaluated.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;await&lt;&#x2F;code&gt; operation will only work if this code is in an &lt;code&gt;async&lt;&#x2F;code&gt; function and will throw a compilation error if it is missing.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;future-await-examples.png&quot; alt=&quot;img&quot; &#x2F;&gt;The &lt;code&gt;await&lt;&#x2F;code&gt; keyword waits for the value of a &lt;code&gt;Future&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In the example above, we have the &lt;code&gt;getAlbumFromInternet()&lt;&#x2F;code&gt; function that represents fetching data from the internet. Since this function is an &lt;code&gt;async&lt;&#x2F;code&gt; function, we can use the &lt;code&gt;await&lt;&#x2F;code&gt; keyword to evaluate the value of a &lt;code&gt;Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Evaluating the &lt;code&gt;Future.delayed()&lt;&#x2F;code&gt; with &lt;code&gt;async&lt;&#x2F;code&gt; keyword will block this line until the elapsed duration. We also use the constructor &lt;code&gt;Future.value()&lt;&#x2F;code&gt; to return a &lt;code&gt;Future&lt;&#x2F;code&gt; with the value &lt;code&gt;&#x27;Album name&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using the same example, we have an asynchronous function that can evaluate another function that returns a &lt;code&gt;Future&lt;&#x2F;code&gt;. Inside the &lt;code&gt;getAlbumName()&lt;&#x2F;code&gt;, we can synchronously wait for the value of &lt;code&gt;getAlbumName()&lt;&#x2F;code&gt; with the &lt;code&gt;await&lt;&#x2F;code&gt; keyword.&lt;&#x2F;p&gt;
&lt;p&gt;In short, use &lt;code&gt;await&lt;&#x2F;code&gt; to evaluate the value of a &lt;code&gt;Future&lt;&#x2F;code&gt; inside an &lt;code&gt;async&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;using-future-in-flutter-widgets&quot;&gt;Using Future in Flutter widgets&lt;&#x2F;h1&gt;
&lt;p&gt;Now that we have covered the basics of &lt;code&gt;Future&lt;&#x2F;code&gt; and the necessary keywords to use them, we can move on to using these concepts in a Flutter app.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;futurebuilder-widget&quot;&gt;FutureBuilder widget&lt;&#x2F;h2&gt;
&lt;p&gt;The most basic way of consuming &lt;code&gt;Future&lt;&#x2F;code&gt; objects in a widget is through the &lt;code&gt;FutureBuilder&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Building this widget requires a few parameters.&lt;&#x2F;p&gt;
&lt;p&gt;The first parameter is the &lt;code&gt;Future&lt;&#x2F;code&gt; itself &lt;code&gt;future:&lt;&#x2F;code&gt; which will be evaluated when the widget is built. This argument allows you to set the &lt;code&gt;Future&lt;&#x2F;code&gt; that you wanted to get a value from.&lt;&#x2F;p&gt;
&lt;p&gt;The next one is the initial value &lt;code&gt;initialData:&lt;&#x2F;code&gt; of type &lt;code&gt;T&lt;&#x2F;code&gt; which will be used when the &lt;code&gt;Future&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; value has not been evaluated yet. This is useful for situations where the Future takes time to assess, such as fetching data with slow internet or doing long processes in the background. Although not a required argument, it&#x27;s better to include an initial value to constrain the possible values of the Future.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the most important argument of &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt; is the &lt;code&gt;builder:&lt;&#x2F;code&gt; itself. The builder function lets you decide what widget to display depending on the state of the future based on its function argument &lt;code&gt;AsyncSnapshot&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;future-builder-widget-example-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Example use of &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The builder function of the &lt;code&gt;FutureBuilder&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; provides access to an &lt;code&gt;AsyncSnapshot&lt;&#x2F;code&gt; argument that gives you both the current state and the current value of the &lt;code&gt;Future&lt;&#x2F;code&gt; that you provided.&lt;&#x2F;p&gt;
&lt;p&gt;You can get the current state of the &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt; through the &lt;code&gt;snapshot.connectionState&lt;&#x2F;code&gt; getter method. The values include &lt;code&gt;active&lt;&#x2F;code&gt;, &lt;code&gt;waiting&lt;&#x2F;code&gt;, &lt;code&gt;done&lt;&#x2F;code&gt;, or &lt;code&gt;none&lt;&#x2F;code&gt;. In most cases, you can check the &lt;code&gt;connectionState&lt;&#x2F;code&gt; if it is done and then show the appropriate widget by obtaining the value of the &lt;code&gt;Future&lt;&#x2F;code&gt; using &lt;code&gt;snapshot.data&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The current value of the &lt;code&gt;Future&lt;&#x2F;code&gt; can be null sometimes depending on the state of &lt;code&gt;Future&lt;&#x2F;code&gt;, that&#x27;s why it is better to do a null check on &lt;code&gt;snapshot.data&lt;&#x2F;code&gt; or validate the current state through &lt;code&gt;snapshot.connectionState&lt;&#x2F;code&gt; to show the appropriate widget.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;sample-weather-app-using-future&quot;&gt;Sample weather app using Future&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s put our learnings into practice by creating a simple app that fetches data from a publicly available API.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setup&quot;&gt;Setup&lt;&#x2F;h2&gt;
&lt;p&gt;A weather app can be a good example to demonstrate the use of &lt;code&gt;Futures&lt;&#x2F;code&gt;.
One of my favourite sources of free public APIs is the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;public-apis&#x2F;public-apis?ref=rafaelds&quot;&gt;public-api repo&lt;&#x2F;a&gt; from Github, so let&#x27;s use one of the weather API there called &lt;code&gt;open-meteo&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In this example, we will need to use the &lt;code&gt;http&lt;&#x2F;code&gt; package for fetching data from the internet. The result is a &lt;code&gt;json&lt;&#x2F;code&gt; formatted file, so we will also need the &lt;code&gt;dart:convert&lt;&#x2F;code&gt; for converting &lt;code&gt;json&lt;&#x2F;code&gt; formatted string to a usable object in our app.&lt;&#x2F;p&gt;
&lt;p&gt;Install the &lt;code&gt;http&lt;&#x2F;code&gt; package by running &lt;code&gt;flutter pub add http&lt;&#x2F;code&gt; on the terminal inside your project directory, or add &lt;code&gt;http: ^1.1.0&lt;&#x2F;code&gt; (&lt;em&gt;latest version at the time of writing&lt;&#x2F;em&gt;) as part of your dependencies inside the &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As I am using a macOS machine to run this app, I also have to add the necessary permission to access the network by adding this snippet in &lt;code&gt;macos&#x2F;Runner&#x2F;DebugProfile.entitlements&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;key&amp;gt;com.apple.security.network.client&amp;lt;&#x2F;key&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;true&#x2F;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;💡&lt;&#x2F;p&gt;
&lt;p&gt;Accessing data from the internet requires network permission on your device. Head over to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;data-and-backend&#x2F;networking?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;data-and-backend&#x2F;networking&lt;&#x2F;a&gt; to learn how to add network permission on the device you&#x27;re using to run this example app.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;app-source-code&quot;&gt;App source code&lt;&#x2F;h2&gt;
&lt;p&gt;Here&#x27;s the full code for our weather app example:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;weather-app-example-using-future-builder.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Sample Weather app that uses &lt;code&gt;Future&lt;&#x2F;code&gt; to obtain and display the current temperature&lt;&#x2F;p&gt;
&lt;p&gt;When you run this code, you should have something like this:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;sample-app-future-download.gif&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Weather app running on desktop&lt;&#x2F;p&gt;
&lt;h2 id=&quot;code-explanation&quot;&gt;Code explanation&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s dissect the source code and understand what each of the parts is doing by first looking at the &lt;code&gt;getCurrentWeather()&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;get-current-weather-future-function-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;The &lt;code&gt;async&lt;&#x2F;code&gt; function &lt;code&gt;getCurrentWeather()&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;getCurrentWeather()&lt;&#x2F;code&gt; function provides our app with the weather data that we need to display. Since we know that fetching data from the internet may block our app while waiting for the result, this is the perfect time to use a &lt;code&gt;Future&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We also want to display the weather data as a &lt;code&gt;String&lt;&#x2F;code&gt;, so we defined the return type to be &lt;code&gt;Future&amp;lt;String&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;url-await-future-example-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Blocking operation to obtain the response using &lt;code&gt;await&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The first thing that this function does is to fetch data from the &lt;code&gt;open-meteo&lt;&#x2F;code&gt; API. Since we need to process the JSON result from &lt;code&gt;http.get()&lt;&#x2F;code&gt;, we have to use the &lt;code&gt;await&lt;&#x2F;code&gt; keyword to block this function while waiting for the result.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;response&lt;&#x2F;code&gt; will have a data type &lt;code&gt;Response&lt;&#x2F;code&gt; as part of the &lt;code&gt;http&lt;&#x2F;code&gt; interface. The &lt;code&gt;response&lt;&#x2F;code&gt; body will have a value in JSON format similar to this:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;weather-response-example-json-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Weather data in JSON format&lt;&#x2F;p&gt;
&lt;p&gt;We use the &lt;code&gt;jsonDecode&lt;&#x2F;code&gt; from &lt;code&gt;dart:convert&lt;&#x2F;code&gt; package to convert this JSON object into a Map. Since we want to traverse this JSON and get the &lt;code&gt;current_weather&lt;&#x2F;code&gt;, we access this value using the &lt;code&gt;Map&lt;&#x2F;code&gt; interface.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;await-example-decoding-weather-json-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Converting JSON data to map&lt;&#x2F;p&gt;
&lt;p&gt;Once we have the temperature value, we can return this value to type &lt;code&gt;Future&amp;lt;String&amp;gt;&lt;&#x2F;code&gt; by constructing a Future with the temperature as its value in &lt;code&gt;String&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;future-current-temperature-string-1.png&quot; alt=&quot;img&quot; &#x2F;&gt;Constructor method of a &lt;code&gt;Future&lt;&#x2F;code&gt; with value&lt;&#x2F;p&gt;
&lt;h2 id=&quot;data-fetch-is-asynchronous&quot;&gt;Data fetch is asynchronous&lt;&#x2F;h2&gt;
&lt;p&gt;While all the data were being fetched and converted from the internet, the sample app &lt;strong&gt;DOES NOT&lt;&#x2F;strong&gt; block because we use a &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt; that processes the &lt;code&gt;Future&lt;&#x2F;code&gt;. If we look at our &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt;, we show a &lt;code&gt;Loading&lt;&#x2F;code&gt; widget while waiting for the Future to get the actual result.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;using-future-in-flutter-get-comfortable-with-asynchronous-data&#x2F;weather-app-future-builder-example-states.png&quot; alt=&quot;img&quot; &#x2F;&gt;Implementation example of using &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This is one way of ensuring we display the appropriate widget according to the state of the &lt;code&gt;Future&lt;&#x2F;code&gt;. We can check for the current status of the &lt;code&gt;Future&lt;&#x2F;code&gt; in a &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt; by using the &lt;code&gt;ConnectionState&lt;&#x2F;code&gt; of the &lt;code&gt;snapshot&lt;&#x2F;code&gt; argument in the function. Based on the status, we can decide which widget to display, such as a loading spinner widget or an error widget, whichever is more appropriate.&lt;&#x2F;p&gt;
&lt;p&gt;The sample app shows a &lt;code&gt;Text&lt;&#x2F;code&gt; widget with a &lt;code&gt;Loading&lt;&#x2F;code&gt; text because the &lt;code&gt;snapshot.connectionState&lt;&#x2F;code&gt; is equal to &lt;code&gt;active&lt;&#x2F;code&gt; as it has not finished fetching the data yet.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;snapshot&lt;&#x2F;code&gt; argument also lets you know if an error has occurred so you can display the appropriate message to the app&#x27;s users.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;Implementing &lt;code&gt;Futures&lt;&#x2F;code&gt; is an essential part of responsive apps in Flutter. Using &lt;code&gt;Future&lt;&#x2F;code&gt; allows your app to process data asynchronously so it can run smoothly.
Understanding &lt;code&gt;Futures&lt;&#x2F;code&gt; also lets you use a wide range of Flutter packages, which is very important to save time creating your own features.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;await&lt;&#x2F;code&gt; keyword lets you wait for a &lt;code&gt;Future&lt;&#x2F;code&gt; value to be processed within an &lt;code&gt;async&lt;&#x2F;code&gt; function. This enables you to treat &lt;code&gt;Future&lt;&#x2F;code&gt; values synchronously in some parts of your app for data transformation and eventually make your apps responsive.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;reference&quot;&gt;Reference&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;dart-async&#x2F;Future-class.html?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;dart-async&#x2F;Future-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Dependency Injection in Flutter using Riverpod</title>
        <published>2023-08-08T00:00:00+00:00</published>
        <updated>2023-08-08T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/dependency-injection-in-flutter-using-riverpod/"/>
        <id>https://rafaelds.com/posts/dependency-injection-in-flutter-using-riverpod/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/dependency-injection-in-flutter-using-riverpod/">&lt;p&gt;In this post, I will talk about the use of the Riverpod package as a dependency injection framework in Flutter.&lt;&#x2F;p&gt;
&lt;p&gt;Managing dependencies is important when writing a scalable project. A component that depends on another dependency should be able to obtain them without needing to know how to create them. One approach to achieve this is through dependency injection.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-is-dependency-injection&quot;&gt;What is Dependency injection?&lt;&#x2F;h1&gt;
&lt;p&gt;Dependency injection is a concept for managing dependencies and separating the concerns of the different blocks of your code.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s say that I would need a &lt;code&gt;Professor&lt;&#x2F;code&gt; to learn from when attending a lecture. I do not care who the assigned &lt;code&gt;Professor&lt;&#x2F;code&gt; is in this particular class. I just need someone to teach the lecture that I am attending. &lt;strong&gt;Dependency injection would be the school providing a professor I can learn from without choosing who that person would be.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;why-do-we-need-to-use-dependency-injection&quot;&gt;Why do we need to use Dependency Injection?&lt;&#x2F;h1&gt;
&lt;p&gt;The main reason behind using a dependency injection framework is to make our coding experience smoother.&lt;&#x2F;p&gt;
&lt;p&gt;The component that we are building should be able to locate and use another component easily. Our component should also be testable in a way that it can easily get a stub or fake version of its dependency when we run tests.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;BAD Example&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Lecture&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Lecture&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _professor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Proton&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; participate&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Student&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; student) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;enterClassroom&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;sit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listenTo&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_professor)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To be consistent with our previous example, look at the example code above. The &lt;code&gt;participate()&lt;&#x2F;code&gt; method will first make a student enter the classroom, sit on a chair, and listen to the &lt;code&gt;_professor&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This will work but what if we want to test this &lt;code&gt;Lecture&lt;&#x2F;code&gt; component? Since the professor was defined internally and was not &lt;strong&gt;injected&lt;&#x2F;strong&gt;, we cannot mock or stub this dependency. It is always set to be the same value; Professor &lt;code&gt;Proton&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; BETTER example&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Lecture&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Lecture&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    required&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _professor &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _professor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; participate&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Student&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; student) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;enterClassroom&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;sit&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    student&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listenTo&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_professor)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A better example would be to use the class &lt;code&gt;constructor&lt;&#x2F;code&gt; as a means of injecting the dependency. As we create an instance of a &lt;code&gt;Lecture&lt;&#x2F;code&gt;, we can easily pass a fake professor as a parameter argument when running tests.&lt;&#x2F;p&gt;
&lt;p&gt;But what happens then when we start having more than one dependency? Using constructor injection is tedious as your app grows. This is the problem we want to solve by using a dependency injection framework.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-is-riverpod&quot;&gt;What is Riverpod?&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;dependency-injection-in-flutter-using-riverpod&#x2F;riverpod-logo.png&quot; alt=&quot;riverpod-logo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Riverpod is a reactive caching and data-binding framework built by Remi Rousselet. It is a popular solution built for doing state management, dependency injection and caching data which the Flutter framework does not provide natively.&lt;&#x2F;p&gt;
&lt;p&gt;While it&#x27;s mainly used for state management, Riverpod can be used for dependency injection alone if your project is using another library for managing states such as BLoC.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Riverpod is best used as a complete package as it can accomplish everything from state management to data caching and not just dependency injection.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Each dependency that we want to inject is wrapped by an object called &lt;code&gt;Provider&lt;&#x2F;code&gt;. In Riverpod, a &lt;code&gt;Provider&lt;&#x2F;code&gt; is an immutable object that is used to obtain a dependency. It wraps the dependency that you need so that it can be accessed anywhere in your app that is within a &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;installation&quot;&gt;Installation&lt;&#x2F;h1&gt;
&lt;p&gt;Set up your project by including the Flutter Riverpod dependency.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; flutter pub add flutter_riverpod&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;how-do-we-get-dependencies-with-riverpod&quot;&gt;How do we get dependencies with Riverpod?&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s use a more realistic example (a simple weather app) to demonstrate how to use Riverpod for dependency injection.&lt;&#x2F;p&gt;
&lt;p&gt;There are three main blocks in &lt;code&gt;Riverpod&lt;&#x2F;code&gt; that we need to be familiar with;  &lt;code&gt;Provider&lt;&#x2F;code&gt;, &lt;code&gt;Ref&lt;&#x2F;code&gt; (or &lt;code&gt;WidgetRef&lt;&#x2F;code&gt; in Flutter widgets), and the ProviderScope. Each of these blocks is important to get &lt;code&gt;Riverpod&lt;&#x2F;code&gt; running.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-provider&quot;&gt;1. Provider&lt;&#x2F;h2&gt;
&lt;p&gt;The first block that we already know is the &lt;code&gt;Provider&lt;&#x2F;code&gt; which can give us the dependency we need. For example, if we have a &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt; to get today&#x27;s weather, we can build a provider for this dependency as such:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Weather Repository to get today&amp;#39;s weather&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _dataSource &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; todaysWeather&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _dataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Weather Repository provider&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; weatherRepositoryProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;weatherRepositoryProvider&lt;&#x2F;code&gt; can be accessed and used across our app to obtain a &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt; dependency.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-providerscope&quot;&gt;2. ProviderScope&lt;&#x2F;h2&gt;
&lt;p&gt;Each dependency that is wrapped by a &lt;code&gt;Provider&lt;&#x2F;code&gt; is only accessible within a &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;. The &lt;code&gt;ProviderScope&lt;&#x2F;code&gt; sets the environment of our app so that the state of our dependencies is contained within this scope.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ProviderScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The best approach to define the scope of providers is to wrap the whole app in a &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;. It is usually done in the top-most parent widget of the app so that all the dependencies can be accessed anywhere within that scope.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3a-dependency-injection-in-providers-using-ref&quot;&gt;3a. Dependency injection in providers using Ref&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;Ref&lt;&#x2F;code&gt; object is the key to locating our providers in the defined scope. If we want to inject a weather data source for our &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt;, we can create a &lt;code&gt;Provider&lt;&#x2F;code&gt; for this data source.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Weather data source interface&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;abstract class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Dummy OpenWeatherAPI Weather data source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;10C&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Weather Data Source provider&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; openWeatherDataSourceProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have a &lt;code&gt;Provider&lt;&#x2F;code&gt; for our weather data source, we can inject this into our &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt;. First, we refactor the repository to accept a data source when constructing the object. Then, we use the &lt;code&gt;watch&lt;&#x2F;code&gt; method of the &lt;code&gt;Ref&lt;&#x2F;code&gt; object in the weather repository provider to obtain the data source.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;watch&lt;&#x2F;code&gt; method locates the latest state of the provider being watched. In our example, it will be the &lt;code&gt;OpenWeatherApiDataSource&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;Using ref to find a weather data source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; weatherRepositoryProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(openWeatherDataSource)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of tightly coupling the open weather data source with the &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt;, we have improved the code by injecting this dependency instead.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we know how to obtain dependencies using the &lt;code&gt;Ref&lt;&#x2F;code&gt; object, let&#x27;s continue with &lt;code&gt;WidgetRef&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3b-dependency-injection-in-widgets-using-widgetref&quot;&gt;3b. Dependency injection in widgets using WidgetRef&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;Ref&lt;&#x2F;code&gt; is used to obtain provider dependencies within other providers. On the other hand, &lt;code&gt;WidgetRef&lt;&#x2F;code&gt; is used to locate providers inside a Flutter widget.&lt;&#x2F;p&gt;
&lt;p&gt;The most common approach to get a &lt;code&gt;WidgetRef&lt;&#x2F;code&gt; is to make your widget extend from the &lt;code&gt;ConsumerWidget&lt;&#x2F;code&gt; class if it is a &lt;code&gt;StatelessWidget&lt;&#x2F;code&gt;, or from &lt;code&gt;ConsumerStatefulWidget&lt;&#x2F;code&gt; if it is a &lt;code&gt;StatefulWidget&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an example of a stateless widget using &lt;code&gt;ConsumerWidget&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;Using widget ref to find a weather data source&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ConsumerWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherScreen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WidgetRef&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherRepository &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherRepositoryProvider)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FutureBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      future&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;todaysWeather&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; snapshot) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (snapshot&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;hasData) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;          return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(snapshot&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;data&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;else&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;          return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CircularProgressIndicator&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We use the &lt;code&gt;WidgetRef&lt;&#x2F;code&gt; instance in the widget&#x27;s build function to obtain the dependency that we need. In the &lt;code&gt;WeatherScreen&lt;&#x2F;code&gt; example, we get the &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt; dependency to obtain today&#x27;s weather.&lt;&#x2F;p&gt;
&lt;p&gt;Any widgets extending either ConsumerWidget or ConsumerStatefulWidget within the &lt;code&gt;ProviderScope&lt;&#x2F;code&gt; can access the &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt; using &lt;code&gt;ref.watch()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;is-defining-providers-as-global-variables-a-bad-thing&quot;&gt;Is defining providers as global variables a bad thing?&lt;&#x2F;h1&gt;
&lt;p&gt;There is a bad connotation with using global variables in your code due to mutability, testability, and scalability dangers. Global variables tend to be not testable due to their global scope and the ability to be modified anywhere in the code.&lt;&#x2F;p&gt;
&lt;p&gt;Providers in Riverpod are safe in this manner because the states that they hold are contained within the &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;. They are also always declared as &lt;code&gt;final&lt;&#x2F;code&gt; in which variables pointing to them cannot be updated or replaced.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;how-do-we-replace-dependencies-when-running-tests&quot;&gt;How do we replace dependencies when running tests?&lt;&#x2F;h1&gt;
&lt;p&gt;We should be able to constrain the environment and scope of our app when running tests. Dependencies such as data repositories should be easy to replace with mocked or stubbed instances as you write your tests. Here is how you do it with Riverpod.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s have all our providers in a separate file called &lt;code&gt;providers.dart&lt;&#x2F;code&gt; which will look something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;providers.dart&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; openWeatherDataSourceProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; weatherRepositoryProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(openWeatherDataSourceProvider)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we want to run our app, we usually run the &lt;code&gt;main.dart&lt;&#x2F;code&gt; which should look like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; Production code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod&#x2F;flutter_riverpod.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;weather_app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ProviderScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if we want to inject fake versions of our dependencies in tests, we can override them by using the &lt;code&gt;overrides&lt;&#x2F;code&gt; argument in the &lt;code&gt;ProviderScope&lt;&#x2F;code&gt; of our test file.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F;Test code&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod&#x2F;flutter_riverpod.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod_di&#x2F;app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod_di&#x2F;providers.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod_di&#x2F;services.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_test&#x2F;flutter_test.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FakeWeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;value&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Fake value&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  testWidgets&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;    &amp;#39;Use a fake weather data source&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    (tester) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;      await&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; tester&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pumpWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;        ProviderScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          overrides&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            openWeatherDataSourceProvider&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;overrideWithValue&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;              FakeWeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the example code above, we replace the open weather data source with a fake one whilst using the real &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt;. To do so, we need to use the provider&#x27;s method &lt;code&gt;overrideWithValue&lt;&#x2F;code&gt; and supply your desired instance.&lt;&#x2F;p&gt;
&lt;p&gt;The variables being global did not cause any issue because the scope is still controlled when running our widget tests and was overridden using the overrides parameter of the &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;. We keep our production code as is and the same time able to stub our dependencies with fake ones.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;weather-app-example&quot;&gt;Weather app example&lt;&#x2F;h1&gt;
&lt;p&gt;Putting everything that we learn into practice, let&#x27;s implement the simple weather app.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;dependency-injection-in-flutter-using-riverpod&#x2F;di-riverpod-weather-app.jpg&quot; alt=&quot;di-riverpod-weather-app&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The first thing to do is make sure that our weather app is wrapped within a &lt;code&gt;ProviderScope&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;main.dart&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod&#x2F;flutter_riverpod.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;weather_app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ProviderScope&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We then declare the different services that we will use in the project. We define the WeatherRepository and WeatherDataSource interface and its implementation.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;services.dart&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;abstract class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    await&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;delayed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Duration&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seconds&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;quot;10C&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;required&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;weatherDataSource})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Future&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; todaysWeather&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;getWeatherToday&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we define the providers that we will use to get our dependencies in a file called &lt;code&gt;providers.dart&lt;&#x2F;code&gt;.  We need a &lt;code&gt;WeatherRepository&lt;&#x2F;code&gt; to fetch today&#x27;s weather using a data source &lt;code&gt;WeatherDataSource&lt;&#x2F;code&gt;. In our implementation, our data source will be hardcoded to a value including a delay to simulate loading progress.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;providers.dart&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod&#x2F;flutter_riverpod.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;services.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; openWeatherDataSourceProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; OpenWeatherApiDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; weatherRepositoryProvider &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Provider&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  (ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(openWeatherDataSourceProvider)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherDataSource&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherDataSource)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, all that is left is to write up our widgets to display the weather. Our &lt;code&gt;WeatherApp&lt;&#x2F;code&gt; extends from &lt;code&gt;ConsumerWidget&lt;&#x2F;code&gt; so that it can access the dependencies it needs to get the weather data.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_riverpod&#x2F;flutter_riverpod.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;providers.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;services.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ConsumerWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    Key&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;?&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; super(key&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; key)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WidgetRef&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ref) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;    final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; WeatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherRepository &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherRepositoryProvider)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;fromSeed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seedColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;blueGrey)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        useMaterial3&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      debugShowCheckedModeBanner&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; false&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        floatingActionButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FloatingActionButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;extended&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; () {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;invalidate&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(weatherRepositoryProvider)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          label&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Refresh&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FutureBuilder&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            future&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; weatherRepository&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;todaysWeather&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            builder&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; snapshot) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;              if&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; ((snapshot&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;hasData) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  (snapshot&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;connectionState &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;==&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ConnectionState&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;done)) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;                return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  snapshot&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;data&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;!&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                  style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(context)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;textTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;headlineMedium&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;                )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              } &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;else&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-storage z-modifier&quot;&gt;                return const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CircularProgressIndicator&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            }&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When the &lt;code&gt;WeatherApp&lt;&#x2F;code&gt; widget builds, it will obtain a reference of the weather repository and get the &lt;code&gt;Future&lt;&#x2F;code&gt; value of today&#x27;s weather. Using a &lt;code&gt;FutureBuilder&lt;&#x2F;code&gt; widget, it will wait for the call to finish before displaying the result.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;dependency-injection-in-flutter-using-riverpod&#x2F;weather-app-riverpod.gif&quot; alt=&quot;weather-app-riverpod&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I have also added a button to simulate fetching up-to-date weather data. The &lt;code&gt;ref.invalidate(provider)&lt;&#x2F;code&gt; is the way to clear the data cache stored by Riverpod. When we call this method, the provider will provide a fresh and updated state for our dependents to access. In our example, it will try to fetch new data from the &lt;code&gt;WeatherDataSource&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;The sample weather app was written to demonstrate dependency injection of services such as data sources and repositories. When using Riverpod, it is much better to use providers like `FutureProvider` or `StreamProvider` to display cached data.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;Dependency injection is important in your app&#x27;s architecture as it keeps your codebase maintainable and scalable. Riverpod is a great framework to use not only for state management but also for implementing dependency injection.&lt;&#x2F;p&gt;
&lt;p&gt;To get the best results from Riverpod, check out https:&#x2F;&#x2F;riverpod.dev&#x2F; to get a more detailed explanation about the framework and how it is meant to be used.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;riverpod.dev&#x2F;&quot;&gt;https:&#x2F;&#x2F;riverpod.dev&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Should I learn Flutter in 2023 - An App Developer&#x27;s edge</title>
        <published>2023-07-13T00:00:00+00:00</published>
        <updated>2023-07-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/should-i-learn-flutter-in-2023/"/>
        <id>https://rafaelds.com/posts/should-i-learn-flutter-in-2023/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/should-i-learn-flutter-in-2023/">&lt;p&gt;&lt;strong&gt;Yes&lt;&#x2F;strong&gt;, it is worth learning Flutter app development in 2023. You get the satisfaction of creating something valuable, get paid for it, and all the other good stuff!&lt;&#x2F;p&gt;
&lt;p&gt;I want to share the benefits and advantages that you will get if you decide to learn it, or even start a career as an app developer.&lt;&#x2F;p&gt;
&lt;p&gt;This article is not only for current software engineers who want to learn a new framework but also for people without a programming background who are considering making a career shift to the world of app development.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;starting-a-career-in-software-engineering&quot;&gt;Starting a career in software engineering&lt;&#x2F;h1&gt;
&lt;p&gt;I personally did not start in the app development space. I was not even a programmer when I finished university. But after spending time learning on my own and doing side projects, I was able to build a career that is still going on for 10 years.&lt;&#x2F;p&gt;
&lt;p&gt;Some people might tell themselves they do not have what it takes to start a career writing code. I disagree. Anyone can become a software developer as long as they have these two key assets: Determination and Time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-determination&quot;&gt;1. Determination&lt;&#x2F;h2&gt;
&lt;p&gt;You need to be determined to learn new skills. You just have to want it. All information that you need to be successful is freely available on the internet, this website included! It is only yourself that&#x27;s preventing you to gain more knowledge.&lt;&#x2F;p&gt;
&lt;p&gt;You don&#x27;t need a high level of mathematics knowledge to start. Considering the requirements and gruelling interview processes posted by companies looking for software engineering candidates, I understand that it is a very bold statement. You still need to learn the fundamentals to start moving.&lt;&#x2F;p&gt;
&lt;p&gt;Knowing the fundamentals of logic theory and mathematics is essential, and is something that you will learn eventually as you start your learning journey in software development.&lt;&#x2F;p&gt;
&lt;p&gt;The catch is that you need to have the determination to start. You need to start soon.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-time&quot;&gt;2. Time&lt;&#x2F;h2&gt;
&lt;p&gt;The other requirement is time. Author Malcolm Gladwell proposed that mastery of a skill happens after spending 10,000 hours of dedicated work. This roughly translates to working on your skills for around 3 hours a day, every day, for 10 years.&lt;&#x2F;p&gt;
&lt;p&gt;While your first goal is not to be a master at first, it is important to designate a reasonable amount of time to learn how to write code, understand programming concepts, and learn the framework that we want to use.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;why-flutter&quot;&gt;Why Flutter?&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;flutter-google.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Flutter x Google&lt;&#x2F;p&gt;
&lt;p&gt;Flutter is an open-source framework made by Google used to build apps across different platforms.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-it-s-easy&quot;&gt;1. It&#x27;s easy*&lt;&#x2F;h2&gt;
&lt;p&gt;The primary language used in Flutter is &lt;code&gt;Dart&lt;&#x2F;code&gt;. Based on my experience, &lt;code&gt;Dart&lt;&#x2F;code&gt; is a relatively easy programming language to learn, especially for developers who have worked with Java, Python, or other object-oriented languages.&lt;&#x2F;p&gt;
&lt;p&gt;Building a user interface in Flutter is straightforward. Flutter code can be described as hierarchical in nature, built from trees of basic units called Widgets. You create these widgets and combine them with each other to form beautiful screens.&lt;&#x2F;p&gt;
&lt;p&gt;The truth is, learning your first programming language may require certain changes in your perspective to understand (e.g. loops, condition statements, etc.). Flutter&#x27;s thorough examples and documentation will definitely help to get you started.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;☝🏾&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Flutter is easy given the assumption that you have been writing code before.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;2-it-runs-on-all-platforms&quot;&gt;2. It runs on all platforms&lt;&#x2F;h2&gt;
&lt;p&gt;This is the best time to write Flutter apps because of how Flutter supports development not just on mobile platforms but also on desktop and web.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;flutter-on-different-platforms.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Flutter apps running on different platforms &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;flutter.dev&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Learning just &lt;code&gt;Dart&lt;&#x2F;code&gt; and &lt;code&gt;Flutter&lt;&#x2F;code&gt; keeps your mind focused on developing features instead of learning all the bits and pieces of each platform that you want to build the app for. As they say, one code to rule them all.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-it-provides-a-smooth-development-experience&quot;&gt;3. It provides a smooth development experience&lt;&#x2F;h2&gt;
&lt;p&gt;Imagine working together with a designer while both of you are going through a prototype. Flutter makes it possible to do this seamlessly because it was built to make the development process fast as it reduces unnecessary waiting times for feedback.&lt;&#x2F;p&gt;
&lt;p&gt;Hot reload is one of the features that I like where you see your new changes in code being applied on your test device instantaneously. There is no need to wait to compile and deploy your app to see the changes which saves you a lot of time.&lt;&#x2F;p&gt;
&lt;p&gt;The framework itself is also open-source, which means a whole bunch of contributors are improving Flutter and developing useful libraries for your apps.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-edge-of-an-app-developer&quot;&gt;The Edge of an App Developer&lt;&#x2F;h1&gt;
&lt;h3 id=&quot;the-perks-you-get-as-a-flutter-developer&quot;&gt;The perks you get as a Flutter developer&lt;&#x2F;h3&gt;
&lt;h2 id=&quot;1-be-a-creator&quot;&gt;1 - Be a creator&lt;&#x2F;h2&gt;
&lt;p&gt;Humans have always been driven to create solutions. A &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.sciencedirect.com&#x2F;science&#x2F;article&#x2F;pii&#x2F;S1053811920302445?ref=rafaelds&quot;&gt;study from Drexel University&lt;&#x2F;a&gt; has shown that creating something and solving a problem invokes a certain reward response from the brain. This response produces pleasurable experiences similar to food intake, positive social experiences, addictive drugs, and orgasms. You can say that our brain rewards us for being creative.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;thisisengineering-raeng-aL2rxQhEfAM-unsplash.jpg&quot; alt=&quot;img&quot; &#x2F;&gt;Make your idea a reality - Source: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;aL2rxQhEfAM?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Being a Flutter engineer makes you a creator. You can create solutions for other people&#x27;s problems. If you discover a problem and think of a solution, you can immediately test a solution by yourself.&lt;&#x2F;p&gt;
&lt;p&gt;Having the ability to quickly write up an app gives you the opportunity to provide instant value especially if it involves a technical approach. If you come up with an interesting business idea or even join a [hackathon](https:&#x2F;&#x2F;www.techtarget.com&#x2F;searchcio&#x2F;definition&#x2F;hackathon?ref=rafaelds#:~:text=A hackathon%2C also known as,build a new software program.), you can easily spin up a product on a browser or desktop and have something to show for it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-financial-stability&quot;&gt;2 - Financial stability&lt;&#x2F;h2&gt;
&lt;p&gt;The technology space is one of the most lucrative fields to have a career in 2024. A quick lookup at the current market in Sydney shows an &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.glassdoor.com.au&#x2F;Salaries&#x2F;software-engineer-salary-SRCH_IM962_KO0,17.htm?ref=rafaelds&quot;&gt;average salary of $130,000 AUD&lt;&#x2F;a&gt; for entry to mid-level software engineering roles. This may widely vary from where you live or your experience but it&#x27;s hard to deny the monetary opportunity it can provide.&lt;&#x2F;p&gt;
&lt;p&gt;As for Flutter-specific developer roles, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flexiple.com&#x2F;flutter&#x2F;salary&#x2F;?ref=rafaelds&quot;&gt;Flexiple&lt;&#x2F;a&gt; has provided data from different sources that show a similar average salary in the US market.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;flexiple-2024-salary.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Flexiple&#x27;s 2024 Flutter salary data&lt;&#x2F;p&gt;
&lt;p&gt;The game has changed now in terms of software engineering opportunities. Many tech start-ups and companies are still on the lookout for developers. Even non-technical businesses are starting to have their own digital transformation, which in turn creates high demands for engineers.&lt;&#x2F;p&gt;
&lt;p&gt;Getting a job from these companies still depends on your skills, but knowing how many tech roles there are indicates the strong demand for software engineers.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-freelancing-opportunities&quot;&gt;3 - Freelancing opportunities&lt;&#x2F;h2&gt;
&lt;p&gt;Working full-time for a company might not be the best fit for you. You may have dependents or might be in a certain condition where you cannot work on a full-time schedule.&lt;&#x2F;p&gt;
&lt;p&gt;Being a Flutter developer gives you the opportunity to work on a freelance basis. Online portals such as Fiverr and UpWork have businesses looking for developers to build their apps from scratch.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;kristin-wilson-z3htkdHUh5w-unsplash.jpg&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Maybe work in Iceland - Source: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;z3htkdHUh5w?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I had a colleague who used to move constantly from one place to another while working according to her local time zone at the time. As long as you have your computer, an internet connection, and skills, you can do your projects wherever you are.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-entrepreneurial-endeavours&quot;&gt;4 - Entrepreneurial endeavours&lt;&#x2F;h2&gt;
&lt;p&gt;Building a business is one of the many paths you can take when you start learning Flutter.&lt;&#x2F;p&gt;
&lt;p&gt;As a Flutter developer, you already have what it takes to build something that generates revenue. Whether you build an app and sell it on different app stores or have subscription-based services, your skills will give you a push to start building.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;should-i-learn-flutter-in-2023&#x2F;austin-distel-RX_0vwSPiWs-unsplash.jpg&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Deliver product fast for early feedback - Source: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;s&#x2F;photos&#x2F;startup?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText&quot;&gt;Unsplash&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can also build a start-up based on your ideas which you would pitch with an actual proof-of-concept you just built. Showing an actual app on a device is a much more effective pitch than pure speech.&lt;&#x2F;p&gt;
&lt;p&gt;Another great by-product of running a business is creating jobs for other people. While you&#x27;re earning money, you also help your team achieve financial stability. It&#x27;s a win-win situation for everyone.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;final-thoughts&quot;&gt;Final thoughts&lt;&#x2F;h1&gt;
&lt;p&gt;Many of the things I shared are based on my experiences as an app developer. I have first-hand experience with many of the points I mentioned and am still working to achieve the rest.&lt;&#x2F;p&gt;
&lt;p&gt;I hope that I have inspired you to try out app development. Whether you have a tech background or not, &lt;strong&gt;you will succeed&lt;&#x2F;strong&gt; if you dedicate your time to working on it.&lt;&#x2F;p&gt;
&lt;p&gt;Start learning Flutter and start giving value to yourself and others. Happy coding!&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flutter.dev&#x2F;?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;flutter.dev&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.sciencedirect.com&#x2F;science&#x2F;article&#x2F;pii&#x2F;S1053811920302445?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;www.sciencedirect.com&#x2F;science&#x2F;article&#x2F;pii&#x2F;S1053811920302445&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;au.talent.com&#x2F;salary?job=software+engineer&amp;amp;ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;au.talent.com&#x2F;salary?job=software+engineer&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flexiple.com&#x2F;flutter&#x2F;salary&#x2F;?ref=rafaelds&quot;&gt;https:&#x2F;&#x2F;flexiple.com&#x2F;flutter&#x2F;salary&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Flutter Design Challenge #1 - Product Page</title>
        <published>2023-07-07T00:00:00+00:00</published>
        <updated>2023-07-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/flutter-design-challenge-1-product-page/"/>
        <id>https://rafaelds.com/posts/flutter-design-challenge-1-product-page/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/flutter-design-challenge-1-product-page/">&lt;p&gt;Welcome to the first Flutter design challenge of rafaelds. Here, we find a beautiful design and try to write it up and implement it into an app.&lt;&#x2F;p&gt;
&lt;p&gt;The goal of design challenges is not to implement the full product itself, but instead make up the design in Flutter so everyone including me can learn and apply the learnings in our own apps.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-design&quot;&gt;The design&lt;&#x2F;h1&gt;
&lt;p&gt;For our first design challenge, we will implement a simple shopping page that contains a photo of the product, a favourite button, and a buy button. I found a great mock-up in Dribbble to illustrate this concept.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;original-product-page.webp&quot; alt=&quot;original-product-page&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For our app, I will use a different photo and change a few bits in the content to make it a little more original.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;app-outcome&quot;&gt;App outcome&lt;&#x2F;h1&gt;
&lt;p&gt;After writing up the code for our challenge, here&#x27;s what it looks like.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-demo-video.gif&quot; alt=&quot;dc1-demo-video&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;resources&quot;&gt;Resources&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s start by defining the resources that we need for this challenge.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-colour-palette&quot;&gt;1. Colour Palette&lt;&#x2F;h2&gt;
&lt;p&gt;The colours we used in this project are the following:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Orange - &lt;code&gt;Color(0xFFF6AB53)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Black - &lt;code&gt;Color(0xFF161616)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;White - &lt;code&gt;Color(0xFFFCFCFC)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;2-image&quot;&gt;2. Image&lt;&#x2F;h2&gt;
&lt;p&gt;The photo we will be using is from &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;3t6rfsCaWiQ&quot;&gt;Unsplash by Tamara Bellis&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-font&quot;&gt;3. Font&lt;&#x2F;h2&gt;
&lt;p&gt;The font used from Dribbble is &lt;code&gt;Neogrotesk Pro&lt;&#x2F;code&gt;  but since it requires a license to use, I chose a font that looks similar called &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fonts.google.com&#x2F;specimen&#x2F;Work+Sans&quot;&gt;WorkSans&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-simulator&quot;&gt;4. Simulator&lt;&#x2F;h2&gt;
&lt;p&gt;I will use an &lt;strong&gt;iPhone 14 simulator&lt;&#x2F;strong&gt; in this challenge, so all the padding dimensions will be appropriate for this phone&#x27;s size.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;screen-layout&quot;&gt;Screen layout&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s determine the different parts of this screen and how they are arranged. We have a full-screen photo behind a content box. This content box is on the same plane as the bottom bar which contains two buttons: the favourite button and the buy button.&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have defined the main components of this screen, we can do a quick implementation to demonstrate this arrangement.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc-1-original-and-own-layout.png&quot; alt=&quot;dc-1-original-and-own-layout&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;main_screen.dart&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-initial-scaffold-dart.png&quot; alt=&quot;dc1-initial-scaffold-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I use a &lt;code&gt;Scaffold&lt;&#x2F;code&gt; widget to contain the components on this screen. I will use the scaffold&#x27;s &lt;code&gt;bottomNavigationBar&lt;&#x2F;code&gt; property to contain the buttons bar widget. Scaffold also has a &lt;code&gt;body&lt;&#x2F;code&gt; property that contains the main content. For the &lt;code&gt;body&lt;&#x2F;code&gt;, we will use a &lt;code&gt;Stack&lt;&#x2F;code&gt; widget to contain two widgets: the image and the content container. We use a stack so we can put elements on top of each other.&lt;&#x2F;p&gt;
&lt;p&gt;Now it&#x27;s time to replace each component with the right widgets.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementing-the-image-widget&quot;&gt;Implementing the image widget&lt;&#x2F;h1&gt;
&lt;p&gt;For the primary image, we will use an &lt;code&gt;Image.asset()&lt;&#x2F;code&gt; widget. This is the best option for static images that are already included in the asset directory of your project.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-step-1-image.png&quot; alt=&quot;dc1-step-1-image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;transform-the-image&quot;&gt;Transform the image&lt;&#x2F;h2&gt;
&lt;p&gt;That image seems a bit small, so we can transform its size and position it accordingly. To do this, wrap the &lt;code&gt;Image.asset()&lt;&#x2F;code&gt; with a &lt;code&gt;Transform&lt;&#x2F;code&gt; widget.&lt;&#x2F;p&gt;
&lt;p&gt;To increase the size, we can use a &lt;code&gt;Transform.scale()&lt;&#x2F;code&gt; and set the scale accordingly. To adjust the position of the image, we can use the &lt;code&gt;Transform.transalate()&lt;&#x2F;code&gt; and move the image vertically so we can see the clothes we are selling clearly.&lt;&#x2F;p&gt;
&lt;p&gt;I used &lt;code&gt;scale = 2.2&lt;&#x2F;code&gt; and &lt;code&gt;imageOffset = Offset(x = 0, y = 340)&lt;&#x2F;code&gt; to achieve my desired outcome for an iPhone 14 screen.&lt;&#x2F;p&gt;
&lt;p&gt;The last thing to note is that the edges of the image should be rounded. Let&#x27;s trim the corners to be rounded by using the &lt;code&gt;ClipRRect&lt;&#x2F;code&gt; widget, which stands for &lt;em&gt;ClipRoundedRectangle&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-step-2-image.png&quot; alt=&quot;dc1-step-2-image&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After doing these changes, we can make a reusable widget that accepts a scale, position offset, and the source image to use as parameters. I will name this &lt;code&gt;RoundedCornerImage&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;rounded_corner_image.dart&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-rounded-corner-image-dart.png&quot; alt=&quot;dc1-rounded-corner-image-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After updating the &lt;code&gt;main_screen.dart&lt;&#x2F;code&gt; with the new widget we created, the screen should be updated with a bigger photo that highlights the product better.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementing-the-product-content&quot;&gt;Implementing the product content&lt;&#x2F;h1&gt;
&lt;p&gt;The product content is the white container that contains the product name, brand, and description. Writing code for this would have been a simple &lt;code&gt;Container&lt;&#x2F;code&gt;, but because of the curves found on the top left and top right corners, we cannot simply use a container.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-clippath-widget&quot;&gt;Using the ClipPath widget&lt;&#x2F;h2&gt;
&lt;p&gt;One approach to do these curved corners is to use the &lt;code&gt;ClipPath&lt;&#x2F;code&gt; widget and write our own &lt;code&gt;CustomClipper&amp;lt;Path&amp;gt;&lt;&#x2F;code&gt;. Check out the official tutorial for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;ClipPath-class.html&quot;&gt;ClipPaths&lt;&#x2F;a&gt; but for a simple explanation, the custom clipper should draw a rectangle base, with the top left curved clockwise, and the top right curved counter-clockwise.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;FancyClipPath implementation&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-fancy-clip-path-dart.png&quot; alt=&quot;dc1-fancy-clip-path-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next thing to do is to create a &lt;code&gt;Container&lt;&#x2F;code&gt; that has a minimum and maximum height and can contain a widget &lt;em&gt;(Column of texts)&lt;&#x2F;em&gt; that describes our product. Let&#x27;s make this a reusable widget too and name it &lt;code&gt;CurvedCornerContainer&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-curved-container-dart.png&quot; alt=&quot;dc1-curved-container-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once we replace the purple box with this container, it should show a white box with our fancy corners on top.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-step3-curve-container.png&quot; alt=&quot;dc1-step3-curve-container&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;adding-the-description-content&quot;&gt;Adding the description content&lt;&#x2F;h2&gt;
&lt;p&gt;The content can be a simple &lt;code&gt;Column&lt;&#x2F;code&gt; with three &lt;code&gt;Text&lt;&#x2F;code&gt; widgets as its children. The important part here is the different text styles used for each text. As we have included the necessary font in this project &lt;em&gt;(WorkSans)&lt;&#x2F;em&gt;, it is just a matter of using different &lt;code&gt;fontWeight&lt;&#x2F;code&gt; , &lt;code&gt;fontSize&lt;&#x2F;code&gt; , and paddings in between.&lt;&#x2F;p&gt;
&lt;p&gt;After writing the code for this, we can make this a reusable widget and call it &lt;code&gt;DescriptionContent&lt;&#x2F;code&gt; which accepts a product title, brand, and description.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-description-content-dart.png&quot; alt=&quot;dc1-description-content-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We should now use this widget and put this as a child of the &lt;code&gt;CurvedCornerContainer&lt;&#x2F;code&gt; that we just wrote. The screen should look much closer to our chosen design!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-step4-content-text.png&quot; alt=&quot;dc1-step4-content-text&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementing-the-buttons-bar&quot;&gt;Implementing the buttons bar&lt;&#x2F;h1&gt;
&lt;p&gt;The last component that we will write is the buttons bar. It contains two buttons laid out vertically, so we will use a &lt;code&gt;Row&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;favourite-button&quot;&gt;Favourite button&lt;&#x2F;h2&gt;
&lt;p&gt;The favourite button is implemented using a &lt;code&gt;MaterialButton&lt;&#x2F;code&gt;. To replicate the button in the design, we have to set the &lt;code&gt;shape&lt;&#x2F;code&gt; property of the button to &lt;code&gt;CircleBorder&lt;&#x2F;code&gt; which has a grey border with a very thin width.&lt;&#x2F;p&gt;
&lt;p&gt;For the icon itself, we use an &lt;code&gt;Icon&lt;&#x2F;code&gt; widget that uses the &lt;code&gt;Icons.favorite&lt;&#x2F;code&gt; as the image. To make this image smaller, use &lt;code&gt;Padding&lt;&#x2F;code&gt; to push the image into a smaller size.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;favourite_button.dart&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-favourite-button-dart.png&quot; alt=&quot;dc1-favourite-button-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;buy-button&quot;&gt;Buy button&lt;&#x2F;h2&gt;
&lt;p&gt;The same thing will be used for the &lt;code&gt;BuyButton&lt;&#x2F;code&gt;. We use a &lt;code&gt;MaterialButton&lt;&#x2F;code&gt; to wrap a &lt;code&gt;Text&lt;&#x2F;code&gt; widget. In order to get the desired shape, we use a &lt;code&gt;RoundedRectangleBorder&lt;&#x2F;code&gt; as the &lt;code&gt;shape&lt;&#x2F;code&gt; property of the &lt;code&gt;MaterialButton&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We also need to make sure that the button colour is black and the text should be white.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;buy_button.dart&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-buy-button-dart.png&quot; alt=&quot;dc1-buy-button-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;buttons-bar-implementation&quot;&gt;Buttons bar implementation&lt;&#x2F;h1&gt;
&lt;p&gt;As mentioned before, the buttons bar will be implemented using a &lt;code&gt;Row&lt;&#x2F;code&gt; widget to hold the buttons horizontally.&lt;&#x2F;p&gt;
&lt;p&gt;One thing to note here is that the &lt;code&gt;BuyButton&lt;&#x2F;code&gt; fills up the remaining space next to the &lt;code&gt;FavouriteButton&lt;&#x2F;code&gt;. To do this behaviour, we need to wrap the &lt;code&gt;BuyButton&lt;&#x2F;code&gt; with an &lt;code&gt;Expanded&lt;&#x2F;code&gt; widget to fill up the remaining space in the &lt;code&gt;Row&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We also need to put appropriate &lt;code&gt;Padding&lt;&#x2F;code&gt; so the sizes and spacings will look more natural.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;buttons_bar.dart&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-buttons-bar-dart.png&quot; alt=&quot;dc1-buttons-bar-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once we replace the &lt;code&gt;Scaffold&lt;&#x2F;code&gt;&#x27;s &lt;code&gt;bottomNavigationBar&lt;&#x2F;code&gt; with the &lt;code&gt;ButtonsBar&lt;&#x2F;code&gt; widget, our app will look almost the same as our design inspiration.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-step5-buttons-bar.png&quot; alt=&quot;dc1-step5-buttons-bar&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;white-border-outline&quot;&gt;White border outline&lt;&#x2F;h1&gt;
&lt;p&gt;In the original design, there is a white outline along the border of the screen. We produce the same effect by adding a &lt;code&gt;Padding&lt;&#x2F;code&gt; to our &lt;code&gt;Stack&lt;&#x2F;code&gt; of widgets.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;main-screen-implementation&quot;&gt;Main screen implementation&lt;&#x2F;h1&gt;
&lt;p&gt;Once we have converted everything, our &lt;code&gt;MainScreen&lt;&#x2F;code&gt; widget should be complete. Mainly, it is a &lt;code&gt;Scaffold&lt;&#x2F;code&gt; that consists of a customised &lt;code&gt;bottomNavigationBar&lt;&#x2F;code&gt; and a &lt;code&gt;Stack&lt;&#x2F;code&gt; that consists of the product image and the content description.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-final-main-screen-dart.png&quot; alt=&quot;dc1-final-main-screen-dart&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;final-result&quot;&gt;Final result&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;flutter-design-challenge-1-product-page&#x2F;dc1-compraison-output.png&quot; alt=&quot;dc1-compraison-output&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This side-by-side comparison tells us that our design challenge was a success! This product page was implemented without using external libraries and was built using widgets included in the Flutter library.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;further-improvements&quot;&gt;Further improvements&lt;&#x2F;h1&gt;
&lt;p&gt;As you get more experienced with Flutter, you will learn that hard-coded values do not work in a real-world setting. While the values that we use in this project worked on an iPhone 14 device, this will not look as good in an iPhone SE, or worse, on a tablet.&lt;&#x2F;p&gt;
&lt;p&gt;These considerations are out of scope for this design challenge but are something to keep in mind when developing real apps.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;dribbble.com&#x2F;shots&#x2F;19772402-Marketplace-Mobile-IOS-App&quot;&gt;Dribbble mock-up designed by Dinara Malysheva&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;unsplash.com&#x2F;photos&#x2F;3t6rfsCaWiQ&quot;&gt;Unsplash by Tamara Bellis&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fonts.google.com&#x2F;specimen&#x2F;Work+Sans&quot;&gt;Google Font - WorkSans&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Rows in Flutter - Arrange Flutter widgets horizontally</title>
        <published>2023-06-30T00:00:00+00:00</published>
        <updated>2023-06-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/rows-in-flutter-arrange-flutter-widgets-horizontally/"/>
        <id>https://rafaelds.com/posts/rows-in-flutter-arrange-flutter-widgets-horizontally/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/rows-in-flutter-arrange-flutter-widgets-horizontally/">&lt;p&gt;There are a few basic Flutter layout widgets that you will always use in all your projects, one of which is the &lt;code&gt;Row&lt;&#x2F;code&gt; widget. The &lt;code&gt;Row&lt;&#x2F;code&gt; widget allows you to arrange the layout of your components horizontally.&lt;&#x2F;p&gt;
&lt;p&gt;For vertical arrangements, see the &lt;code&gt;Column&lt;&#x2F;code&gt; layout widget.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-a-row&quot;&gt;Using a Row&lt;&#x2F;h2&gt;
&lt;p&gt;The main parameter when using a &lt;code&gt;Row&lt;&#x2F;code&gt; widget is the &lt;code&gt;children&lt;&#x2F;code&gt; property which accepts a list of child widgets to arrange horizontally.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;rows-in-flutter-arrange-flutter-widgets-horizontally&#x2F;flutter-row-image-example.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Row example&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;rows-in-flutter-arrange-flutter-widgets-horizontally&#x2F;simple-row-code.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Example of Row code in Flutter&lt;&#x2F;p&gt;
&lt;h2 id=&quot;row-properties&quot;&gt;Row properties&lt;&#x2F;h2&gt;
&lt;p&gt;Aside from the required &lt;code&gt;children&lt;&#x2F;code&gt; parameter, it is important to know the other properties to use the &lt;code&gt;Row&lt;&#x2F;code&gt; widget effectively.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;axis-alignment&quot;&gt;Axis Alignment&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; handles how the child widgets are arranged along the horizontal space. The types of &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; that can be used include &lt;code&gt;start&lt;&#x2F;code&gt;, &lt;code&gt;end&lt;&#x2F;code&gt;, &lt;code&gt;center&lt;&#x2F;code&gt;, &lt;code&gt;spaceAround&lt;&#x2F;code&gt;, &lt;code&gt;spaceEvenly&lt;&#x2F;code&gt; and &lt;code&gt;spaceBetween&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, the &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; arranges the child widget along the vertical space. Values of this alignment include  &lt;code&gt;start&lt;&#x2F;code&gt;, &lt;code&gt;end&lt;&#x2F;code&gt;, &lt;code&gt;center&lt;&#x2F;code&gt;, &lt;code&gt;stretch&lt;&#x2F;code&gt;, and &lt;code&gt;baseline&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;horizontal-size&quot;&gt;Horizontal Size&lt;&#x2F;h3&gt;
&lt;p&gt;When using the &lt;code&gt;Row&lt;&#x2F;code&gt; widget, the horizontal size stretches according to its child widgets and as allowed by its parent widget. You can override this behaviour by setting the &lt;code&gt;mainAxisSize&lt;&#x2F;code&gt; to &lt;code&gt;MainAxisSize.min&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;direction&quot;&gt;Direction&lt;&#x2F;h3&gt;
&lt;p&gt;Child widgets in a &lt;code&gt;Row&lt;&#x2F;code&gt; are laid out from left to right by default. To change this behaviour, you can override the &lt;code&gt;horizontalDirection&lt;&#x2F;code&gt; property.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;when-to-use-rows&quot;&gt;When to use Rows&lt;&#x2F;h2&gt;
&lt;p&gt;Use &lt;code&gt;Rows&lt;&#x2F;code&gt; when you want to arrange static widgets horizontally. For example, we could use a &lt;code&gt;Row&lt;&#x2F;code&gt; to horizontally arrange the logo, the description part which is a &lt;code&gt;Column&lt;&#x2F;code&gt; layout, and the options button in a job advertisement widget.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;rows-in-flutter-arrange-flutter-widgets-horizontally&#x2F;job-ad-widget-centered.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Job ad using Row&lt;&#x2F;p&gt;
&lt;h2 id=&quot;issues-with-rows&quot;&gt;Issues with Rows&lt;&#x2F;h2&gt;
&lt;p&gt;Because a &lt;code&gt;Row&lt;&#x2F;code&gt; is not a scrollable widget, one common issue developers encounter is that when a Row&#x27;s child widgets increased in size much more than it is allowed to, the app would show an overflow error.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;rows-in-flutter-arrange-flutter-widgets-horizontally&#x2F;job-ad-widget-overflow.png&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Size of the blue box extending outside the Row&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-1-use-expanded-or-flexible&quot;&gt;Solution 1: Use Expanded or Flexible&lt;&#x2F;h3&gt;
&lt;p&gt;A solution for this issue is to use an &lt;code&gt;Expanded&lt;&#x2F;code&gt; or &lt;code&gt;Flexible&lt;&#x2F;code&gt; layout widget to wrap the big child widgets inside a &lt;code&gt;Row&lt;&#x2F;code&gt;. This makes the child widgets ready to overflow safely when their content is dynamic.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-2-use-a-scrollview&quot;&gt;Solution 2: Use a ScrollView&lt;&#x2F;h3&gt;
&lt;p&gt;Another way to make a &lt;code&gt;Row&lt;&#x2F;code&gt; scrollable is by wrapping it with a &lt;code&gt;SingleChildScrollView&lt;&#x2F;code&gt; or any widget that lets its child widgets scroll. This is used for rare situations where you have a scrollable row with a fixed item count.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-3-use-a-listview&quot;&gt;Solution 3: Use a ListView&lt;&#x2F;h3&gt;
&lt;p&gt;If you are planning to make a row that is scrollable with dynamic content, check out &lt;code&gt;ListView&lt;&#x2F;code&gt; for displaying a list of similar widgets. One example that comes to mind is when one scrolls an album horizontally from the Spotify app.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds&#x2F;content&#x2F;images&#x2F;2023&#x2F;06&#x2F;horizontal-scroll.gif&quot; alt=&quot;img&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Example of horizontal scrolling&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;Here&#x27;s a short introduction to using &lt;code&gt;Rows&lt;&#x2F;code&gt; in Flutter. It is important to keep the usage of &lt;code&gt;Rows&lt;&#x2F;code&gt; to arrange cohesive widgets horizontally. Finally, keep your content adaptable to size constraints by wrapping child widgets with &lt;code&gt;Flexible&lt;&#x2F;code&gt; or &lt;code&gt;Expanded&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Row-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Row-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>What does a Flutter code structure look like to a beginner? - An Introduction</title>
        <published>2023-06-28T00:00:00+00:00</published>
        <updated>2023-06-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/what-does-a-flutter-code-structure-look-like-to-a-beginner/"/>
        <id>https://rafaelds.com/posts/what-does-a-flutter-code-structure-look-like-to-a-beginner/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/what-does-a-flutter-code-structure-look-like-to-a-beginner/">&lt;p&gt;Back when I started learning Flutter app development many years ago, I was surprised at how the code structure of Flutter was presented. When I learned that you only need &lt;code&gt;Dart&lt;&#x2F;code&gt; code to make Flutter apps, this concept of a single language to encompass both &lt;em&gt;View logic&lt;&#x2F;em&gt; and &lt;em&gt;Business logic&lt;&#x2F;em&gt; was foreign to me.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Messy, brackets, nesting hell. It&amp;#x27;s a mess!&quot; title=&quot;Messy, brackets, nesting hell. It&amp;#x27;s a mess!&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;hello-world-example-flutter.8903255f478e413b.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;hello-world-example-flutter.3344dbf334de25fd.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;hello-world-example-flutter.8903255f478e413b.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;The first time I looked at the &lt;code&gt;Hello World&lt;&#x2F;code&gt; Flutter code example, I shrugged my head and said to myself how absurd this is. Because I was a more naive developer back then, looking at the hello world app in &lt;em&gt;one file&lt;&#x2F;em&gt;, and in &lt;em&gt;one language&lt;&#x2F;em&gt; made me think how bad this design was.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, I was wrong. Not only can you separate these logic blocks cleanly, but this design decision also made Flutter one of the easiest app-development frameworks to learn these days.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;this-nested-flutter-code&quot;&gt;This nested Flutter code&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s great to look at a source code that clearly tells you exactly what it does. Let&#x27;s look at an example of a UI element I wrote using Flutter.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Job Advertisement Card&quot; title=&quot;Job Advertisement Card&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-flutter.8dc4b0606739cd2b.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-flutter.3c4070b6175afb31.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-flutter.8dc4b0606739cd2b.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Now, without knowing how to read or write &lt;code&gt;Dart&lt;&#x2F;code&gt; code or not knowing what a &lt;code&gt;Widget&lt;&#x2F;code&gt; is, let&#x27;s see if you can understand this nested code and what it represents.&lt;&#x2F;p&gt;




&lt;img alt=&quot;The Job Advertisement Card written in Dart&quot; title=&quot;The Job Advertisement Card written in Dart&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-simple-code.e98fa21e092b20f4.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-simple-code.45f8da91bb75ab14.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-simple-code.e98fa21e092b20f4.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;To me, this code is straightforward when it comes to telling me how this job advertisement card is implemented. It is a nest of objects where each object can have a &lt;code&gt;child&lt;&#x2F;code&gt; or &lt;code&gt;children&lt;&#x2F;code&gt; if it is more than one. A &lt;code&gt;Row&lt;&#x2F;code&gt; arranges its children horizontally, a &lt;code&gt;Column&lt;&#x2F;code&gt; vertically, and so on.&lt;&#x2F;p&gt;
&lt;p&gt;You might already have a clue, but the &lt;code&gt;StarRating&lt;&#x2F;code&gt; object there is made of a few nested objects as well.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Disclaimer: Paddings and spacings are implemented within the objects in the sample code for simplification and demonstration purposes.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Thinking back on my first look at the &lt;code&gt;Hello World&lt;&#x2F;code&gt; example from the Flutter team made me realise that any code can be refactored and improved to be more readable and reusable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;widgets-everywhere&quot;&gt;Widgets everywhere&lt;&#x2F;h2&gt;
&lt;p&gt;Another popular saying in the Flutter community is &lt;em&gt;&quot;Everything in Flutter is a Widget&quot;&lt;&#x2F;em&gt;. While it is not technically 100% true, there is still some truth to that.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Everything in Flutter is a Widget, sort of.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;A Flutter &lt;code&gt;Widget&lt;&#x2F;code&gt; is the basic unit of the user interface in Flutter. It can be described as a &lt;em&gt;unit of composition&lt;&#x2F;em&gt; and &lt;em&gt;the building block of a Flutter app&lt;&#x2F;em&gt;. Almost everything that you see in a Flutter app is a &lt;code&gt;Widget&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using the example above, the &lt;code&gt;Row&lt;&#x2F;code&gt;, &lt;code&gt;Column&lt;&#x2F;code&gt;, and so on are &lt;code&gt;Widgets&lt;&#x2F;code&gt;. The way they are arranged together, the nesting of parent and child &lt;code&gt;Widgets&lt;&#x2F;code&gt;, is called a &lt;code&gt;Widget Tree&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Job ad widget tree&quot; title=&quot;Job ad widget tree&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-tree.dceb34c6b7ac3752.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-tree.8de4784c61db7c4a.png 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;It is important for us developers to know this tree structure because we will need to pass data from one &lt;code&gt;Widget&lt;&#x2F;code&gt; to another, and knowing how our app is structured helps us plan how to do so.&lt;&#x2F;p&gt;
&lt;p&gt;This widget tree also plays a significant role when it comes to speed and memory optimisation. This involves two more sets of trees called &lt;code&gt;Element Tree&lt;&#x2F;code&gt; and &lt;code&gt;RenderObject Tree&lt;&#x2F;code&gt;, but is out of scope and will be discussed in another article.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;flutter-s-composability&quot;&gt;Flutter&#x27;s composability&lt;&#x2F;h2&gt;
&lt;p&gt;Take a look again at our job card code and widget tree. It is very much simplified and is composed of a few widgets. The Flutter framework was designed with the composition and reusability of &lt;code&gt;Widgets&lt;&#x2F;code&gt; in mind. Of course, there are proper ways to nest &lt;code&gt;Widgets&lt;&#x2F;code&gt; but for now, it is best to understand that you can.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Job ad widgets&amp;#x27; composition of basic widgets&quot; title=&quot;Job ad widgets&amp;#x27; composition of basic widgets&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-composition.d293a1210bb5c45b.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-widget-composition.3ff1a7753b95f47c.png 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Some of the widgets that you see in the job card widget tree were built from other widgets. The &lt;code&gt;StarRating&lt;&#x2F;code&gt; widget for example is implemented using a &lt;code&gt;Row&lt;&#x2F;code&gt; with children composed of &lt;code&gt;Image&lt;&#x2F;code&gt; widgets.&lt;&#x2F;p&gt;
&lt;p&gt;While this is a common pattern that you&#x27;ll see in general software design, Flutter under the hood has its own optimisations in which changing a widget&#x27;s state may or may not rebuild the other widgets in the tree. (See &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;flutter-community&#x2F;the-layer-cake-widgets-elements-renderobjects-7644c3142401&quot;&gt;Frederik&#x27;s layer cake explanation&lt;&#x2F;a&gt; for a deep-dive explanation).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;user-input&quot;&gt;User input&lt;&#x2F;h2&gt;
&lt;p&gt;The last thing I want to talk about is how Flutter handles user inputs such as button clicks, scrolls, drag, and so on. &lt;code&gt;Widgets&lt;&#x2F;code&gt; such as &lt;code&gt;Button&lt;&#x2F;code&gt; can accept input through an &lt;code&gt;onAction()&lt;&#x2F;code&gt; function callback which varies depending on which input &lt;code&gt;Widget&lt;&#x2F;code&gt; you use.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Any widget can accept user inputs&quot; title=&quot;Any widget can accept user inputs&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-user-input.799e1472940bf918.png&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-user-input.88bd61808597fe7c.png 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;job-ad-card-user-input.799e1472940bf918.png 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;For widgets that don&#x27;t have user inputs, one can just wrap this widget around with something like a &lt;code&gt;GestureDetector&lt;&#x2F;code&gt; widget, and decide which inputs you want your widget to receive.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;closing-remarks&quot;&gt;Closing remarks&lt;&#x2F;h2&gt;
&lt;p&gt;There is so much ground to cover in Flutter, like changing a &lt;code&gt;Widget&lt;&#x2F;code&gt; properties such as changing colour, size, and text (hint: Flutter state management) and we will discuss that eventually. I hope you learned something from this short introductory article about what a Flutter code structure looks like.&lt;&#x2F;p&gt;
&lt;p&gt;If you are new to Flutter or someone who is interested to jump into Flutter development, I guarantee that you will have a pleasant experience learning and building apps that are ready to be deployed on mobile devices, desktop devices, and even the web.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Widget-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Widget-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;flutter-community&#x2F;the-layer-cake-widgets-elements-renderobjects-7644c3142401&quot;&gt;https:&#x2F;&#x2F;medium.com&#x2F;flutter-community&#x2F;the-layer-cake-widgets-elements-renderobjects-7644c3142401&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;resources&#x2F;architectural-overview&quot;&gt;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;resources&#x2F;architectural-overview&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Axis alignment of Columns and Rows in Flutter</title>
        <published>2023-06-22T00:00:00+00:00</published>
        <updated>2023-06-22T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/axis-alignment-of-columns-and-rows-in-flutter/"/>
        <id>https://rafaelds.com/posts/axis-alignment-of-columns-and-rows-in-flutter/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/axis-alignment-of-columns-and-rows-in-flutter/">&lt;p&gt;There are different ways to align widgets within a &lt;code&gt;Column&lt;&#x2F;code&gt; and a &lt;code&gt;Row&lt;&#x2F;code&gt;. Both widgets share similar properties, yet each differs based on their axes. In this tutorial, we will see the effects of applying different types of alignments to both &lt;code&gt;Column&lt;&#x2F;code&gt; and &lt;code&gt;Row&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;column-and-row-refresher&quot;&gt;Column and Row refresher&lt;&#x2F;h2&gt;
&lt;p&gt;A &lt;code&gt;Column&lt;&#x2F;code&gt; widget arranges its child widgets vertically, therefore defining its main axis as the &lt;strong&gt;Y-Axis&lt;&#x2F;strong&gt;.  A &lt;code&gt;Row&lt;&#x2F;code&gt; on the other hand does it horizontally, which means that its main axis is the &lt;strong&gt;X-Axis&lt;&#x2F;strong&gt;. It is important to keep this in mind so we know which properties we should change to achieve a certain result.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;arranging-the-child-widgets-using-mainaxisalignment&quot;&gt;Arranging the child widgets using mainAxisAlignment&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; modifies how the widgets are arranged vertically in a &lt;code&gt;Column&lt;&#x2F;code&gt; and horizontally in a &lt;code&gt;Row&lt;&#x2F;code&gt;. Changing this property affects either the position of the widgets along the main axis or the spacing between the child widgets.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;start&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;&amp;lt;&amp;lt; Change this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  children &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;...&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Row&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;start&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;&amp;lt;&amp;lt; Change this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  children &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;...&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at the following illustrations to learn how each type of main axis alignment affects a &lt;code&gt;Column&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;axis-alignment-of-columns-and-rows-in-flutter&#x2F;column-main-axis-alignment.png&quot; alt=&quot;column-main-axis-alignment&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And now for a &lt;code&gt;Row&lt;&#x2F;code&gt;, changing the &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; arranges the child widgets like in the image below.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;axis-alignment-of-columns-and-rows-in-flutter&#x2F;row-main-axis-alignment.png&quot; alt=&quot;row-main-axis-alignment&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For reference, the &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; properties are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;start&lt;&#x2F;strong&gt;: Move the widgets to the top for a &lt;code&gt;Column&lt;&#x2F;code&gt;, left for a &lt;code&gt;Row&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;end&lt;&#x2F;strong&gt;: Move the widgets to the bottom for a &lt;code&gt;Column&lt;&#x2F;code&gt;, right for a &lt;code&gt;Row&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;center&lt;&#x2F;strong&gt;: Layout the widgets to the centre&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;spaceBetween&lt;&#x2F;strong&gt;: Arrange the widgets with even spaces between them&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;spaceAround&lt;&#x2F;strong&gt;: Arrange widgets with even spaces between each widget, with half of that space above the top widget and below the bottom widget&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;spaceEvenly&lt;&#x2F;strong&gt;: Arrange the widgets with equal spaces on both the top and bottom of each widget&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;what-about-crossaxisalignment&quot;&gt;What about crossAxisAlignment?&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; property arranges the widget opposite of the main axis called the cross axis.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  crossAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CrossAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;start&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;&amp;lt;&amp;lt; Change this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  children &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;...&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Row&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  crossAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; CrossAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;start&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-comment&quot;&gt; &#x2F;&#x2F;&amp;lt;&amp;lt; Change this&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  children &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;...&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Have a look at the different &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; values and how child widgets would look inside a &lt;code&gt;Column&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;axis-alignment-of-columns-and-rows-in-flutter&#x2F;column-cross-axis-alignment.png&quot; alt=&quot;column-cross-axis-alignment&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; in a &lt;code&gt;Row&lt;&#x2F;code&gt;, the child widget arrangement will be along the vertical axis.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;axis-alignment-of-columns-and-rows-in-flutter&#x2F;row-cross-axis-alignment.png&quot; alt=&quot;row-cross-axis-alignment&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For reference, here are the &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; values:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;start&lt;&#x2F;strong&gt;: Move the widgets to the top for a &lt;code&gt;Row&lt;&#x2F;code&gt;, left for a &lt;code&gt;Column&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;end&lt;&#x2F;strong&gt;: Move the widgets to the bottom for a &lt;code&gt;Row&lt;&#x2F;code&gt;, right for a &lt;code&gt;Column&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;center&lt;&#x2F;strong&gt;: Layout the widgets to the centre of the cross axis&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;stretch&lt;&#x2F;strong&gt;: Fills up all available extending along the cross axis&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;baseline&lt;&#x2F;strong&gt;: Move the children along the cross-axis such that their baselines match. It will only work if child widgets have baseline properties&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Using the CrossAxisAlignment.stretch would throw a BoxConstraints error if the parent widget of the &lt;code&gt;Row&lt;&#x2F;code&gt; or &lt;code&gt;Column&lt;&#x2F;code&gt; does not have a constrained width e.g. parent widgets are &lt;code&gt;Row&lt;&#x2F;code&gt; or &lt;code&gt;Column&lt;&#x2F;code&gt; as well.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; and &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; are the main properties to arrange the child widgets of &lt;code&gt;Columns&lt;&#x2F;code&gt; and &lt;code&gt;Rows&lt;&#x2F;code&gt;. Just remember that &lt;code&gt;Column&lt;&#x2F;code&gt; has a vertical main axis and &lt;code&gt;Row&lt;&#x2F;code&gt; has a horizontal main axis. The layouts&#x27; cross axes will be the opposite of their main axes.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Column-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Column-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;appmaking.com&#x2F;flutter-row-column-example&#x2F;&quot;&gt;https:&#x2F;&#x2F;appmaking.com&#x2F;flutter-row-column-example&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Columns in Flutter - Arrange Flutter widgets vertically</title>
        <published>2023-06-21T00:00:00+00:00</published>
        <updated>2023-06-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/columns-in-flutter-arrange-flutter-widgets-vertically/"/>
        <id>https://rafaelds.com/posts/columns-in-flutter-arrange-flutter-widgets-vertically/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/columns-in-flutter-arrange-flutter-widgets-vertically/">&lt;p&gt;One of the key aspects of creating a beautiful user interface is to learn how to use the fundamental widgets of Flutter. The &lt;code&gt;Column&lt;&#x2F;code&gt; widget is one of the most used widgets to arrange the layout of your components.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-a-column&quot;&gt;What is a Column?&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;Column&lt;&#x2F;code&gt; is a fundamental layout widget that allows you to &lt;strong&gt;arrange child widgets vertically&lt;&#x2F;strong&gt;. If you want to set the order of widgets from top to bottom or the other way around, Column is the widget you need.&lt;&#x2F;p&gt;
&lt;p&gt;For horizontal arrangements, see &lt;code&gt;Row&lt;&#x2F;code&gt; layout widget.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementing-a-column&quot;&gt;Implementing a Column&lt;&#x2F;h2&gt;
&lt;p&gt;The most important parameter when using a Column is the &lt;code&gt;children&lt;&#x2F;code&gt; property which accepts a list of child widgets to arrange vertically.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;columns-in-flutter-arrange-flutter-widgets-vertically&#x2F;column-example-image-compressed.png&quot; alt=&quot;column-example-image-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    SplashImage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    HeaderText&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;      &amp;#39;Columns in Flutter&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    BodyText&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;      &amp;#39;Learn how to arrange your widgets vertically while keeping them elegant.&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;    NextButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;column-properties&quot;&gt;Column properties&lt;&#x2F;h2&gt;
&lt;p&gt;On top of the required &lt;code&gt;children&lt;&#x2F;code&gt; parameter, there are other useful properties to keep in mind when using a &lt;code&gt;Column&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;axis-alignment&quot;&gt;Axis Alignment&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;mainAxisAlignment&lt;&#x2F;code&gt; property handles the arrangement of the child widgets along the vertical space. Types of arrangement include &lt;code&gt;start&lt;&#x2F;code&gt;, &lt;code&gt;end&lt;&#x2F;code&gt;, &lt;code&gt;center&lt;&#x2F;code&gt;, &lt;code&gt;spaceAround&lt;&#x2F;code&gt;, &lt;code&gt;spaceEvenly&lt;&#x2F;code&gt; and &lt;code&gt;spaceBetween&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;On the opposite axis, the &lt;code&gt;crossAxisAlignment&lt;&#x2F;code&gt; property handles the arrangement of child widgets along the horizontal space. Values of this type are &lt;code&gt;start&lt;&#x2F;code&gt;, &lt;code&gt;end&lt;&#x2F;code&gt;, &lt;code&gt;center&lt;&#x2F;code&gt;, &lt;code&gt;stretch&lt;&#x2F;code&gt;, and &lt;code&gt;baseline&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vertical-size&quot;&gt;Vertical Size&lt;&#x2F;h3&gt;
&lt;p&gt;By default, &lt;code&gt;Columns&lt;&#x2F;code&gt; stretch vertically as permitted by its parent widget. However, you can override this to only expand according to child widgets&#x27; sizes vertically by setting the &lt;code&gt;mainAxisSize&lt;&#x2F;code&gt; to &lt;code&gt;MainAxisSize.min&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;direction&quot;&gt;Direction&lt;&#x2F;h3&gt;
&lt;p&gt;The last property of a &lt;code&gt;Column&lt;&#x2F;code&gt; is the &lt;code&gt;verticalDirection&lt;&#x2F;code&gt;. This defines whether the widgets should start from top to bottom, or bottom to top.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;when-to-use-columns&quot;&gt;When to use Columns&lt;&#x2F;h2&gt;
&lt;p&gt;The expected use of &lt;code&gt;Columns&lt;&#x2F;code&gt; is to group static widgets vertically. For example, you are designing a card widget that has a title text and a subtitle text. While you can implement this arrangement using other layout widgets such as &lt;code&gt;Stack&lt;&#x2F;code&gt;, it is more straightforward and clear to do so with &lt;code&gt;Columns&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It is important to define your use case when using &lt;code&gt;Columns&lt;&#x2F;code&gt; to avoid unnecessary problems.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;issues-with-columns&quot;&gt;Issues with Columns&lt;&#x2F;h2&gt;
&lt;p&gt;If you have been writing Flutter apps, you have probably encountered an issue where your &lt;code&gt;Column&lt;&#x2F;code&gt; has increased in size to the point where it overflows out of its parent widget.&lt;&#x2F;p&gt;
&lt;p&gt;An issue with &lt;code&gt;Columns&lt;&#x2F;code&gt; is that it is not scrollable by default. When widgets inside the &lt;code&gt;Column&lt;&#x2F;code&gt; grow bigger, overflow happens.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;columns-in-flutter-arrange-flutter-widgets-vertically&#x2F;column-example-overflow-compressed.png&quot; alt=&quot;column-example-overflow-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-1-use-expanded-or-flexible&quot;&gt;Solution 1: Use Expanded or Flexible&lt;&#x2F;h3&gt;
&lt;p&gt;A solution for this issue is to use an &lt;code&gt;Expanded&lt;&#x2F;code&gt; or &lt;code&gt;Flexible&lt;&#x2F;code&gt; layout widget to wrap the big child widgets inside a &lt;code&gt;Column&lt;&#x2F;code&gt;. This makes the child widgets ready to overflow safely when their content is dynamic.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-2-use-a-scrollview&quot;&gt;Solution 2: Use a ScrollView&lt;&#x2F;h3&gt;
&lt;p&gt;Another way to make a &lt;code&gt;Column&lt;&#x2F;code&gt; scrollable is by wrapping it with a &lt;code&gt;SingleChildScrollView&lt;&#x2F;code&gt; or any widget that lets its child widgets scroll. But in most cases, it is much better to use &lt;code&gt;Column&lt;&#x2F;code&gt; to arrange related widgets vertically instead of making a scrollable screen from it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-3-use-a-listview&quot;&gt;Solution 3: Use a ListView&lt;&#x2F;h3&gt;
&lt;p&gt;If you are planning to make a screen that is scrollable with dynamic content, check out &lt;code&gt;ListView&lt;&#x2F;code&gt; for displaying a list of similar widgets or better.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;We just covered a short introduction to using &lt;code&gt;Columns&lt;&#x2F;code&gt; in Flutter. It is important to keep the usage of Columns to arrange cohesive widgets vertically. Finally, keep your content adaptable to size constraints by wrapping child widgets with &lt;code&gt;Flexible&lt;&#x2F;code&gt; or &lt;code&gt;Expanded&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Column-class.html&quot;&gt;https:&#x2F;&#x2F;api.flutter.dev&#x2F;flutter&#x2F;widgets&#x2F;Column-class.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The right way to create a Flutter project</title>
        <published>2023-06-14T00:00:00+00:00</published>
        <updated>2023-06-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/the-right-way-to-create-a-flutter-project/"/>
        <id>https://rafaelds.com/posts/the-right-way-to-create-a-flutter-project/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/the-right-way-to-create-a-flutter-project/">&lt;p&gt;It is very important to create your Flutter project correctly. It will save you a lot of time in the future by naming your project properly, as well as setting up documentation to make your project readable.&lt;&#x2F;p&gt;
&lt;p&gt;Before you start creating your project, make sure you have set up your Flutter tools on your machine. Once your machine is ready, then let&#x27;s get started!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;creating-the-flutter-project&quot;&gt;Creating the Flutter Project&lt;&#x2F;h2&gt;
&lt;p&gt;When creating a Flutter project from scratch, it is important to do it correctly. While you can get away with a simple &lt;code&gt;flutter create&lt;&#x2F;code&gt;, it is much better to include not just the project name, but your &lt;strong&gt;organisation name&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Your organisation name will appear both in your iOS and Android sub-directory projects, so make sure you do this right and do not end up with &lt;strong&gt;com.example&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To do so, run the command:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; create my_awesome_app --org com.rafaelds&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This way, your project will be your own and you won&#x27;t have to worry about renaming packages later.&lt;&#x2F;p&gt;
&lt;p&gt;Another good point that &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;@andrew.ackie?ref=rafaelds&quot;&gt;Andrew&lt;&#x2F;a&gt; raised to minimise your project size is to define only the platforms you are going to build an app for.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; create my_awesome_app --org com.rafaelds --platform=ios,android&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This command will prevent creating app dependencies on other platforms like &lt;code&gt;Web&lt;&#x2F;code&gt;, &lt;code&gt;Desktop&lt;&#x2F;code&gt;, etc.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;💡Creating a project in this way adds the correct files to exclude in .gitignore, so it is best to always start a Flutter project with this.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can also include a description field so that your &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; would be updated. I think updating the description within the pubspec file should be enough.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clean-up-your-pubspec-yaml&quot;&gt;Clean up your pubspec.yaml&lt;&#x2F;h2&gt;
&lt;p&gt;The next thing to do is to clean up the &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt;. This file contains your project&#x27;s Flutter dependency and configurations.&lt;&#x2F;p&gt;
&lt;p&gt;To help you set it up to standards, your &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; file should look something like this. (as of June 2023):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; my_awesome_app&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;description&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; My Awesome App is an app to make everyone awesome.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;publish_to&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;#39;none&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt; # Remove this line if you wish to publish to pub.dev&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;version&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; 1.0.0+1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;environment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  sdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string z-string&quot;&gt; &amp;#39;&amp;gt;=3.0.3 &amp;lt;4.0.0&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;    sdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; flutter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  cupertino_icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^1.0.2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;dev_dependencies&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter_test&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;    sdk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; flutter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  flutter_lints&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ^2.0.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-tag z-yaml&quot;&gt;  uses-material-design&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-separator z-key-value&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language z-boolean&quot;&gt; true&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  # To add assets to your application, add an assets section, like this:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  # assets:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  # - images&#x2F;a_dot_burr.jpeg&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  # - images&#x2F;a_dot_ham.jpeg&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do not forget to add an app description here. This &lt;code&gt;pubspec.yaml&lt;&#x2F;code&gt; also contains commented lines of code for assets in case you want to add images or videos to your app in the future.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clean-up-your-readme-md&quot;&gt;Clean up your README.md&lt;&#x2F;h2&gt;
&lt;p&gt;Many public projects found in GitHub made by personal accounts always have the default &lt;code&gt;README.md&lt;&#x2F;code&gt; starter content.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s best to update it now so that anyone looking at your project will see your efforts in your code.&lt;&#x2F;p&gt;
&lt;p&gt;If you need inspiration for your &lt;code&gt;README.md&lt;&#x2F;code&gt;, you can follow the template below:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;markdown&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-heading z-markdown z-heading z-\31  z-markdown&quot;&gt;# My Awesome App&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-text&quot;&gt;My Awesome App is an app to make everyone awesome.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-heading z-markdown z-heading z-\32  z-markdown&quot;&gt;## Setup&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-text&quot;&gt;Here are the steps to set up your machine to start running My Awesome App&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-text&quot;&gt;&#x2F;&#x2F;TODO: Add more information here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;add-a-license-file&quot;&gt;Add a LICENSE file&lt;&#x2F;h2&gt;
&lt;p&gt;If you are planning to make your project public, you might want to decide which license to use. If it is going to be a private or top-secret project, then it&#x27;s best &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;choosealicense.com&#x2F;no-permission&#x2F;?ref=rafaelds&quot;&gt;to not put any license as it is by default an exclusive copyright&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For public or open-source projects, feel free to check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;choosealicense.com&#x2F;?ref=rafaelds&quot;&gt;Github&#x27;s tool&lt;&#x2F;a&gt; to help you decide which license will fit your use case.&lt;&#x2F;p&gt;
&lt;p&gt;For simplicity&#x27;s sake, use an MIT license if you want to allow other people to use, modify, and improve your project, as long as they keep this &lt;code&gt;LICENSE&lt;&#x2F;code&gt; and copyright notice.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;MIT License&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Copyright (c) [2023] [Rafael Delos Santos]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Permission is hereby granted, free of charge, to any person obtaining a copy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;of this software and associated documentation files (the &amp;quot;Software&amp;quot;), to deal&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;in the Software without restriction, including without limitation the rights&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;to use, copy, modify, merge, publish, distribute, sublicense, and&#x2F;or sell&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;copies of the Software, and to permit persons to whom the Software is&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;furnished to do so, subject to the following conditions:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;The above copyright notice and this permission notice shall be included in all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;copies or substantial portions of the Software.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SOFTWARE.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;add-a-changelog-md-file&quot;&gt;Add a CHANGELOG.md file&lt;&#x2F;h2&gt;
&lt;p&gt;Not everyone wants to add unnecessary files in the project that do not help in making the app run. While &lt;code&gt;CHANGELOG&lt;&#x2F;code&gt; is not required, it is highly encouraged to have one in your project.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;CHANGELOG&lt;&#x2F;code&gt; can easily tell other developers (including you in the future) what has been done in each release. This file provides significant value especially when you publish your very own Flutter library in public.&lt;&#x2F;p&gt;
&lt;p&gt;You can copy this template that I use in my projects for your &lt;code&gt;CHANGELOG&lt;&#x2F;code&gt; and start adding notes every time you add new features to the new version of your app.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;markdown&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-heading z-markdown z-heading z-\31  z-markdown&quot;&gt;# Changelog&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-heading z-markdown z-heading z-\32  z-markdown&quot;&gt;## v1.0.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-list z-begin z-markdown&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-text&quot;&gt; Set up the proper project directory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-list z-begin z-markdown&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-text&quot;&gt; Add hello world feature&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;add-an-app-file&quot;&gt;Add an app file&lt;&#x2F;h2&gt;
&lt;p&gt;Your &lt;code&gt;main.dart&lt;&#x2F;code&gt; file contains all the necessary code to run your initial Flutter project. While this works, putting everything in one file is not the best practice, especially when your project gets bigger.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing to do is create a separate app file to contain the &lt;code&gt;MyApp&lt;&#x2F;code&gt;. This is good practice because you can later put your navigation routing, screens, themes, and more in this file. You can also create other instances of this file for running tests, trying out themes, and other configurations.&lt;&#x2F;p&gt;
&lt;p&gt;Go to your &lt;code&gt;main.dart&lt;&#x2F;code&gt; file and cut the code that contains the &lt;code&gt;MyApp&lt;&#x2F;code&gt; class and paste it into the new file created.&lt;&#x2F;p&gt;
&lt;p&gt;I would suggest renaming the &lt;code&gt;MyApp&lt;&#x2F;code&gt; class to your own app name e.g. &lt;code&gt;MyAwesomeApp&lt;&#x2F;code&gt; using the &lt;strong&gt;Refactor&lt;&#x2F;strong&gt; shortcut (F2).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;add-a-home-page-class-file&quot;&gt;Add a Home Page class file&lt;&#x2F;h2&gt;
&lt;p&gt;You may have noticed as well in the &lt;code&gt;main.dart&lt;&#x2F;code&gt; file that it contains a &lt;code&gt;MyHomePage&lt;&#x2F;code&gt; default widget that contains the very first default screen. Let&#x27;s clean this up by &lt;strong&gt;creating a separate file&lt;&#x2F;strong&gt; for this.&lt;&#x2F;p&gt;
&lt;p&gt;The location of this file should not matter for now, as there are many discussions as to what the right structure of files should be (feature directory vs types directory), but we can deal with that in another tutorial.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;clean-up-the-main-file&quot;&gt;Clean up the main file&lt;&#x2F;h2&gt;
&lt;p&gt;Your &lt;code&gt;main.file&lt;&#x2F;code&gt; should contain less code now after moving both the &lt;code&gt;MyAwesomeApp&lt;&#x2F;code&gt; class and the &lt;code&gt;MyHomePage&lt;&#x2F;code&gt; class to their respective files.&lt;&#x2F;p&gt;
&lt;p&gt;It is better to separate these functions and classes into their respective files as it will help you later the moment you consider having different build configs, running tests and flavour types.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;fix-the-widget-test&quot;&gt;Fix the Widget Test&lt;&#x2F;h2&gt;
&lt;p&gt;When creating a Flutter project, it adds a widget test by default. After doing the reorganisation in the previous steps, make sure to fix the &lt;code&gt;widget_test.dart&lt;&#x2F;code&gt; found under the &lt;code&gt;test&lt;&#x2F;code&gt; directory by fixing the imports and using the correct class names in there.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;updated-file-output&quot;&gt;Updated file output&lt;&#x2F;h2&gt;
&lt;p&gt;After doing these steps and fixing the imports on each file, the different files should look something like the snippets above. All class names and imports have been updated so that it builds just fine.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;new-main-dart&quot;&gt;New main.dart&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:my_awesome_app&#x2F;my_awesome_app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  runApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyAwesomeApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;main.dart&lt;&#x2F;p&gt;
&lt;h3 id=&quot;new-my-awesome-app-dart&quot;&gt;New my_awesome_app.dart&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:my_awesome_app&#x2F;my_home_page.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyAwesomeApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatelessWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyAwesomeApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MaterialApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      title&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;My Awesome App&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ThemeData&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; ColorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;fromSeed&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(seedColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Colors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;deepPurple)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        useMaterial3&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; true&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      home&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyHomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(title&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;Flutter Demo Home Page&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;my_awesome_app.dart&lt;&#x2F;p&gt;
&lt;h3 id=&quot;new-my-home-page-dart&quot;&gt;New my_home_page.dart&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyHomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; StatefulWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyHomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;({super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;key&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; required&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; this&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;title})&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;  final&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; title&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyHomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; createState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _MyHomePageState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; _MyHomePageState&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; State&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;MyHomePage&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  int&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _counter &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;  void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _incrementCounter&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    setState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      _counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;++&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;  @override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;  Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      appBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; AppBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        backgroundColor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(context)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;colorScheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;inversePrimary&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        title&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;title)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      body&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Center&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Column&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          mainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MainAxisAlignment&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;center&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          children&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;: &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;[&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;            const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;              &amp;#39;You have pushed the button this many times:&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;            Text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;              &amp;#39;$&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;              style&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Theme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;of&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(context)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;textTheme&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;headlineMedium&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;            )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;          ]&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      floatingActionButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; FloatingActionButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        onPressed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _incrementCounter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        tooltip&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;Increment&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;        child&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt; const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Icon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;add)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;my_home_page.dart&lt;&#x2F;p&gt;
&lt;h3 id=&quot;new-widget-test-dart&quot;&gt;New widget_test.dart&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter&#x2F;material.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:flutter_test&#x2F;flutter_test.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &amp;#39;package:my_awesome_app&#x2F;my_awesome_app.dart&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; main&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;  testWidgets&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;Counter increments smoke test&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;WidgetTester&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; tester) &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;async&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Build our app and trigger a frame.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    await&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; tester&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pumpWidget&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;const&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; MyAwesomeApp&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;())&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Verify that our counter starts at 0.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    expect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;0&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; findsOneWidget)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    expect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;1&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; findsNothing)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Tap the &amp;#39;+&amp;#39; icon and trigger a frame.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    await&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; tester&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;byIcon&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Icons&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;add))&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    await&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; tester&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;pump&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;    &#x2F;&#x2F; Verify that our counter has incremented.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    expect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;0&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; findsNothing)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    expect&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(find&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;text&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;#39;1&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; findsOneWidget)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  })&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;widget_test.dart&lt;&#x2F;p&gt;
&lt;h2 id=&quot;file-directory&quot;&gt;File Directory&lt;&#x2F;h2&gt;
&lt;p&gt;Once everything has been updated, your Flutter project directory should look something like this. We can discuss in another tutorial how and when to use different file directory structures, but for now, this would suffice.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-right-way-to-create-a-flutter-project&#x2F;directory-hierarchy-flutter-starter.png&quot; alt=&quot;Directory hierarchy in a Flutter starter project&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h1&gt;
&lt;p&gt;In summary, following the steps in this tutorial is one of the better ways to start a Flutter project. While this looks like more work and setting up than just running the default set-up made by Flutter, this approach has saved me lots of time in terms of documentation and implementing new features.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The top 8 Visual Studio Code shortcuts you need to know as a Flutter developer</title>
        <published>2023-06-13T00:00:00+00:00</published>
        <updated>2023-06-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer/"/>
        <id>https://rafaelds.com/posts/the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer/">&lt;p&gt;It is important to master the tools that you use on a daily basis. Not only does it help you produce better outcomes, but it also saves you more time that could be used for something else.&lt;&#x2F;p&gt;
&lt;p&gt;Knowing and using hotkeys and shortcuts in the long term also prevents you from getting &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Repetitive_strain_injury?ref=rafaelds&quot;&gt;Repetitive Strain Injury&lt;&#x2F;a&gt; caused by typing and switching between your mouse and keyboard.&lt;&#x2F;p&gt;
&lt;p&gt;Now let&#x27;s jump right into the important shortcuts that you need to know in Visual Studio Code as a Flutter developer!&lt;&#x2F;p&gt;
&lt;p&gt;If you are not using a macOS keyboard, you can usually replace the CMD shortcut with CTRL in VS Code. However, it is better to search the correct command in your OS through Settings &amp;gt; Keyboard Shortcuts&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-go-to-file&quot;&gt;1. Go to File&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;go-to-file-shortcut-compressed.gif&quot; alt=&quot;go-to-file-shortcut-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CMD + P&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first one is the most used shortcut in my opinion. You will always go from one file to another when working on your project.&lt;&#x2F;p&gt;
&lt;p&gt;Most people will use their mouse, go to the file directory hierarchy and look for the file they need. A much faster way is to use the &lt;strong&gt;Go to File&lt;&#x2F;strong&gt; shortcut.&lt;&#x2F;p&gt;
&lt;p&gt;To use this shortcut, press hotkeys and type the file name that you need to open. Take note that this also uses a &lt;strong&gt;smart search&lt;&#x2F;strong&gt; approach where you can just type a part of the file name and it will show possible results.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-refactor&quot;&gt;2. Refactor&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;refactor-shortcut-compressed-2.gif&quot; alt=&quot;refactor-shortcut-compressed-2&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CTRL + Shift + R&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The second most used shortcut for me is the &lt;strong&gt;Refactor&lt;&#x2F;strong&gt; shortcut. It gives you a lot of options to either improve your code or provide Flutter utility shortcuts.&lt;&#x2F;p&gt;
&lt;p&gt;Here are some of the things Refactor shortcut can do:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Wrap a widget with another widget&lt;&#x2F;li&gt;
&lt;li&gt;Wrap a widget with a builder widget&lt;&#x2F;li&gt;
&lt;li&gt;Extract a selected piece of code into its own method or variable&lt;&#x2F;li&gt;
&lt;li&gt;Extract a selected piece of code into its own widget&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;3-show-all-commands&quot;&gt;3. Show All Commands&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;show-all-commands-shortcut-compressed.gif&quot; alt=&quot;show-all-commands-shortcut-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CMD + Shift + P&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The second shortcut that I like is the &lt;strong&gt;Show all commands&lt;&#x2F;strong&gt; shortcut. You can think of it as the map when you get lost trying to find a shortcut.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to do something, say Go to File, or Format the Document, just type in the shortcut and enter the action that you want to do.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-rename&quot;&gt;4. Rename&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;refactor-shortcut-compressed.gif&quot; alt=&quot;refactor-shortcut-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;F2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Rename&lt;&#x2F;strong&gt; command in Visual Studio Code is like a smart rename feature. This shortcut can save you minutes to hours by pressing this shortcut and entering the new variable or function name.&lt;&#x2F;p&gt;
&lt;p&gt;This will lessen your dependence on &lt;strong&gt;Find and Replace&lt;&#x2F;strong&gt; whenever you want to rename a function name or a variable.&lt;&#x2F;p&gt;
&lt;p&gt;The neat thing about this shortcut is that it renames not only the names within a file but all the other files that have a reference to that across the project.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;5-auto-suggest-and-auto-complete&quot;&gt;5. Auto-suggest and Auto-complete&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CTRL + Space&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;VS Code has &lt;em&gt;IntelliSense&lt;&#x2F;em&gt; where the editor helps you in terms of code completion. Whether you are writing dart code or configuration files, always use this shortcut to save you from typing more than you need.&lt;&#x2F;p&gt;
&lt;p&gt;Most of the time, you won&#x27;t need to press this as VS Code suggests the best completion anyway.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;6-quick-fix&quot;&gt;6. Quick Fix&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;quickfix-shortcut-compressed.gif&quot; alt=&quot;quickfix-shortcut-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CMD + .&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The other side of auto-complete is the &lt;strong&gt;Quick Fix&lt;&#x2F;strong&gt; shortcut. Whenever you see an error or red underline in your code, the first thing you should do is apply the quick fix shortcut. VS Code will show a pop-up window showing possible fixes for your error.&lt;&#x2F;p&gt;
&lt;p&gt;To be much more efficient, if you press the shortcut twice, the error will be fixed using the best option (VS Code will choose the best fix for you)&lt;&#x2F;p&gt;
&lt;p&gt;There are a few quick fixes that VS Code can do for you such as fixing imports, implementing interfaces, and access modifier suggestions.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;7-organise-import&quot;&gt;7. Organise Import&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;organise-import-shortcut-compressed.gif&quot; alt=&quot;organise-import-shortcut-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Shift + Option(ALT) + O&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After working on a file, it should be in your nature to always apply the &lt;strong&gt;Organise Import&lt;&#x2F;strong&gt; shortcut.
This shortcut arranges your imports found at the top of your file. It also removes unused imports, so be sure to always use this after every edit.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;8-select-all-occurrence&quot;&gt;8. Select All Occurrence&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;the-top-8-visual-studio-code-shortcuts-you-need-to-know-as-a-flutter-developer&#x2F;select-all-occurrences-compressed.gif&quot; alt=&quot;select-all-occurrences-compressed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CMD + Shift + L&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are times when the &lt;strong&gt;Rename&lt;&#x2F;strong&gt; shortcut will not work depending on which words or text you want to replace.&lt;&#x2F;p&gt;
&lt;p&gt;For example, you have hard-coded strings in your code that are written repetitively across your file. Instead of using a Find and Replace, you can use the &lt;strong&gt;Select All Occurrence&lt;&#x2F;strong&gt; shortcut to change this in real-time.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;Now that you have learned all the important shortcuts in VS Code, it is up to you to start using them and integrating them into your workflow.&lt;&#x2F;p&gt;
&lt;p&gt;To see all the shortcuts in VS Code, you can find them by going to &lt;code&gt;Settings &amp;gt; Keyboard Shortcuts&lt;&#x2F;code&gt;. You can also customise these hotkeys in the settings.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How to set up your Mac to develop Flutter apps</title>
        <published>2023-06-11T00:00:00+00:00</published>
        <updated>2023-06-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/how-to-set-up-your-mac-to-develop-flutter-apps/"/>
        <id>https://rafaelds.com/posts/how-to-set-up-your-mac-to-develop-flutter-apps/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/how-to-set-up-your-mac-to-develop-flutter-apps/">&lt;p&gt;I hope you are excited to start your Flutter app development journey! I believe that macOS is the best platform to develop a Flutter app, as you can cover multi-platform development all in one machine.&lt;&#x2F;p&gt;
&lt;p&gt;In this article, I will share my own Mac setup and show you how to set up your Mac to start developing Flutter apps.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;&#x2F;h2&gt;
&lt;p&gt;To make your development life easy, here are the apps that you need:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Xcode&lt;&#x2F;li&gt;
&lt;li&gt;Git&lt;&#x2F;li&gt;
&lt;li&gt;Android Studio&lt;&#x2F;li&gt;
&lt;li&gt;Visual Studio Code&lt;&#x2F;li&gt;
&lt;li&gt;iTerm2&lt;&#x2F;li&gt;
&lt;li&gt;Flutter&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Make sure that all these apps are installed on your machine before you start installing Flutter.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-xcode&quot;&gt;Installing Xcode&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;Xcode_14_icon.png&quot; alt=&quot;Xcode_14_icon&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It is important to install Xcode as it contains libraries to run your Flutter code on your physical iOS or a simulator. It also contains SDK licenses with terms and conditions that you need to agree with.&lt;&#x2F;p&gt;
&lt;p&gt;The best way to install Xcode is through the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;apps.apple.com&#x2F;au&#x2F;app&#x2F;xcode&#x2F;id497799835?mt=12&quot;&gt;App Store&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Once installed, install the command lines tools included by running this command in the terminal.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;xcode-select&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; --install&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;installing-git&quot;&gt;Installing Git&lt;&#x2F;h2&gt;
&lt;p&gt;Git is supposed to be installed already after downloading Xcode. If it is not head over to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;git-scm.com&#x2F;download&#x2F;mac&quot;&gt;git-scm&lt;&#x2F;a&gt; to download the latest version of git for your Mac.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-android-studio&quot;&gt;Installing Android Studio&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;android-studio-icon.png&quot; alt=&quot;android-studio-icon&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Android Studio is necessary to help you download Android development SDKs and tools for running your Flutter app in emulators. Check out the latest Android Studio here on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;developer.android.com&#x2F;studio&quot;&gt;Google&#x27;s website&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It is possible to run Flutter apps without Android Studio and just install the necessary command line tools, but it is much easier to just download the whole suite and not worry too much about the specific tools to find.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-visual-studio-code&quot;&gt;Installing Visual Studio Code&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;visual-studio-code-logo.png&quot; alt=&quot;visual-studio-code-logo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I think The best IDE for writing Flutter code in &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;code.visualstudio.com&#x2F;download&quot;&gt;Visual Studio Code&lt;&#x2F;a&gt;. It currently has tons of plugins, templates, and useful shortcuts to make your Flutter journey more enjoyable and streamlined.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t forget to make it a habit of using [[The top 8 Visual Studio Code shortcuts you need to know as a Flutter developer| keyboard shortcuts in Visual Studio Code]] as it will greatly speed up your workflow.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-iterm2&quot;&gt;Installing iTerm2&lt;&#x2F;h2&gt;
&lt;p&gt;I highly suggest you &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;iterm2.com&#x2F;downloads.html&quot;&gt;download &lt;strong&gt;iTerm2&lt;&#x2F;strong&gt;&lt;&#x2F;a&gt; and use it over the default &lt;code&gt;Terminal.app&lt;&#x2F;code&gt;. Our workflow involves more automation and scripting over UI in our domain, so we might as well get the best tool for it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;iterm-screenshot-hello-world.png&quot; alt=&quot;iterm-screenshot-hello-world&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once iTerm2 is installed, I highly encourage you to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;ohmyz.sh&#x2F;&quot;&gt;download &lt;strong&gt;oh-my-zsh&lt;&#x2F;strong&gt;&lt;&#x2F;a&gt; to make your terminal more beautiful and rich with autocomplete tools.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;installing-flutter&quot;&gt;Installing Flutter&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;flutter-logo.png&quot; alt=&quot;flutter-logo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Once you have installed all the recommended apps, it is time to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;get-started&#x2F;install&#x2F;macos&quot;&gt;install the latest version of Flutter&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After downloading and extracting the SDK in your chosen directory, make sure to add the Flutter executable in your shell path so you can run Flutter in your terminal.&lt;&#x2F;p&gt;
&lt;p&gt;Assuming you are using &lt;strong&gt;zsh&lt;&#x2F;strong&gt;, open up your &lt;code&gt;.zshrc&lt;&#x2F;code&gt; and add the following line:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;export&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; PATH&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-other z-normal z-shell&quot;&gt;$PATH&lt;&#x2F;span&gt;&lt;span class=&quot;z-string z-punctuation z-definition z-string&quot;&gt;:[PATH_WHERE_YOU_EXTRACTED_THE_FLUTTER_ZIP]&#x2F;bin&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;verifying-flutter-installation&quot;&gt;Verifying Flutter installation&lt;&#x2F;h2&gt;
&lt;p&gt;The final step is to make sure that everything works. To test if Flutter is working, run the Flutter doctor command:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; doctor&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Normally, this would show up with a few warnings about licenses not being accepted yet, but that&#x27;s okay! Since we have installed Xcode and Android Studio, we can run the following commands to accept these licenses.&lt;&#x2F;p&gt;
&lt;p&gt;For Xcode licenses, run:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; xcodebuild -license accept&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And for Android Studio, run:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;flutter&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; doctor --android-licenses&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully, once you run &lt;code&gt;flutter doctor&lt;&#x2F;code&gt; again, you will see that everything else has been successfully green-ticked!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;posts&#x2F;how-to-set-up-your-mac-to-develop-flutter-apps&#x2F;flutter-doctor-result-successful.png&quot; alt=&quot;flutter-doctor-result-successful&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h1 id=&quot;references&quot;&gt;References&lt;&#x2F;h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;get-started&#x2F;install&#x2F;macos&quot;&gt;https:&#x2F;&#x2F;docs.flutter.dev&#x2F;get-started&#x2F;install&#x2F;macos&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;verygood.ventures&#x2F;blog&#x2F;very-good-flutter-setup&quot;&gt;https:&#x2F;&#x2F;verygood.ventures&#x2F;blog&#x2F;very-good-flutter-setup&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.freecodecamp.org&#x2F;news&#x2F;how-to-download-and-install-xcode&#x2F;&quot;&gt;https:&#x2F;&#x2F;www.freecodecamp.org&#x2F;news&#x2F;how-to-download-and-install-xcode&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;FlutterDev&#x2F;comments&#x2F;s8z1xm&#x2F;android_studio_vs_vs&#x2F;&quot;&gt;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;FlutterDev&#x2F;comments&#x2F;s8z1xm&#x2F;android_studio_vs_vs&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Transforming themobilecoder to rafaelds, and the other way around</title>
        <published>2023-06-08T00:00:00+00:00</published>
        <updated>2023-06-08T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/transforming-themobilecoder-to-rafaelds-and-the-other-way-around/"/>
        <id>https://rafaelds.com/posts/transforming-themobilecoder-to-rafaelds-and-the-other-way-around/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/transforming-themobilecoder-to-rafaelds-and-the-other-way-around/">&lt;p&gt;Here&#x27;s an update for those looking 👀 into my page.&lt;&#x2F;p&gt;
&lt;p&gt;I got a notification in my email that my domain rafaelds dot com is expring.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what&quot;&gt;What?&lt;&#x2F;h3&gt;
&lt;p&gt;I did not know that I &lt;em&gt;HAVE&lt;&#x2F;em&gt; this domain! It&#x27;s short and sweet.&lt;&#x2F;p&gt;
&lt;p&gt;So for now, I will make my personal one use &lt;code&gt;rafaelds.com&lt;&#x2F;code&gt;, whilst my &lt;code&gt;themobilecoder.com&lt;&#x2F;code&gt; domain will be used for my Flutter-centric blog.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The new mobilecoder website</title>
        <published>2023-03-07T00:00:00+00:00</published>
        <updated>2023-03-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/the-new-mobilecoder-website/"/>
        <id>https://rafaelds.com/posts/the-new-mobilecoder-website/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/the-new-mobilecoder-website/">&lt;p&gt;I made a decision to migrate my website &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;rafaelds.com&quot;&gt;rafaelds.com&lt;&#x2F;a&gt; from Bluehost&#x2F;Wordpress into a static site generator.&lt;&#x2F;p&gt;
&lt;p&gt;I am now using &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.11ty.dev&#x2F;&quot;&gt;eleventy&lt;&#x2F;a&gt; as my tool to build my website. Since it is a static site generator, I have to update my source code and redeploy to my server every time I want my changes to be public. This includes new posts such as this one.&lt;&#x2F;p&gt;
&lt;p&gt;This change gives me more flexibility in what I want to do with my content.&lt;&#x2F;p&gt;
&lt;p&gt;Special thanks to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.lenesaile.com&#x2F;en&#x2F;&quot;&gt;https:&#x2F;&#x2F;www.lenesaile.com&#x2F;en&#x2F;&lt;&#x2F;a&gt; for sharing a beautiful Eleventy template!&lt;&#x2F;p&gt;
&lt;p&gt;Ta!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Flutter hackathon #hack20 journey</title>
        <published>2020-07-06T00:00:00+00:00</published>
        <updated>2020-07-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/flutter-hackathon-hack20-journey/"/>
        <id>https://rafaelds.com/posts/flutter-hackathon-hack20-journey/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/flutter-hackathon-hack20-journey/">&lt;p&gt;I will share in this post how our Flutter hackathon #hack20 journey was. This is an anecdote on how the whole experience was for me and my team.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hack20-background&quot;&gt;#hack20 background&lt;&#x2F;h2&gt;
&lt;p&gt;#hack20 is the second of the annual Flutter hackathon hosted by the Flutter community that happened from 27-29th of June 2020. While it is not an official Google event, it is backed and sponsored by Google and other big companies in the field.&lt;&#x2F;p&gt;
&lt;p&gt;Basically, you think of a project that you can create using Flutter. You then form a team or work on your own for 48 hours before you submit your project. Top 20 winners get prizes, with the first place getting a $4000 worth of electronic setup.&lt;&#x2F;p&gt;
&lt;p&gt;The theme of your project can be &lt;strong&gt;Saving the Planet or Retro&#x2F;Cyberpunk&lt;&#x2F;strong&gt;. There were around ~2600 participants, 650 teams and ~250 submitted projects&lt;&#x2F;p&gt;
&lt;h2 id=&quot;did-you-win&quot;&gt;Did you win?&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;No, we did not&lt;&#x2F;strong&gt; 😂. There were two rounds of voting, and all participating teams get to vote on other teams&#x27; projects. We luckily managed to reach the final round (one of the 90 teams), but that was it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-project&quot;&gt;The project&lt;&#x2F;h2&gt;
&lt;p&gt;You can checkout our code in this &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;InkRibbonApp&#x2F;inkribbonflutter&quot;&gt;github repository&lt;&#x2F;a&gt;. You can also try it on web &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;inkribbon-cd2f7.web.app&#x2F;&quot;&gt;from here&lt;&#x2F;a&gt;, though some of the features are not really optimised for browsers. Make sure to use landscape! Also, exporting PDF on web sometimes work!&lt;&#x2F;p&gt;
&lt;p&gt;On top of that, here is the video that we almost did not submit due to iMovie crashing 30 minutes before the deadline.&lt;&#x2F;p&gt;
&lt;div class=&quot;yt&quot;&gt;&lt;iframe src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;KS8G8Mi8MFA&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;&lt;&#x2F;div&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;the-team&quot;&gt;The team&lt;&#x2F;h2&gt;
&lt;p&gt;Our team of three is called &lt;strong&gt;GMT+10&lt;&#x2F;strong&gt;. We chose that name because we all work together here in Sydney, AU. The three of us are all Android developers who believed that we should use Flutter in our company app!&lt;&#x2F;p&gt;
&lt;p&gt;We believe that we are at a slight disadvantage because of timezones. The official time started at Saturday, 10:00 PM Sydney local time and will end at Monday 10:00 pm. So following the rules, we only have around 24 hours with sleep because we have a day job on a Monday.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-1-ideation&quot;&gt;Day 1 - Ideation&lt;&#x2F;h2&gt;
&lt;p&gt;This is the fun part in my opinion. This is where we tried to think of ideas that can either be fun to use or just plain helpful to others. We used Invision to draw out and gather different ideas and suggestions, because all of us were working together remotely.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Our sketch before we started writing code&quot; title=&quot;Our sketch before we started writing code&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;invisonscreenshot.3a97a7ecf17ed4e2.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;invisonscreenshot.d3f3ea57ce0a38e7.jpg 512w, https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;invisonscreenshot.3a97a7ecf17ed4e2.jpg 1024w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;At the same time, this is the hardest part of the hackathon. There are so many considerations while we were thinking of possible projects.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Is it doable in 48 hours?&lt;&#x2F;li&gt;
&lt;li&gt;Will people use it?&lt;&#x2F;li&gt;
&lt;li&gt;Has anyone already made one?&lt;&#x2F;li&gt;
&lt;li&gt;Will someone possibly have the same project?&lt;&#x2F;li&gt;
&lt;li&gt;Will it look presentable on the demo?&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Each of these points are valid considerations. At the time, we did not know how voting will work, but I realized after that the video of the demo is the most important part of it.&lt;&#x2F;p&gt;
&lt;p&gt;On top of that, we avoided projects that other people will most likely make e.g. Recycling App, Retro Games, COVID tracing, etc.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;P.S. The winner of the hackathon was actually a retro game (Game and Watch)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;day-2-making-the-app&quot;&gt;Day 2 - Making the app&lt;&#x2F;h2&gt;
&lt;p&gt;This day is how we spent our time making the app. We were using Zoom to be constantly together. While everyone worked on every part of the code base, each one of us has a specific focus.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;app-design&quot;&gt;App design&lt;&#x2F;h3&gt;
&lt;p&gt;As a disclaimer, I am in no means a designer. None of us in the team claimed to be. Therefore, our approach is to make the best design we can do with our skills. In order to do this, we have to go for the simplest thing we could do.&lt;&#x2F;p&gt;
&lt;p&gt;If you had a look at our ideation page above, the typewriter app would look something like this.&lt;&#x2F;p&gt;




&lt;img alt=&quot;The keyboard is not even centered!&quot; title=&quot;The keyboard is not even centered!&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;raw-typewriter.b0db50fb6f8ec9cf.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;raw-typewriter.cf691623e513188e.jpg 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Because I have prior experience in using Sketch for images in my blog, I volunteered to make the designs and export them as assets.&lt;&#x2F;p&gt;
&lt;p&gt;One of the main source of assets that I used was &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;undraw.co&#x2F;&quot;&gt;unDraw&lt;&#x2F;a&gt;. They are open source and don&#x27;t even require to cite them as sources. And if you haven&#x27;t already, don&#x27;t forget to give them a shoutout through twitter. I am sure that most of you have used one or two of their images.&lt;&#x2F;p&gt;
&lt;p&gt;I get these images, and modify them as I go. Even our app logo was derived from it. This is how my Sketch board looks like during the hackathon.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Messy messy sketchboard&quot; title=&quot;Messy messy sketchboard&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;sketch-design.a4b2fadbe9637489.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;sketch-design.6325df3fa7bddb3d.jpg 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;h3 id=&quot;keyboard-design&quot;&gt;Keyboard design&lt;&#x2F;h3&gt;
&lt;p&gt;I designed the keyboard using Sketch. The idea is not an original one from me, as I have to look at how a typewriter keyboard would look in a mobile app. We used the top typewriter app in Play Store as a guide. Looking at our InVision screenshot again, here is what we used as a reference.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Existing typewriter app in Android&quot; title=&quot;Existing typewriter app in Android&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;android-typewriter.a9502851ff9068e4.jpg&quot; srcset=&quot;&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Now, the dreaded part is to design our own keyboard. I didn&#x27;t have much time and experience in doing this efficiently, so I had to quickly look for tutorials on how to do this properly.&lt;&#x2F;p&gt;
&lt;p&gt;In Sketch, there is a thing called &lt;strong&gt;Symbol&lt;&#x2F;strong&gt;, where it is like a master copy of a design. It means that if I change this master copy, all the designs that use this will also update. Here is what I had in Sketch.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Far from perfect but I&amp;#x27;m proud of it :)&quot; title=&quot;Far from perfect but I&amp;#x27;m proud of it :)&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;master-key.54db9e1254830ab3.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;master-key.57fd0a4a532ab9bf.jpg 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Using that symbol, I had to make each key that will be present in the keyboard. The letters in the keyboard has to change their cases too, so I have to account for that as well. Never forget all those numbers and symbols!&lt;&#x2F;p&gt;




&lt;img alt=&quot;I already feel tired looking at it&quot; title=&quot;I already feel tired looking at it&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;keys.4224d633c7de903a.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;keys.18ce2a467cf7c25a.jpg 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;h3 id=&quot;writing-the-code&quot;&gt;Writing the code&lt;&#x2F;h3&gt;
&lt;p&gt;For the most part, I wrote the code for the typewriter keyboard. It was far from perfect, and not really using any patterns such as BLoC.&lt;&#x2F;p&gt;
&lt;p&gt;What I did however is to make it reusable. The typewriter keyboard widget is just a normal Flutter widget that allows you to listen to keystroke events.&lt;&#x2F;p&gt;
&lt;p&gt;It means that user of this keyboard just needs to pass a TypewriterKeyboardController, listen to keys being pressed and process it elsewhere in the code. Here is an actual snippet of our main screen using the typewriter keyboard and controller.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;dart&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;@override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; initState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  super&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;initState&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  _typewriterKeyboardController &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TypewriterKeyboardController&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  _typewriterKeyboardController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;textStream&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;listen&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(_onTextReceived)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type z-annotation&quot;&gt;@override&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support z-class&quot;&gt;Widget&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; build&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;BuildContext&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; context) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; Scaffold&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    appBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; AppBar&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      leading&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; BackButton&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    bottomSheet&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt; TypewriterKeyboard&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;      typewriterKeyboardController&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; _typewriterKeyboardController&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;  )&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;void&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; _onTextReceived&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-class&quot;&gt;String&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; text) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-modifier&quot;&gt;   final&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; textValue &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; text&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;toLowerCase&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;   switch&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; (textValue) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;      &#x2F;&#x2F;Update EditText&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-comment&quot;&gt;&#x2F;&#x2F; © rafaelds&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Again, this could be further refactored, such that the logic to handle keys would be on a BLoC or a different service class. Given the time and pressure, we just wrote what would work at the time.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;code-collaboration&quot;&gt;Code collaboration&lt;&#x2F;h3&gt;
&lt;p&gt;This can a bit inconvenient sometimes. Each of us have our own style of writing code. While there is really no right or wrong with style, it is important to have a consistent way of structuring and formatting code within a codebase.&lt;&#x2F;p&gt;
&lt;p&gt;One example is line formatting. I personally prefer putting commas at the end of a Flutter tree code so it would look like a tree when auto format happens. Some of us however prefer to make it single line if it&#x27;s short enough.&lt;&#x2F;p&gt;
&lt;p&gt;Due to these inconsistencies, a small commit &lt;strong&gt;might end up having a large changeset due to spaces and formatting&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If we write code in Android, we will definitely have consistent style of writing code as we have been pair programming at work, and everyone just syncs with each other.&lt;&#x2F;p&gt;
&lt;p&gt;The good thing with what we did is that we barely stepped on each others&#x27; toes. We split our work by features, such that one is working with Firebase authentication and the other is on exporting and viewing PDFs.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;day-3-ish-submitting-the-project&quot;&gt;Day 3-ish - Submitting the project&lt;&#x2F;h2&gt;
&lt;p&gt;The team was able to barely finish the app on Sunday evening. The problem is that the deadline will be at 10:00 pm Monday. We have our daily job on a weekday, so we only have the Monday afternoon to finish the video.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;making-the-video&quot;&gt;Making the video&lt;&#x2F;h3&gt;
&lt;p&gt;We had an idea of just showing in the video how the app works, but it is &lt;strong&gt;so damn boring to present a note taking app with a typewriter design.&lt;&#x2F;strong&gt; I mean yeah, there&#x27;s Firebase integration so you can save your notes online, but every other note app does the same thing!&lt;&#x2F;p&gt;
&lt;p&gt;We already know that we are not going to win &lt;em&gt;(not with that attitude)&lt;&#x2F;em&gt;. Our last saving grace is to just make a good video, and that&#x27;s what we tried to achieve.&lt;&#x2F;p&gt;
&lt;p&gt;Our solution is to select a really dramatic music that still sits well with the app, with a great message written in the note. Since we already knew we will never going to win, we decided to just write a message that wishes everyone good luck.&lt;&#x2F;p&gt;
&lt;p&gt;We were sure that other teams will be writing apps that will help save the planet, so that&#x27;s where we focused on. We spent a fair amount of time looking for a good music and thinking about a message.&lt;&#x2F;p&gt;
&lt;p&gt;If you have not watched the video above, here is the message in our video.&lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;video src=&quot;typewriter-message.mp4&quot; controls preload=&quot;metadata&quot; style=&quot;max-width: 100%&quot;&gt;&lt;&#x2F;video&gt;
  &lt;figcaption&gt;Our message to the hackathon participants&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;exporting-the-video&quot;&gt;Exporting the video&lt;&#x2F;h3&gt;
&lt;p&gt;Exporting the whole video gave us all a mild heart attack. It was &lt;strong&gt;30 minutes&lt;&#x2F;strong&gt; before the deadline, and we just started exporting this movie project to a video file.&lt;&#x2F;p&gt;
&lt;p&gt;As someone who has not exported a 1-minute long video on a 6 year old laptop, this was a new experience for me. Our saving grace to our simple app is going down the drain as we saw this image. Oh, I could have benefited from the new Macbook prize 😂&lt;&#x2F;p&gt;
&lt;p&gt;We are in a dilemma because if I just force shutdown iMovie, it might take another 30 minutes to export. We are also not quite sure if we have saved the movie project, because &lt;strong&gt;iMovie does not have a manual Save function!!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, the project was auto saved. One of my teammate was using a newer Macbook, so we thought of just copying the iMovie project (which was hard to locate btw), send it to him with our amazing Australian internet speed, and let him do the file export.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-miracle&quot;&gt;The miracle&lt;&#x2F;h3&gt;
&lt;p&gt;While we are waiting for a miracle, my other team mate started to export the video in his laptop. I was browsing the Slack channel for this Flutter hackathon and saw a message that kept our hopes up.&lt;&#x2F;p&gt;




&lt;img alt=&quot;Just in time!&quot; title=&quot;Just in time!&quot; src=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;deadline-extension.d442c8417d2f9aad.jpg&quot; srcset=&quot;https:&#x2F;&#x2F;rafaelds.com&#x2F;processed_images&#x2F;deadline-extension.7f1011631a3fd814.jpg 512w&quot; class=&quot;&quot; &#x2F;&gt;

&lt;p&gt;Now that we have an additional hour to submit our video, we knew that we will be able to sleep well that night.&lt;&#x2F;p&gt;
&lt;p&gt;Even before my teammates video export, iMovie successfully exported the video. However, iMovie didn&#x27;t recover so I just shut it down. If you have a dinosaur laptop like I do, stay away from iMovie.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;takeaway&quot;&gt;Takeaway&lt;&#x2F;h2&gt;
&lt;p&gt;It was a fun hackathon experience. I was coughing the whole time during the hackathon, so it was fun but not entirely a pleasant experience.&lt;&#x2F;p&gt;
&lt;p&gt;My teammates and I just proved that Flutter is a great tool to develop apps reliably on most platforms, including web. It will be a long stretch to convince our teammates in the office how much Flutter can do, but it does not matter.&lt;&#x2F;p&gt;
&lt;p&gt;I honestly learned a lot more in doing designs in Sketch. This is a skill that I really want to improve.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, I am really impressed with the app ideas of the other participants, especially the ones with the &lt;strong&gt;Save the Environment&lt;&#x2F;strong&gt; theme.&lt;&#x2F;p&gt;
&lt;p&gt;Congratulations to everyone that won, and see you next hackathon!&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;links&quot;&gt;Links&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=KS8G8Mi8MFA&quot;&gt;Promotional video&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;InkRibbonApp&#x2F;inkribbonflutter&quot;&gt;Github project repo&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;inkribbon-cd2f7.web.app&quot;&gt;Demo web app&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>I started building a radio app</title>
        <published>2020-04-04T00:00:00+00:00</published>
        <updated>2020-04-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://rafaelds.com/posts/i-started-building-a-radio-app/"/>
        <id>https://rafaelds.com/posts/i-started-building-a-radio-app/</id>
        
        <content type="html" xml:base="https://rafaelds.com/posts/i-started-building-a-radio-app/">&lt;p&gt;I started writing a radio app following the tutorial on making a radio app in Flutter that I recently published. I was inspired to make one as I wanted to publish a radio app for iOS and Android devices.&lt;&#x2F;p&gt;
&lt;p&gt;Although still in its early stages, I am planning to release this soon to both App Store and Google Play stores.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;current-features&quot;&gt;Current features&lt;&#x2F;h3&gt;
&lt;p&gt;At the time of writing (4th April, 2020), the radio app is at least able to play an online radio station. You can also choose which country to get stations from and bookmark them.&lt;&#x2F;p&gt;
&lt;p&gt;I have used the most basic and vanilla design I can think just so I can push more features early on. No fancy animations yet!&lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;video src=&quot;playthrough.mov&quot; controls preload=&quot;metadata&quot; style=&quot;max-width: 240px&quot;&gt;&lt;&#x2F;video&gt;
  &lt;figcaption&gt;Radio playthrough&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Here are some of the features that are already in the app worth mentioning:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Works on both iOS and Android&lt;&#x2F;li&gt;
&lt;li&gt;Can play radio station on the background (even when the phone is locked)&lt;&#x2F;li&gt;
&lt;li&gt;Can play &#x2F; pause radio from the lock screen&lt;&#x2F;li&gt;
&lt;li&gt;Bookmark a radio station&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;challenges&quot;&gt;Challenges&lt;&#x2F;h3&gt;
&lt;p&gt;I found out that the simple things are the ones difficult to implement. It took me a while to fix simple issues just to make the experience of using the app better, even if they seem very easy to do.&lt;&#x2F;p&gt;
&lt;figure&gt;
  &lt;video src=&quot;add-favourite.mov&quot; controls preload=&quot;metadata&quot; style=&quot;max-width: 240px&quot;&gt;&lt;&#x2F;video&gt;
  &lt;figcaption&gt;Features&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Things like consistency between parts of the screens in the app for example. Pressing the heart button in the media player should update the favourites screen. At the same time, if I remove the station in the favourites screen, the heart button in the media player should be updated too.&lt;&#x2F;p&gt;
&lt;p&gt;Another example is showing screens in between stages of the app. The app should show important information such as telling the user that there are no stations saved (instead of just showing a blank canvas). Or letting the user know that the app is still getting the list of radio stations by showing a loading spinner.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;features-to-be-added-soon&quot;&gt;Features to be added soon&lt;&#x2F;h3&gt;
&lt;p&gt;The app still needs a lot of polishing and updates before I publish it. These are some of the features I might add before I release the app to the public&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Make the UI more beautiful&lt;&#x2F;li&gt;
&lt;li&gt;Add simple animations&lt;&#x2F;li&gt;
&lt;li&gt;Add a screen to show the radio information&lt;&#x2F;li&gt;
&lt;li&gt;Get stations by genre (news, rock, country, etc.)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h3&gt;
&lt;p&gt;That is it for the update! Writing this app has been both a joy and a pain in the butt, but (double pun) I will still continue and finish this up for everyone to use.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
