<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shakleen Ishfar on Medium]]></title>
        <description><![CDATA[Stories by Shakleen Ishfar on Medium]]></description>
        <link>https://medium.com/@shakleenishfar?source=rss-8757dc4503d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VycCcWN1FqfDGe1X5AoHyQ.jpeg</url>
            <title>Stories by Shakleen Ishfar on Medium</title>
            <link>https://medium.com/@shakleenishfar?source=rss-8757dc4503d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:01:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shakleenishfar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Document Detection in Python]]></title>
            <link>https://medium.com/intelligentmachines/document-detection-in-python-2f9ffd26bf65?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f9ffd26bf65</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 04:24:07 GMT</pubDate>
            <atom:updated>2020-06-06T04:11:28.288Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*q368PkwWdT9CblI3" /><figcaption>Photo by <a href="https://unsplash.com/@imattsmart?utm_source=medium&amp;utm_medium=referral">iMattSmart</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello, good people! I hope everything is going well. It’s been quite a while since I wrote an article on Medium. So, I decided to take up the pen… errm… keyboard, and write again.</p><p>Recently, I had a chance to work on document detection. Learnt some pretty amazing things while doing so. I hope to share what I learnt in this article, <strong>which is mainly about document detection using image processing. </strong>That being said, I’m not going to give a thorough walk-through on building the absolute best document detector ever.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FyGesXBuMnMSdi%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FyGesXBuMnMSdi%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FyGesXBuMnMSdi%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="322" frameborder="0" scrolling="no"><a href="https://medium.com/media/0227e1bebdaf75bb30ba867671150a4c/href">https://medium.com/media/0227e1bebdaf75bb30ba867671150a4c/href</a></iframe><p>I’ll discuss the general process of building it and mention what needs to change depending on the situation.</p><h3>Before proceeding further</h3><h4>Required knowledge</h4><p>You just need to know Python 3. I’ll provide links to helpful articles for literally everything else.</p><h4>Where to find the code</h4><p><a href="https://github.com/Shakleen/Python-Document-Detector">The code from this article can be found in this GitHub repository.</a></p><h4>Environment Details</h4><p>I’ll be using Python3 (3.7.7) and relying heavily on the additional python packages (that aren’t be included by default). <a href="https://github.com/Shakleen/Python-Document-Detector/blob/master/requirements.txt">Check the requirements.txt file and install them using pip.</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F12WPxqBJAwOuIM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F12WPxqBJAwOuIM%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F12WPxqBJAwOuIM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a1675db230e638ba64312b3a06eb65a/href">https://medium.com/media/0a1675db230e638ba64312b3a06eb65a/href</a></iframe><h3>Document Detection</h3><p>Document detection is basically extracting a document from an image. Machine learning and image processing are two common approaches to do this. This article discusses the latter approach. Here, we use pixel manipulation techniques to detect the document.</p><h4>Steps Involved</h4><ol><li>Pre-processing image to enhance document.</li><li>Finding document extraction points in the image. Quadrilaterals are the extraction points in this article.</li><li>Extracting the document from the points.</li><li>Post processing to correct distortions and imperfections.</li></ol><p>Let’s code this up in Python. We’ll fill in the details as we go.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b7a630d3c42433deb000bcf545668e9/href">https://medium.com/media/2b7a630d3c42433deb000bcf545668e9/href</a></iframe><h4>PageExtractor Class</h4><ol><li>It’s only for performing page extraction, which it does in its<strong> _extract_page</strong> method. It is called in line no 22. (You can find how this method works in step 3, towards the end of the article.)</li><li>Corner detection, pre and post processing code isn’t going to be a part of this class. Separate class objects will perform these actions. They’ll be passed as parameters. (We will create the required classes soon.) <br><strong>Why are these parameters?</strong> <br>Because there are many different ways to perform these tasks. This way, I can swap them out and leave the core logic unchanged. (Core logic being the page extraction function.)<br><strong>What’s <em>output_process</em> parameter?</strong><br>I use this parameter for debugging. When set to true, intermediate images are saved to output folder. I use these images to check if the filters are working properly.</li><li>Notice lines 13 to 16. Pre-processing is done by applying a chain of processors. The output of one processor is fed into the next processor. This is repeated until there are no more processors left to call. <strong>All processors thus needs to follow a common structure. It must take only an input image and produce only an output image.</strong></li><li>You might have noticed that there is no post-processing here. I omitted it for the sake of keeping the article short. But they should follow the same structure as the pre-processors.</li></ol><h3>Step 1: Pre-processing</h3><blockquote>The end goal is to get a clear and usable document. Pre-processing is done to help achieve that goal.</blockquote><p><strong>Common processing steps</strong><br>1) Threshold or binarize to make images only black and white.<br>2) De-noising to reduce noise.<br>3) Sharpening to make blurry images workable.<br>4) <a href="https://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html">Morphologial transformations</a> to fill in missing details or erase unnecessary details.<br>5) Contrast stretching to increase the contrast of an image.<br>6) Inversion to invert light and dark places of an image.<br>etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGUpqv67Lj1N0mGdQx2xSQ.jpeg" /><figcaption>Figure #1: Example image of a document</figcaption></figure><p>Some things to note before we begin this section:</p><ol><li><a href="https://github.com/Shakleen/Python-Document-Detector/blob/master/processors.py">All the processors are kept in the same file <strong>processors.py, </strong>which can be found here<strong>.</strong></a></li><li>All processors have a common structure, they take an input image and produce an output image. Necessary settings are passed during class object initialization.</li><li>I’m keeping things simple and using only 3 pre-processing functions. Depending on your use case, you might need to come up with a list of your own. <strong>Processor ordering matter because they are applied in a chain fashion.</strong></li><li>I’ll be using the image seen in <em>Figure #1</em> as an example. It’ll be helpful for visualization.</li></ol><h4>1.1. Re-scaling filter</h4><blockquote>Processing high-resolution images are quite resource intensive.</blockquote><p>When an image is above a certain size, I like to scale them down for faster processing. My implementation can be seen in <em>Code Snippet #2</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99b4850257567fcceae931236c96fabe/href">https://medium.com/media/99b4850257567fcceae931236c96fabe/href</a></iframe><blockquote><strong>A bit of caution is advised.</strong><br>1) Scaling an image down too much will cause it lose details and become pixelated.<br>2) Distortions can arise from scaling images. We want to keep height-width ratio same.</blockquote><h4>1.2. De-noising filter</h4><blockquote>The amount of noise in an image depends on the hardware and the condition it was captured in. Images captured in poor lighting tend to be very noisy.</blockquote><p>There are multiple methods for de-noising. And their use case is dependent on the amount of noise present. <a href="https://docs.opencv.org/3.4/d5/d69/tutorial_py_non_local_means.html">Here are some functions offered by OpenCV.</a></p><blockquote>Be careful when applying de-noising. <strong>Because, the more strongly an image is de-noised, the more details is lost.</strong> Hence, it can cause data loss in images.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f6124bd7ff4f19895ed1d5267817efd/href">https://medium.com/media/5f6124bd7ff4f19895ed1d5267817efd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*clPqJQfggm7pZqmDoICS6Q.jpeg" /><figcaption>Figure #2: Denoised image</figcaption></figure><p>I used <strong>fastNlMeansDenoising</strong>, as seen in line 9 of <em>Code Snippet #3</em>. The <strong>strength</strong> parameter in <strong>FastDenoiser</strong>, controls the amount of de-noising.</p><p><em>Figure #2</em> shows the output of the de-noiser. Pay particular attention to the surface the page lies on. In the original image, it had a grainy texture. But here, it’s smooth. That’s the smoothing effect of de-noising.</p><p>Another thing to notice is the speed of operation. Typically, median filters are really good for de-noising, but are very slow to run. Blurring filters are faster to run but they tend to smooth out important details as well.</p><h4>1.3. Thresholding</h4><blockquote>In <a href="https://en.wikipedia.org/wiki/Digital_image_processing">digital image processing</a>, <strong>thresholding</strong> is the simplest method of <a href="https://en.wikipedia.org/wiki/Image_segmentation">segmenting images</a>. From a <a href="https://en.wikipedia.org/wiki/Grayscale">grayscale</a> image, thresholding can be used to create <a href="https://en.wikipedia.org/wiki/Binary_image">binary images</a>. — Wikipedia</blockquote><p>There are multiple ways to perform thresholding. Depending on the use case, one method may perform better than the other. <a href="https://docs.opencv.org/master/d7/d4d/tutorial_py_thresholding.html">Here are some thresholding functions offered by OpenCV.</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3934b522a06eda9b001b238c0fe7b79d/href">https://medium.com/media/3934b522a06eda9b001b238c0fe7b79d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*J52fNrvmM-geuTyQda7w6Q.jpeg" /><figcaption>Figure #3: Thresholded image</figcaption></figure><p>I decided to go with Otsu’s method for my needs (<em>Code Snippet #4</em>).</p><p>The result of thresholding is illustrated in <em>figure #3</em>. Compare this with <em>figure #2</em>. The page doesn’t have wrinkles. It is pure white and the writings are more easily visible. The background is completely black.</p><p>However, thresholding might not always be desirable. When color is an important factor for example.</p><h3>Step 2: Getting the extraction points</h3><blockquote>Extraction points are points on the perimeter of the document which will be used to extract it.</blockquote><p>The simplest approach is to use quadrilaterals or the four corners of the document. There are two popular ways to do find quadrilaterals:<br>1. Hough lines<br>2. Contours</p><p>My implementation is based on the hough lines approach. <a href="https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/">If you’re interested in a contour based implementation, here’s a great article by Adrian in PyImageSearch.</a> Which ever way you decide to perform this, simply create a class that takes the processed image and spits out the quadrilaterals. Then pass the object of this class to <strong>PageExtractor</strong>.</p><p><strong>My Corner Detection Process</strong></p><ol><li><strong>Processing step: </strong>Process the input image so that hough transform goes smoothly.</li><li><strong>Getting the lines: </strong>Run hough transform to get hough lines.</li><li><strong>Getting the intersections: </strong>Calculate the intersections between hough lines.</li><li><strong>Finding quadrilaterals: </strong>Group the intersections into 4 clusters and take the mean of each cluster. These mean points are our quadrilaterals or corners.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2455dbd1539df202f4923b7e990b766b/href">https://medium.com/media/2455dbd1539df202f4923b7e990b766b/href</a></iframe><h4>2.1. Processing for corner detection</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FYV3befxMEEqLd0tsaF%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYV3befxMEEqLd0tsaF%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FYV3befxMEEqLd0tsaF%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/d081e0f6e05490c781ec359fa26ba12e/href">https://medium.com/media/d081e0f6e05490c781ec359fa26ba12e/href</a></iframe><p><strong>Hold on a minute, mister! Why are we processing again? Didn’t we just do this?</strong><br>Well, yes. We did. But there is a reason to this madness. Corner detection will work best if we can omit unnecessary details in the image. For example the writings on the page are not necessary to detect it’s boundary. To prevent them from affecting out detection process it is desirable to erase them. <br>But we want the writings to exist in the final detected document. Which is why, we don’t want to include these processing in the previous pre-processing step. This is just for corner detection!</p><h4>2.1.1. Morph functions for eliminating unnecessary details</h4><p>Now, that we’re in the same page, how do we erase unnecessary details? It should be noted that the image being fed into the corner detector is assumed to be a black and white image. (If it isn’t it needs to be binarized before anything) We can use morphological transformations for erasing unnecessary details. <a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html">Here’s a great article to get you up to speed on morph functions offered by OpenCV.</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7b04a8685975ac2c3ccdd32f79ac853/href">https://medium.com/media/b7b04a8685975ac2c3ccdd32f79ac853/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*RqcSH6EHxr-KVwzsUv5CpA.jpeg" /><figcaption>Figure #4: Output of closing. Writings eliminated. Only page visible.</figcaption></figure><p>I created <strong>Closer </strong>class (<em>Code Snippet #6</em>) to perform morphological close function. You can control the effect of the morphological function by tuning the parameters <strong>kernel_size</strong> and <strong>iterations</strong>.</p><p>The output should look something like <em>Figure #4</em>. Notice how only the page is left and the writings are gone. Perfect!</p><p>Depending on the use case, you might need other morph functions. For example, there might be dark regions in the page or white noise in the back ground. These can be fixed using morph functions!</p><blockquote>Be cautious when playing with the parameters. You might distort the actual shape of the page if you aren’t careful.</blockquote><h4>2.1.2. Canny Edge Detection</h4><p>Canny is an edge detection algorithm.<a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html"> A quick guide of canny and how it works can be found here.</a></p><p><strong>So, what purpose does this serve?</strong><br>Mainly, to detect the page perimeter. To perform document detection, we need only the perimeter and nothing else. The inner white portion is unnecessary. Canny gives us exactly that. It gives us the perimeter outlines or the edges. Moreover, without this, hough trabsform will not behave the way we want it to!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e676f00631f29a97d3f2a77d61efa502/href">https://medium.com/media/e676f00631f29a97d3f2a77d61efa502/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*cSDh-nklocHdB2DBPZhGNw.jpeg" /><figcaption>Figure #5: EdgeDetector output</figcaption></figure><p>I created a processor for edge detection using canny called <strong>EdgeDetector </strong>(<em>Code Snippet #7</em>).</p><p>The output looks like <em>figure #5</em>. Usually, this will be the image that we feed into hough transform function. But it’s possible for this image to have way too many unnecessary edge information (Yes, even after all that processing). Hence, an edge cleaning filter might be required to clean the output before proceeding with hough transform.</p><h4>2.2. Hough lines</h4><p>If you’re not sure what hough transform is, <a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html">here’s an excellent explanation</a> to get you up-to speed. OpenCV has <strong>HoughLines</strong> and <strong>HoughLinesP</strong> functions for this purpose. I’ll be using the former. It has 3 parameters which are of concern:<br>1. Rho Accuracy<br>2. Theta Accuracy<br>3. Line threshold</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/629fb7989a49697e2bf9681f9777a565/href">https://medium.com/media/629fb7989a49697e2bf9681f9777a565/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*oany2VdxWBGZqlrfsygiOQ.jpeg" /><figcaption>Figure #6: Hough lines from processed image</figcaption></figure><p><strong>_get_hough_lines</strong> is our function of interest (illustrated in <em>Code Snippet #8</em>). The lines variable holds our desired hough lines (Line 5). Here, lines is an array of rho and theta value pairs. These two values describe each of the hough lines.</p><p>I got the result shown in <em>figure #6</em>. The hough lines are colored in red. You can clearly see that it properly encompasses the page in the processed image. There might be stray hough lines (although there aren’t any here). These are lines that don’t align well with the page border. As long there aren’t too many of them, they are of no concern.</p><h4>2.3. Getting intersections</h4><p><strong>So, we need to calculate the intersections between the hough lines and we’re all good right? <br></strong>Not so fast! As you can see from <em>figure #6</em>, there are multiple lines on each side. Lines on the same side are almost parallel to each other. If we calculate intersections among all lines, then we’d be left with <br>1) way too many intersections<br>2) and some won’t be even close to the quadrilateral points.</p><p>So, we need a way to check if the intersections are proper. We’re going to do the following for each and every pair of hough lines:</p><ol><li>We will calculate the intersection angle between them. That this angle will be close to 90 degrees. (Because the angle created by each side of the page at the quadrilaterals should be 90 degree). Let’s assume the angle will be between 80 and 100 degrees.</li><li>If the angle is within bounds for two lines, we will calculate the intersection them.</li><li>Finally, we check if the intersection is within the boundary of the image and if so, add it our list of intersections calculated so far (which will empty to start with).</li></ol><p>You might employ more tactics to carefully choose the intersections. The better you perform here, the better the detection will be.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/769a41f093527ecd41606fa20abb365f/href">https://medium.com/media/769a41f093527ecd41606fa20abb365f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*zIWqVHB1s88V3uAqX_0KRw.jpeg" /><figcaption>Figure #7: Intersections between hough lines</figcaption></figure><p><em>Code Snipper #9 </em>produces the image in <em>Figure #7</em>. The blue dots are the intersections we get. Also notice that the parallel lines didn’t produce any intersection points.</p><p>Here’s a quick run down of what happens where.</p><ol><li><strong>_get_intersections</strong> at line 3 is going to produce the intersections we want. It first checks the angle and then computes intersection point. Finally returning a list of all intersections.</li><li>Angles are being calculated by <strong>_get_angle_between_lines </strong>at lines 25 to 33.</li><li>Calculated angles are being checked at line 15.</li><li>If the angle is acceptable, <strong>_intersection</strong> calculates the intersection (lines 36–53).</li><li>In line 18, I check to make sure that the intersection point is within the boundary of the image and if so it’s added to the list of intersections in line 19.</li></ol><h4>2.4. Getting the quadrilaterals</h4><p>All that’s left is to cluster the intersections into 4 groups and get the mean point from each group. I did this using KMeans algorithm, <a href="https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html">which you can learn more about here</a>!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb5746c47a32996a11a484c7eba12430/href">https://medium.com/media/cb5746c47a32996a11a484c7eba12430/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*PVhcAl2dhq13HyHTcDnwDg.jpeg" /><figcaption>Figure #8: Quadrilaterals</figcaption></figure><p>The clustering step is shown in <em>Code Snippet #10</em>. This piece of code produces the image seen in <em>Figure #8</em>. The white points are our quadrilaterals.</p><p>A question might pop into your mind right about now. What if there were hough lines all over the place? What if they weren’t as properly positioned as these? Wouldn’t that mess up the intersections in in turn mess up the quadrilaterals? Well yes. They would. But remember that we’re discarding intersections whose angles aren’t close to 90 degrees. And then we’re taking the mean of the intersections of a group. The results won’t be perfect sure. But it’ll be reasonably close.</p><h3>Step 3: Page Extraction</h3><p>Now that we have the quadrilaterals, we just need to extract the page. If you remember this was the purpose of <strong>PageExtractor</strong> class. We can use OpenCV’s <strong>warpPerspective</strong> method to perform this extraction. <a href="https://www.pyimagesearch.com/2014/05/05/building-pokedex-python-opencv-perspective-warping-step-5-6/">Here’s an excellent article that discusses this function in depth.</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07e77729cda9b7b28a57ca772724f000/href">https://medium.com/media/07e77729cda9b7b28a57ca772724f000/href</a></iframe><h4>Running the code</h4><p>We have all that we need. Now we just need to create an instance of PageExtractor and call it with an image path. <em>Code snippet #12 </em>shows how to do this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b8a101fd512f1c001921acffd6a35b6/href">https://medium.com/media/8b8a101fd512f1c001921acffd6a35b6/href</a></iframe><p>And finally, here’s what this code produces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*AnZo8_KKN8s_jHLxH5M9kQ.jpeg" /><figcaption>Figure #9: Detected document</figcaption></figure><h3>Step #4: Post Processing</h3><p>There aren’t too many distortions in the extracted image. However, it might benefit from a little bit of post processing. Here’s what could be done:</p><ol><li>Notice that the image is slightly tilted to one side. The writings aren’t completely horizontal. So, a rotation correction filter would be useful here.</li><li>Some of the characters appear to have lost some details. Morphological transformations can help to reduce this.</li></ol><p>But because these aren’t big issues and the article is already humongous enough, I’ll leave it to you, the reader, to explore this more on your own.</p><h3>Pitfalls and Challenges</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1k5k3J5K3BywQOrpNA%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F1k5k3J5K3BywQOrpNA%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F1k5k3J5K3BywQOrpNA%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f6e6b08348457cbaacee5594929ce4a/href">https://medium.com/media/3f6e6b08348457cbaacee5594929ce4a/href</a></iframe><ol><li><strong>Quadrilaterals aren’t enough.</strong><br>Quadrilaterals work well if the page is lying flat on a surface. But what if it’s in a bended state? We’d need more points to extract this. Some popular document scanners, like <strong>CamScanner</strong>, uses 8 points for page extraction.</li><li><strong>Parameter tweaking can and will raise hell.</strong><br>There are so many parameters in this simple detector! Changing their values can result in poorer or better performance. So, what’s the best combination of values? The only way to find out is to tweak them. This is why Machine Learning detectors perform better. Because this process is automated by the computer. Meaning, the computer tweaks and sees what works best.</li><li><strong>A universally well performing document detector is hard to build using just image processing.</strong><br>Our document was easily distinguishable from the surface it was lying on. But what if it wasn’t? What if the image was taken in very poor lighting? What if the image was blurry? What if there were multiple pages in the image? I could go on, but you get the point.</li><li><strong>Distortions, data loss and abnormalities can be minimized but never eliminated.</strong><br>Distortions will be ever present. The best we can do is minimize them to a level that is good enough for us. Going down the rabbit hole of perfectionism will cost us way too much time and effort.</li></ol><h3>Summary</h3><p>That was a long article. Don’t worry if the underlying details have escaped you. Here’s a quick run down of the entire thing:</p><ol><li>Pre-process image to enhance the document. Use processors like sharpening, blurring, thresholding, resizing etc.</li><li>Get the quadrilaterals. This can be done by contours or hough lines.</li><li>Extract document using the quadrilaterals.</li><li>Correct unwanted distortions due to the extraction process using post-processing filters.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FdsKnRuALlWsZG%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FdsKnRuALlWsZG%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FdsKnRuALlWsZG%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/b262e606843d1af9503450acc0765723/href">https://medium.com/media/b262e606843d1af9503450acc0765723/href</a></iframe><h3>Further reading</h3><ol><li><a href="https://dropbox.tech/machine-learning/fast-and-accurate-document-detection-for-scanning">Fast and Accurate Document Detection for Scanning by DropBox development team</a></li><li><a href="https://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/">How to Build a Kick-Ass Mobile Document Scanner in Just 5 Minutes by Adrian from PyImageSearch</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f9ffd26bf65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/intelligentmachines/document-detection-in-python-2f9ffd26bf65">Document Detection in Python</a> was originally published in <a href="https://medium.com/intelligentmachines">intelligentmachines</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 8: Post Page and Comments]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/78eefb8ff447</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Sat, 30 Nov 2019 05:16:02 GMT</pubDate>
            <atom:updated>2019-11-30T05:33:28.532Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 8: Post Page and Comments</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NBlbu48PUIt5Wigx" /><figcaption>Photo by <a href="https://unsplash.com/@skabrera?utm_source=medium&amp;utm_medium=referral">Sergi Kabrera</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello, good people! Hope you’re having an amazing day. Last time, I talker about refactoring to make developing the app easier and testing simpler.</p><h3>Today’s topics include</h3><ol><li><strong>Creating the post page</strong> where all details of a page will be displayed.</li><li>Creating a static comment section suing “<strong>ListView</strong>” and “<strong>ExpansionTile</strong>”</li></ol><p>So, let’s get coding.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FrVbAzUUSUC6dO%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FrVbAzUUSUC6dO%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FrVbAzUUSUC6dO%2Fgiphy.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebe6e51e7181a37a27bed427a5b43b79/href">https://medium.com/media/ebe6e51e7181a37a27bed427a5b43b79/href</a></iframe><h3>Post Page Design Overview</h3><h4>Accessing post page</h4><p>When a user taps on a “<strong>PostCard</strong>” widget, the “<strong>PostPage</strong>” widget will be shown. We’ve already done the navigation part in a previous post.</p><h4>“PostPage” widget contents</h4><p>The information related to the “<strong>PostCard</strong>” widget will be displayed in full details in the “<strong>PostPage</strong>” widget. There are namely 5 types of contents that are going to be displayed. There are:</p><ol><li>Image header portion.</li><li>Textual information like post title, summary, and body.</li><li>Post statistics like reacts and views.</li><li>Post author details and follow option.</li><li>Post comments section and commenting option.</li></ol><h4>“PostPage” widget tree</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3mjfyQHBy1SG_DOZV1bTRw.png" /><figcaption>PostPage widget tree diagram</figcaption></figure><h3>Creating “PostPage” widget</h3><h4>Code Snippet</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f4e5d79788954d26d799085e8127654/href">https://medium.com/media/4f4e5d79788954d26d799085e8127654/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FA8NNZlVuA1LoY%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FA8NNZlVuA1LoY%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FA8NNZlVuA1LoY%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="250" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b384e93c053ce49f7614910846e569e/href">https://medium.com/media/4b384e93c053ce49f7614910846e569e/href</a></iframe><h4>Code Clarification</h4><ol><li>Notice lines 1 through 6. The class “<strong>PostPageKeys</strong>” is used mainly for one thing. To assign keys to the different widgets of interest. Keys are like IDs for widget. They are used to identify them from all other widgets. <br><strong>So, basically when I want to find a specific widget, I can use the assigned key to find it.</strong></li><li>Next line 17. If you’ve read the post about inherited widget you’d know why this exists. Simply to pass the post data to child widgets. <a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e">If you’re not sure by what I mean, read about it here.</a></li><li>“<strong>_NonImageContents</strong>” widget has a mixture of public widgets (i.e. widgets saved in other files) and private widgets (i.e. widgets saved in the same file). Among these, “<strong>PostTimeStamp</strong>” and “<strong>PostStats</strong>” were created previously. I will create “<strong>UserDetailsWithFollow</strong>” and “<strong>CommentsList</strong>” widgets now.</li></ol><p>The rest of code should be self-explanatory.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FLPIUZuASSJrreuVX31%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FLPIUZuASSJrreuVX31%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FLPIUZuASSJrreuVX31%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="239" frameborder="0" scrolling="no"><a href="https://medium.com/media/17a5d4b8c8bd8846cfd720e3dcfc4384/href">https://medium.com/media/17a5d4b8c8bd8846cfd720e3dcfc4384/href</a></iframe><h3>Modifying “UserDetails” widget</h3><h4>Code snippet</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a91f189b4fcfba58012f0d6b24b333b9/href">https://medium.com/media/a91f189b4fcfba58012f0d6b24b333b9/href</a></iframe><h4>Code clarification</h4><p>Notice that, “<strong>UserDetails</strong>” now takes a “<strong>UserModel</strong>” object as a parameter to be constructed. Previously it used to go up the widget tree and use the user information found in “<strong>PostModel</strong>”. A post has two types of user related information.</p><ol><li>The user details of the <strong>author</strong> of post.</li><li>The user details of the <strong>commenter</strong> of a post.</li></ol><p><strong>“UserDetails”</strong> widget should be able to show any user information. That’s why, I’ll pass in a “<strong>UserModel</strong>” object and this widget will show the information related to that user. Because I’ve added a parameter to the constructor of the widget, I need to pass the required parameter from “<strong>PostCard</strong>” widget. That can easily be done. So, I’m not giving a code snippet here. Refer to code changes at the end of the post to see how I’ve done it.</p><h3>Creating “UserDetailsWithFollow” widget</h3><h4>Widgets necessary</h4><ol><li>“<strong>UserDetails</strong>” widget to show user name, email and picture.</li><li><a href="https://api.flutter.dev/flutter/material/IconButton-class.html">“<strong>IconButton</strong></a>” widget for implementing follow mechanism</li><li><a href="https://api.flutter.dev/flutter/widgets/Row-class.html">Row</a> | <a href="https://api.flutter.dev/flutter/widgets/Expanded-class.html">Expanded</a> | <a href="https://api.flutter.dev/flutter/widgets/Container-class.html">Container</a> for spacing and styling.</li></ol><h4>Code snippet</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e357275137209e8f78fb951300afb38d/href">https://medium.com/media/e357275137209e8f78fb951300afb38d/href</a></iframe><h4>Code clarification</h4><ol><li>Notice lines 20 to 24. I’ve used “<strong>UserDetails</strong>” to show the user information and used some widgets around it for styling and spacing. Here, “<strong>UserDetails</strong>” is a public widget.</li><li>Line 31 shows that I’ve used a button. This button will be used as a mechanism to follow this user. By this user, I mean the user indicated by the object “<strong>userData</strong>” in line 7. I haven’t implemented the follow function yet. I’ll do so in a future post.</li></ol><h3>Creating “CommentsList” widget</h3><h4>Basic Design</h4><p>I want the comment section to be hidden away. A user can view comments by tapping to expand a widget. Meaning, the comment section should be collapsible. It will toggle between expanded and collapsed mode when being tapped.</p><h4>Comment Model</h4><p>A comment will have 3 data values which are commenting user details, time of comment posting and the actual text of the comment. I’ve created a “<strong>CommentModel”</strong> class to create this model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6229f5357ce378484c7459f1b171d487/href">https://medium.com/media/6229f5357ce378484c7459f1b171d487/href</a></iframe><p>Because comments are part of a post, “<strong>PostModel</strong>” needs to have a list of comment data. So I’ve modified “<strong>PostModel</strong>” to have a list of “<strong>CommentModel</strong>” objects. Refer to the code changes to see what I’ve done.</p><h4>Widgets necessary</h4><ol><li><a href="https://api.flutter.dev/flutter/material/ExpansionTile-class.html">ExpansionTile</a> for implementing the collapsible mechanism.</li><li><a href="https://api.flutter.dev/flutter/widgets/Padding-class.html">Padding</a> | <a href="https://api.flutter.dev/flutter/widgets/Container-class.html">Container</a> | <a href="https://api.flutter.dev/flutter/widgets/Column-class.html">Column</a> | <a href="https://api.flutter.dev/flutter/material/Divider-class.html">Divider</a> widgets for spacing and styling.</li></ol><h4>Code snippet</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08f7459478bcaa9bc2688dc5b73088d6/href">https://medium.com/media/08f7459478bcaa9bc2688dc5b73088d6/href</a></iframe><h4>Code clarification</h4><ol><li><strong>Notice lines 18 to 29.</strong> I’ve used the “<strong>ExpansionTile”</strong> widget to create a collapsible list of comments. Each comment is a “<strong>_SingleComment</strong>” widget implemented in lines 34 to 67.</li><li>Notice the way I’ve created the list of comments in <strong>lines 22 to 28</strong>. I used the function <strong>.generate()</strong> to create a list. It’s an easy function to understand. Like the name of the function it generates a list. It takes as argument the number of items to generate and a function to create the items. Here, I’ve passed in a lambda function to create the “<strong>_SingleComment</strong>” widgets. <br>If you’re new to Dart and Lambda functions, here’s an <a href="https://medium.com/jay-tillu/lambda-functions-in-dart-7db8b759f07a">article</a> by <a href="https://medium.com/u/c29047bd0f64">JAY TILLU</a> that’ll help you learn it quickly.</li><li>Lines 50 to 53 uses the “<strong>UserDetailsWithFollow</strong>” widget to show the information of the user who commented. <br>See how convenient it is to reuse widgets? It saved us a bunch of time!</li><li><strong>Notice lines 1 to 6. </strong>Unlike before, though this class is used for key generation it is only partially responsible. It only gives the prefixes of the key values. <br>There is a reason why I implemented it like this. In a comment section, the same comment widget can appear multiple times. So, the keys assigned to each widget needs to be different. Otherwise I won’t be able to refer to a specific widget later on. <br>The idea is simple. <strong>Use the prefix with something else to make the key unique.</strong> In this case, I’ve used the index value to make them unique. I used the keys in line 25, 51, 56, and 60. See how I’ve done it in these lines.</li></ol><h3>Running the app</h3><p>That was a LOT of coding. I’m exhausted and my fingers hurt!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FFqdruC6cJYXxC%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FFqdruC6cJYXxC%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FFqdruC6cJYXxC%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/d23b6b1c537b18f700b0bc2f6fa44d64/href">https://medium.com/media/d23b6b1c537b18f700b0bc2f6fa44d64/href</a></iframe><p>But I gotta know how it looks! So, the moment of truth. Let’s run the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*bL6RkA-ItF1jnosPxeU3Yg.gif" /><figcaption>Excuse the sucky quality</figcaption></figure><h3>Summary</h3><p>We’re done for today. Hurrah!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FnXxOjZrbnbRxS%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FnXxOjZrbnbRxS%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FnXxOjZrbnbRxS%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="587" frameborder="0" scrolling="no"><a href="https://medium.com/media/48fdf92ddbf824c2c0aead562509b07c/href">https://medium.com/media/48fdf92ddbf824c2c0aead562509b07c/href</a></iframe><p>Today, we’ve learned about the followings:</p><ol><li>Expansion tiles</li><li>List creating function called generator</li><li>Using value keys in widgets</li></ol><p>Next post will be about widget tests (yes again.) and integration tests.</p><h3>Code changes</h3><p><a href="https://github.com/Shakleen/Leaf/pull/4/files">Visit here to see all the files that were modified.</a></p><h3>Important links</h3><ol><li><a href="https://github.com/Shakleen">GitHub Profile</a></li><li><a href="https://github.com/Shakleen/Leaf">Leaf GitHub Repository</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78eefb8ff447" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/88bc7d131e5b</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Wed, 27 Nov 2019 15:36:17 GMT</pubDate>
            <atom:updated>2019-11-30T05:22:40.517Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello, good people! Hope you’re having a wonderful day. Last time, I talked about data passing using models and inherited widget.</p><h3>Today I’m writing about</h3><ol><li>Importing dart packages from dart pub into a flutter project.</li><li>Refactoring widgets for reuse in building other widget trees.</li><li>Revisiting widget tests to make them better.</li></ol><p>Remember that all the code changes can be found at the end of the article. Let’s get started.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1TzKVQwH820wM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F1TzKVQwH820wM%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExbnBtd29raXh2aGpsYWgwZWxheWkzM2czZnBpb3F5ZmtsbWRxc3RxbiZlcD12MV9naWZzX2dpZklkJmN0PWc%2F1TzKVQwH820wM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d57b5ec664b4ba37ca3d6224ca45728/href">https://medium.com/media/4d57b5ec664b4ba37ca3d6224ca45728/href</a></iframe><h3>Date formatting</h3><h4>What is it?</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fj0YFA1gPjdnETHua52%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fj0YFA1gPjdnETHua52%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fj0YFA1gPjdnETHua52%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/e623edbf231b6d11e7347caee07c8018/href">https://medium.com/media/e623edbf231b6d11e7347caee07c8018/href</a></iframe><p>Basically, showing a date in a specific format. For example 27th November, 2019 and 27/11/2019 is the same day formatted two different ways.</p><h4>How to do it?</h4><ol><li><strong>Import a dart package</strong> called intl into the project.</li><li><strong>Use intl</strong> to return a formatted date to use anywhere.</li></ol><h4>Step #1: Importing a dart package</h4><p><a href="https://pub.dev/packages/intl#-installing-tab-">Here, you can find intl and the instructions for how to use it.</a></p><blockquote><strong>Pro tip: </strong><a href="https://pub.dev/">Dart pub</a> is the one stop shop to find any package for dart and flutter. If you need anything, search there. Someone might have already implemented it and made it a package.</blockquote><h4>Step #2: Using intl</h4><p>There’s one built in function in <strong>intl</strong> I’m going to use. You are encouraged to try formatting your own format string by following the guide <a href="https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html">here</a> if you want to. But I’m going to stick to the basic one provided.</p><p>In both <strong>PostModel</strong> and <strong>UserModel</strong>, I have a field of type <strong>DateTime</strong>. For both these fields, I’m going to create a getter. This getter function will call the format function from <strong>intl</strong> and return the formatted string of the DateTime properties. Refer to the code snippet below to see how I did this for <strong>PostModel</strong> class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3426bac74442d9dd18fbdc01496be732/href">https://medium.com/media/3426bac74442d9dd18fbdc01496be732/href</a></iframe><h3>DRY (Don’t Repeat Yourself)</h3><h4>What does it mean?</h4><blockquote><strong>Don’t repeat yourself</strong> (<strong>DRY</strong>, or sometimes <strong>do not repeat yourself</strong>) is a <a href="https://en.wikipedia.org/wiki/Principle#Principle_as_axiom_or_logical_fundament">principle</a> of <a href="https://en.wikipedia.org/wiki/Software_development_process">software development</a> aimed at reducing repetition of software patterns, replacing it with abstractions or using <a href="https://en.wikipedia.org/wiki/Data_normalization">data normalization</a> to avoid redundancy.</blockquote><blockquote>— Wikipedia</blockquote><h4>Why should you care?</h4><p>Copying and pasting code is easier to do. But it is really difficult to maintain. Say you have a piece of code that performs addition of two numbers. And you copy pasted this code in different places about 10 times. Later, you realize that you need to change it from adding to subtracting. So, you have to find those 10 places and then change the add to a subtraction sign. This will be extremely inconvenient when the project gets large. You’ll spend a lot of time just searching and changing stuff which will waste time.</p><h4>The main principle of DRY</h4><blockquote>The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”.</blockquote><p>In Layman&#39;s terms, if you ever feel the need to copy something, just create a function or place it in a public class or structure. You can just reuse this code multiple times and will save you the trouble of having to find all the uses when a change must be made.</p><h3>Code refactoring</h3><h4>What is it?</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1rf4hhXCoTQNa%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F1rf4hhXCoTQNa%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F1rf4hhXCoTQNa%2Fgiphy.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="379" frameborder="0" scrolling="no"><a href="https://medium.com/media/a89a5255cca48ffe5959a87eab7b3eab/href">https://medium.com/media/a89a5255cca48ffe5959a87eab7b3eab/href</a></iframe><blockquote>Code refactoring is the process of restructuring existing computer code — changing the <a href="https://en.wikipedia.org/wiki/Decomposition_(computer_science)">factoring</a> — without changing its external behavior.</blockquote><blockquote>— Wikipedia</blockquote><h4>Why should you care?</h4><p>Refactoring is intended to improve <a href="https://en.wikipedia.org/wiki/Non-functional_requirement"><em>nonfunctional</em></a> attributes of the <a href="https://en.wikipedia.org/wiki/Software">software</a>. It’s one way to achieve DRY. Advantages include</p><ol><li>Improved code <a href="https://en.wikipedia.org/wiki/Readability">readability</a>.</li><li>Reduced <a href="https://en.wikipedia.org/wiki/Cyclomatic_complexity">complexity</a>.</li><li>Improved <a href="https://en.wikipedia.org/wiki/Source_code">source-code</a> <a href="https://en.wikipedia.org/wiki/Maintainability">maintainability</a>.</li><li>More expressive internal <a href="https://en.wikipedia.org/wiki/Software_architecture">architecture</a>.</li></ol><h3>Flutter widget refactoring</h3><h4>Why should I do it?</h4><p>In Flutter, everything is a widget. An app is nothing but a tree of widgets. A widget can appear multiple times in different places in the widget tree or even in different widget trees. If the implementation of a widget is saved then it can be reused in different parts of the widget tree. Hence, the widget doesn’t need to be re-created from the ground up.</p><h4>Rough guidelines for refactoring widgets</h4><ol><li><strong>Give child widgets a name based on the duty it serves. </strong>For example: out <strong>PostCard</strong> widget will be made up of two things which are <strong>PostContents</strong> and <strong>PostDetails</strong>. <strong>PostDetails</strong> will be composed of <strong>UserDetails</strong> and <strong>PostStats</strong>. <strong>PostContents</strong> will composed of an <strong>ImageHeader</strong>, <strong>PostTitleAndSummary</strong> and <strong>PostTimeStamp</strong>. This is my way of dividing the parent widget based on what purpose the child widgets serve. Your way could be different from mine based on how you perceive the duty of the child widgets.</li><li><strong>A user defined widget could be used only once or multiple times to make different widgets.</strong> If a widget is only used once, then I prefer placing that widget with its parent widget in the same file as the parent widget. In this case, I will generally make the widget private by prefixing its name with an underscore. If it’s going to be used to create multiple different widgets, then I like placing it into it’s own file.</li><li>Say that there were widgets created through refactoring. Of these widgets some were placed into their own independent files. For these widgets, we need to <strong>write widget tests</strong>. In this case, a rule needs to be followed. <strong>Widget tests will thoroughly test the components of a widget if it is directly in that widget and not under a child widget.</strong> For example: Say you have a list of n text widgets. The widget test for the list will be to check whether it has n text widgets. But the widget test for the text widget will be to check if the text it is showing is the right one. The widget test for the list widget won’t check this one.</li></ol><h4>How far should widgets be refactored?</h4><p>Refactoring widgets are useful but not to an extreme point. You shouldn’t be creating a widget through refactoring that is made up only a single widget. The golden rule is simple to remember.</p><blockquote>Refactor widgets so that it makes your life easier. It should either be for easy testing, reading or understanding. If refactoring improves none of these, there’s no point in doing it.</blockquote><h3>Refactoring PostCard widget</h3><h4>Step #1: Planning out the refactoring game</h4><p>Lucky for me, I already factored out my <strong>PostCard</strong> widget into meaningful chunks already. In post 4, I emphasized on this point by saying it makes the code more readable and maintainable. I further refactored it a little. I refactored <strong>_PostDetails</strong> to create another widget called <strong>_UserDetails</strong>.</p><h4>Step #2: Moving refactored widgets into their files</h4><p>In the next post, I’ll be building the <strong>PostPage</strong>. For that I’m going to need some of the widgets we get from <strong>PostCard</strong>. The widgets I’ll need are as follows:</p><ol><li><strong>_UserDetails</strong> widget composed of a user iamge, user name and email.</li><li><strong>_PostTimeStamp</strong> widget showing the formatted date and time.</li><li><strong>_PostStats</strong> widget showing the view and react count of a post.</li></ol><p>What I’m going to do is</p><ol><li>Rename them to remove the underscore. This makes the widgets public.</li><li>Move them to their own files for reuse.</li></ol><h4>Step #3: Writing tests for new widgets</h4><p>These 3 new widgets will need tests of their own. So, I wrote 3 new widget tests for them. Another thing I should point out now. If you’ve read my code for widget tests from part 3, you’d see that I wrote a comment on top of a test to signify what the test was for. This can be improved. I’m showing how here. Notice the new widget test code snippet here.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85997faead11c08887cf73edfb767904/href">https://medium.com/media/85997faead11c08887cf73edfb767904/href</a></iframe><p>Notice line 7. Widget tests can be grouped like unit tests. Under a group you can have multiple <strong>testWidget</strong> function calls. I do it this way because I can now see what the test tests for in the code and handle each test individually. So, in short my approach is:</p><ol><li>Give <strong>group</strong> function a string that says which widget is being tested.</li><li>Give <strong>testWidgets</strong> function a string that says what it is being tested on.</li></ol><p>Everything else is the same.</p><h3>Summary</h3><p>Today was mostly talk and less code. The talk was important to hammer a few points in. Proper structure is important when building a project. Its better to follow a structured way from the very beginning than to wait until the project becomes a mess. To avoid this always follow the DRY principle. And to implement the DRY principle always refactor code to make it look good and understandable.</p><h3>Code Changes</h3><p><a href="https://github.com/Shakleen/Leaf/pull/3/files">Visit here to see the changes.</a></p><h3>Important links</h3><ol><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>That’s all for today. Thanks for reading! Stay safe and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88bc7d131e5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a19c3699a56e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Sun, 24 Nov 2019 17:25:16 GMT</pubDate>
            <atom:updated>2019-11-30T05:22:08.170Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello, good people! I hope you’re having a wonderful day. Last time, I talked about gestures, navigation and orientation.</p><h3>Today I’m writing about</h3><ol><li>Creating, passing and working with data models.</li><li>Passing data in the widget tree using Inherited widgets</li></ol><p>Lots of stuff to do. <a href="https://github.com/Shakleen/Leaf/pull/2/files">Remember that all the code changes can be viewed here. </a>Let’s get started.</p><h3>Creating models</h3><p>Before we can pass data around the widget tree. We need to create model.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F4hsnjGgoxKxR6%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F4hsnjGgoxKxR6%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F4hsnjGgoxKxR6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/36678e1927d2eaefa0f4b00d73c3f96e/href">https://medium.com/media/36678e1927d2eaefa0f4b00d73c3f96e/href</a></iframe><h4>Model as in Fashion model?</h4><p>Errm… No. (Though it would be so cool to code up a fashion model.) When I say model I mean the following</p><blockquote>A model is a class that has some properties and some functions.</blockquote><p>For example, say you have a model of a cat. The class <strong>CatModel</strong> should contain</p><ol><li><strong>Properties</strong> of a cat such as no of legs, no of eyes, no of times it’ll wake you up in the middle of the night and so on.</li><li><strong>Functions</strong> of a cat like eating, sleeping, staring at a blank wall corner for 5 minutes etc.</li></ol><h4>Why create a model?</h4><p>Working with models is convenient.</p><ol><li><strong>Easily find properties and functions </strong>related to a common type of object under one place i.e. in the class. Example: Find how many legs, eyes etc. a cat has from the <strong>CatModel</strong> class object.</li><li><strong>Cleaner code</strong> when working with classes. Example: You need to pass data related to a cat to a function. You could create 3 different parameters for that function and then pass in 3 arguments every time. Or you could just make do with one, the <strong>CatModel</strong> itself. because <strong>CatModel</strong> will contain all 3 of the properties. You can reference the properties when needed.</li><li><strong>Parsing becomes easier.</strong> Later posts will talk about json parsing in detail. Models will play a huge role there.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FVhneMg6g1xrDiEpVUu%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FVhneMg6g1xrDiEpVUu%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FVhneMg6g1xrDiEpVUu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="242" frameborder="0" scrolling="no"><a href="https://medium.com/media/11bd7e683245b326a0e30e3b2b31356b/href">https://medium.com/media/11bd7e683245b326a0e30e3b2b31356b/href</a></iframe><h4>My models</h4><p>Can’t create fashion models. But I can create class models. Here are the two models I created. I will need them for passing around data in a few moments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/239d16399e320635168e55272eb0850e/href">https://medium.com/media/239d16399e320635168e55272eb0850e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceb7f231264a1d02a19641e7c4e9bbae/href">https://medium.com/media/ceb7f231264a1d02a19641e7c4e9bbae/href</a></iframe><h3>Showing different posts in the news feed</h3><p>The app home page currently shows the same exact post 5 times. A realistice news feed will have a variety of different posts. So, I need <strong>PostCard</strong> widgets in <strong>ListView</strong> to be different from one another. To do make them different, 3 things need to be done.</p><ol><li>We need 3 different fake post data. That can easily be created. I’ll just create 3 differing <strong>PostModel</strong> objects.</li><li>Modify <strong>PostCard</strong> to take data input. This is also easy to do. I just need to create a property in <strong>PostCard</strong> class for the data and accept value for it using the <strong>PostCard</strong> constructor.</li><li>Modify <strong>PostCard</strong> to show the inputted data. For this, the data in <strong>PostCard</strong> widget needs to be propagated to the widgets further down the widget tree. Meaning, the widgets that make up <strong>PostCard</strong>, need to access the post data somehow.</li></ol><h4>Step 1: Using models to create fake data</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F134MhjLjg4mhc4%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F134MhjLjg4mhc4%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F134MhjLjg4mhc4%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/836353e2766910f1b6d145d6d2e8fbd5/href">https://medium.com/media/836353e2766910f1b6d145d6d2e8fbd5/href</a></iframe><p>Let me create 3 fake post and user data using my <strong>PostModel</strong> and <strong>UserModel</strong> classes. I created the fake data in my <strong>DemoValues</strong> class. <em>Notice that I have deleted the previous dummy values.</em> We won’t need them anymore. We’ll use these new ones everywhere.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/191a24b89c3519838430886740fbe87f/href">https://medium.com/media/191a24b89c3519838430886740fbe87f/href</a></iframe><h4>Step 2: Making PostCard be able to take in data</h4><p>We just need to modify the <strong>PostCard</strong> widget to make it take external data. And that’s very easy to do. Just add a new property to the <strong>PostCard</strong> widget class and add it to the constructor. Refer to the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad8133528bac78fd2973511f7662071b/href">https://medium.com/media/ad8133528bac78fd2973511f7662071b/href</a></iframe><h4>Step 3: Showing the inputted data</h4><p>All that remains now is to use the post data. The child widgets of the <strong>PostCard</strong> widget sub-tree need to access this post data and show it to the screen. We already created the widget tree. We just need to get the data to the widgets. But wait. How do we pass it to the child widgets further down the tree? We could do this in two ways. The latter of the two is better.</p><ol><li>Do the same thing as the above snippet. Just modify all the classes to have an additional property for the post data. Then pass the data from parent to child.</li><li>Or we could take advantage of inherited widgets.</li></ol><h3>Inherited widgets? The frick is that?</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FCPskAi4C6WLHa%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FCPskAi4C6WLHa%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FCPskAi4C6WLHa%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="198" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc3da9e2d0fc3141c59d893d7625df24/href">https://medium.com/media/cc3da9e2d0fc3141c59d893d7625df24/href</a></iframe><p>Don’t be afraid of the term. It’s an easy concept to understand. The flutter team explains this widget extremely well. See this video to understand what it is and how to use it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZbm3hjPjQMk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZbm3hjPjQMk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FZbm3hjPjQMk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/93acf2eb17f07f6a482f2f35a559c464/href">https://medium.com/media/93acf2eb17f07f6a482f2f35a559c464/href</a></iframe><h4>Creating an inherited widget</h4><p>If you watched the video I linked above, you know how simple it is to create this widget. I’m going to create a folder called <strong>inherited_widgets</strong> in <strong>lib/widgets/</strong> for all my inherited widgets. Here, I’m going to create my inherited widget for post model. I’m naming the file <strong>inherited_post_model.dart</strong> and filling it up with the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bebc414dc2bbcb8a50bc75627f034da5/href">https://medium.com/media/bebc414dc2bbcb8a50bc75627f034da5/href</a></iframe><h4>Using the inherited widget to pass data</h4><p>There are two parts to this which are as follows:</p><ol><li>I need to pass in the data by first creating a <strong>InheritedPostModel</strong> widget in the PostCard widget.</li><li>I need to retrieve the data using either <strong>inheritedWidgetOfExactType()</strong> function. Or using <strong>InheritedPostModel.of(context)</strong></li></ol><p>First, I create the <strong>InheritedPostModel</strong> widget. Then I pass the data in.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b4d39fd366b750912150c543230a81d/href">https://medium.com/media/6b4d39fd366b750912150c543230a81d/href</a></iframe><p>Now, I will try to grab the data from the inherited widget. Here, I show how I do it for one of the many child widgets of <strong>PostCard</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23c44d0875cb4da60de1eec4b0d38373/href">https://medium.com/media/23c44d0875cb4da60de1eec4b0d38373/href</a></iframe><p>I did the same thing everywhere I used static values from <strong>DemoValue</strong>s class. Now, the PostCard widget doesn’t need the <strong>DemoValues</strong> class at all. Try to do the rest of the widgets. If you’re facing trouble you can always check out the repository for the code.</p><h4>Updating HomePage widget</h4><p>The final thing to do is to update the home_page.dart file. Previously, no data was being passed to the <strong>PostCard</strong> widget from the <strong>HomePage</strong> widget. Now, we are passing in data. The data being passed is from the <strong>DemoValues</strong> class. See the code snippet below to see what has changed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5235a1f3a67f957b5f66f034708dc7e/href">https://medium.com/media/c5235a1f3a67f957b5f66f034708dc7e/href</a></iframe><h3>Running the app</h3><p>Running the app now doesn’t destroy the universe. I’ve made a lot of changes under the hood. But the app still works as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*LWZAI-J8ThgAi-rcHv_Lcg.png" /><figcaption>I need to format the date but I can do that later.</figcaption></figure><h3>What about passing data to PostPage widget?</h3><blockquote>Can <strong>PostPage</strong> retrieve data from inherited widget as well? The answer is NO.</blockquote><p>Because <strong>PostPage</strong> is not a child of <strong>PostCard</strong>. It is a separate page. Meaning, it is not a part of the widget tree of <strong>PostCard</strong>. So, it can’t just inherit widget from <strong>PostCard</strong> widget. In this case, I have no choice but to pass the data via parameters. So that’s what I’ll do.</p><h4>I need to do 2 things</h4><p>First let me edit <strong>PostPage</strong> widget so that it can take <strong>PostModel</strong> data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6e4277a3df49115fdfa65fc579eb662/href">https://medium.com/media/f6e4277a3df49115fdfa65fc579eb662/href</a></iframe><p>Second, I need to now pass in post data from <strong>PostCard</strong> widget when I navigate to <strong>PostPage</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d202a495265166e6b379521163568004/href">https://medium.com/media/d202a495265166e6b379521163568004/href</a></iframe><p>So, now if we run the app and try to go to the post page, we’ll see that each post produces a different post page. The post page isn’t pretty I know. But it does change based on the post being tapped. So, that’s great! I’ll design the post page in the next post. For now, we’ve achieved a lot!</p><h3>Summary</h3><p>So that was a long post. We went through a lot of things today. I hope you get why we’re doing things the way we’re doing them. If you get that much that’s enough. I’m very proud of what we’ve learned today.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl49JHLpRSLhecYEmI%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl49JHLpRSLhecYEmI%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl49JHLpRSLhecYEmI%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c325e10b5549961764d7df102dd2a25/href">https://medium.com/media/5c325e10b5549961764d7df102dd2a25/href</a></iframe><p>Remember to update the test for <strong>PostPage</strong> widget and commit your code.</p><h3>Code changes</h3><p><a href="https://github.com/Shakleen/Leaf/pull/2/files">Visit here to see code changes</a></p><h3>Important links</h3><ol><li><a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html">Inherited Widget</a></li><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>That’s all for today. Thanks for reading! Stay safe and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a19c3699a56e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fundamental Flutter Widgets — Part 1]]></title>
            <link>https://medium.com/@shakleenishfar/fundamental-flutter-widgets-part-1-482495624938?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/482495624938</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[flutter-ui]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Sun, 24 Nov 2019 13:03:36 GMT</pubDate>
            <atom:updated>2019-11-27T16:12:49.132Z</atom:updated>
            <content:encoded><![CDATA[<h3>Fundamental Flutter Widgets — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*SfqwR53kczsdc5CR.jpg" /></figure><p>Hello, good people! Hope you’re having a wonderful day! Today I’m writing about the fundamental widgets that every flutter developer should know and their use cases. Here’s a list of the widgets I’m writing about:</p><ol><li>Container</li><li>Row</li><li>Column</li><li>Expanded</li><li>Padding</li><li>SizedBox</li><li>AspectRatio</li></ol><p>The post is going to be pretty long. So let’s get started!</p><h3>1. <a href="https://api.flutter.dev/flutter/widgets/Container-class.html">Container</a></h3><h4>What is it?</h4><p>Just as the name suggests this widget contains another child widget. The child widget is confined to the area defined by the container. If no height and width value is provided then the container widget will expand to give enough space to the child widget. The documentation says the following:</p><blockquote>A convenience widget that combines common painting, positioning, and sizing widgets.</blockquote><blockquote>A container first surrounds the child with <a href="https://api.flutter.dev/flutter/widgets/Container/padding.html">padding</a> (inflated by any borders present in the <a href="https://api.flutter.dev/flutter/widgets/Container/decoration.html">decoration</a>) and then applies additional <a href="https://api.flutter.dev/flutter/widgets/Container/constraints.html">constraints</a> to the padded extent (incorporating the width and height as constraints, if either is non-null). The container is then surrounded by additional empty space described from the <a href="https://api.flutter.dev/flutter/widgets/Container/margin.html">margin</a>.</blockquote><h4>What are the important parameters?</h4><ol><li><strong>Height and width</strong></li><li><strong>Decoration</strong>: A way to decorate the container using BoxDecoration.</li><li><strong>Alignment</strong>: Align the child widget inside the container.</li><li><strong>Padding and Margin</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/0*EVMBQZzVKpbNX0Gu.gif" /></figure><h4>When to use?</h4><p>A container is a versatile widget. The common usage includes</p><ol><li>Separating out widgets from each other.</li><li>Adding decoration to widgets.</li><li>Adding size constraints to widgets.</li><li>Adding margin and padding.</li><li>Aligning child widget.</li></ol><blockquote><strong>Pro tip:</strong> Use a Container when you need to do multiple of the above mentioned things. For doing just one consider other widgets. Using a widget that was built for a specific purpose, will make the code more READABLE. Say, you have to add only padding to a widget. You can do so using both <strong>Container</strong> and <strong>Padding</strong> widget. Use padding because it will make your intent more clear to the reader of the code.</blockquote><h3>2. <a href="https://api.flutter.dev/flutter/widgets/Row-class.html">Row</a></h3><h4>What is it?</h4><p>A row is used to stack children widgets horizontally.</p><h4>What are its properties?</h4><ol><li>Takes multiple child widgets</li><li>Fills the available horizontal space</li><li>Is NOT scroll-able by default.</li></ol><h4>What are the important parameters?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/0*hmdLxifYCYwYgkK6.png" /></figure><ol><li><strong>MainAxisAlignment</strong>: Determine how the children are placed in the main axis. For row, the main axis is the X axis or horizontal axis.</li><li><strong>CrossAxisAlignment</strong>: Determine where the children are placed in the cross axis. For row, the cross axis is the Y axis or vertical axis.</li><li><strong>MainAxisSize</strong>: The exact size of the row widget where the children are to be placed.</li></ol><h4>When to use?</h4><p>To stack widgets horizontally.</p><h4>Common errors and fixes</h4><ol><li>Row is NOT by default scroll-able. So, if your children don’t fit within the screen size you’ll get a render overflow error. Use <strong>ListView</strong> with horizontal scrolling to make a scroll-able row like widget.</li><li>Row take all the available horizontal space. If you have other widgets you want to place beside a row, wrap the row in a container or specify the main axis size of the row.</li></ol><h3>3. <a href="https://api.flutter.dev/flutter/widgets/Column-class.html">Column</a></h3><h4>What is it?</h4><p>A widget that stacks children widgets vertically. This is, like row, also a multi child widget. Meaning, it takes a list of widgets as children.</p><h4>What are the important parameters?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/147/0*uH0orVPOeE0KjQoq.png" /></figure><ol><li><strong>MainAxisAlignment</strong>: The main axis of a column is the Y axis. This property determines how children widgets are placed in the main axis.</li><li><strong>CrossAxisAlignment</strong>: The cross axis for column widget is the X axis. This property determines how children widgets are placed in the cross axis.</li><li><strong>MainAxisSize</strong>: The exact size of the column widget where the children are to be placed.</li></ol><h4>When to use?</h4><p>To stack widgets vertically.</p><h4>Common errors</h4><ol><li>Column is NOT by default scroll-able. So, if your children don’t fit within the screen size you’ll get a render overflow error. Use <strong>ListView</strong> with vertical scrolling instead to make a scroll-able column like widget.</li><li>Column take all the available vertical space. If you have other widgets you want to place around a column, wrap the column in a container or specify the main axis size of the column.</li></ol><h3>4. <a href="https://api.flutter.dev/flutter/widgets/Expanded-class.html">Expanded</a></h3><h4>What is it?</h4><p>An expanded widget is used to impose flex. Flex means how much free space a widget is allowed to take. According to the official documentation:</p><blockquote>Widget that expands a child of a <a href="https://api.flutter.dev/flutter/widgets/Row-class.html">Row</a>, <a href="https://api.flutter.dev/flutter/widgets/Column-class.html">Column</a>, or <a href="https://api.flutter.dev/flutter/widgets/Flex-class.html">Flex</a> so that the child fills the available space.</blockquote><p>Let me give an example to clear this up. Say we have 50 pixels of space. We have 3 widget that all take 10 pixels of minimum space. So, we’re left with 20 pixels of free space. The flex value of each of the 3 widgets will determine how this 20 pixel will be distributed to the 3 widgets. Say the widgets have a flex value of 1, 2 and 3. So, the space distribution between the widgets will be 1:2:3. So, widget 3 will get (3 * 20) / 6 = 10 pixels of free space. Widget 2 will get (2 * 20) / 6 = 6.33 pixel of free space. Widget 1 will get the remaining free space.</p><h4>What should I know about it?</h4><p>Flex property and space distribution based on flex value. It should be noted that Expanded is usually used for child widgets in column or row.</p><h4>When to use it?</h4><p>Hard coding the height and width is very bad practice. Because it doesn’t transfer well from device to device. So, expanded widgets should be used to specify dimensions of widgets. This way, the widgets are guaranteed to have proper ratio of sizes in any device.</p><h4>Common errors</h4><p>It’s common to be faced with render overflow error. Because the amount of space provided to a widget is insufficient for the widget. Then the widget isn’t rendered properly and will cause the error.</p><h3>5. <a href="https://api.flutter.dev/flutter/widgets/Padding-class.html">Padding</a></h3><h4>What is it?</h4><p>A widget used to add padding around any widget. This is a single child weight.</p><h4>What should I know about it?</h4><p>Padding values are set using EdgeInsets class. There are many ways to set the values like all(), only(), symmetric(), fromLRTB() etc.</p><h4>When to use it?</h4><p>Use it instead of container, when you only need to add padding to more than one side of a widget.</p><h3>6. <a href="https://api.flutter.dev/flutter/widgets/SizedBox-class.html">SizedBox</a></h3><h4>What is it?</h4><p>Exactly as the name suggests it’s a box that’s a specific size.</p><h4>What should I know about it?</h4><p>Height and width property.</p><h4>When to use it?</h4><ol><li>Useful to create a specific size container like enclosing to place widgets. Use it instead of container when only this is your goal.</li><li>Useful for providing proper spacing between widgets in a row or column when necessary.</li></ol><h3>7. <a href="https://api.flutter.dev/flutter/widgets/AspectRatio-class.html">AspectRatio</a></h3><h4>What is it?</h4><p>A widget used to express width and height of a widget as a ratio of one another.</p><h4>What should I know about it?</h4><p>Aspect ratio value and how it sizes the widget. Aspect ratio = width / height. It’s good practice to write code just like the division here. Even though at the end of the day it’ll be a simple double value. But this makes the intent clear and easy to understand.</p><h4>When to use it?</h4><p>When unsure about the actual width and height of a widget. Using aspect ratio ensures that the widget looks exactly same when it is scaled up or down. The proportions of the widget will be intact.</p><p>That’s all for today. Hope this article helped. Be safe and happy coding!</p><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=482495624938" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 5: Orientation, Gesture and Navigation]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cc4aea4f989</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Sat, 23 Nov 2019 14:48:28 GMT</pubDate>
            <atom:updated>2019-11-30T05:21:13.738Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 5: Orientation, Gesture and Navigation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello good people! Hope you’re having a wonderful day! Last time, I created a simple new feed with some posts in it. Today, I’m going to build on top of that.</p><h3>Today I’m writing about</h3><ol><li><strong>Orientation</strong> of mobile devices and designing UI while keeping it in mind.</li><li><strong>Gesture Detection</strong> and how to use it to implement functionalities.</li><li><strong>Basic navigation</strong> from one page to another and back.</li></ol><p>Hope you’re excited! Let’s get started.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F7kn27lnYSAE9O%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F7kn27lnYSAE9O%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExd2s4ZHlia2ltaXE0d2JvcW16ZnVhNmhneXJ2bDVycjRiamR6ejF5ZyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2F7kn27lnYSAE9O%2Fgiphy_s.gif&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="339" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8c4f6b637cd445526d536a878a418e9/href">https://medium.com/media/c8c4f6b637cd445526d536a878a418e9/href</a></iframe><h3>Past mistakes</h3><p>I made two mistakes in the last post (That I can think of). These are:</p><ol><li><strong>I didn’t update the test file for home_page.dart file.</strong> I changed the widget <strong>HomePage</strong>, but didn’t update the tests for it. That’s why when running the tests Travis CI informed me of test failure.</li><li><strong>When I designed the <em>PostCard</em> widget I didn’t consider the landscape orientation.</strong> I only designed it for portrait. When rotating the mobile the UI goes from decent to ugly fast.</li></ol><h3>Mistake #1: Not updating the tests</h3><blockquote><strong>Lesson: </strong>Always update your tests when you update your code, if the testing parameters have changed.</blockquote><h4>I removed two tests which were</h4><ol><li>Test to see if there are exactly n number of texts.</li><li>Test to see if “Hello World!” text appears.</li></ol><h4>I added two tests which were</h4><ol><li>Check to see if there is <em>exactly one</em> <strong>ListView</strong> widget.</li><li>Check to see if there is <em>at least one</em> <strong>PostCard</strong> widget.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cadc33bed279cca89fc7fcbc2ede7991/href">https://medium.com/media/cadc33bed279cca89fc7fcbc2ede7991/href</a></iframe><h3>Mistake #2: Not checking orientation</h3><blockquote><strong>Lesson:</strong> Always check user interface in both orientations when designing</blockquote><h4>Mobile Orientations</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/0*0yzvP46jFJ9w2kb4" /></figure><h4>What’s the mistake?</h4><p>In my last post, when designing the <strong>PostCard</strong> widget I only checked it in the portrait orientation. I forgot to look at it in the landscape orientation. As a result, the values I assigned to <strong>AspectRatio</strong> and <strong>Expanded</strong> widgets weren’t appropriate for the landscape orientation.</p><h4>What’s the fix?</h4><p>There are two ways to fix this problem.</p><ol><li>I could <strong>create a complete new widget</strong> for the landscape orientation. This would be a viable option if I wanted to make a different look for landscape orientation.</li><li>I could just <strong>assign values at specific points</strong> (after checking which orientation the device is in) of the current widget to make it fit the landscape orientation. I’m going to do this. Because I want the look to be the same in both orientations.</li></ol><p>I created a <em>global</em> <strong>_isLandscape()</strong> function that checks to see if the device is in landscape or not. The function uses <strong>MediaQuery</strong> to get the orientation of the device.</p><blockquote>MediaQuery class contains many useful data about the media (device) such as size, orientation and so on.</blockquote><p>If <strong>_isLandscape()</strong> returns true, I assign a value appropriate for the landscape orientation. Otherwise I assign a value appropriate for the portrait orientation.</p><h4>Code for checking orientation</h4><p>The snippet below shows how I used the function for <strong>AspectRatio</strong> widget. I checked to see if <strong>_isLandscape(context)</strong> is true. If so I assigned 6/2 to <strong>aspectRatio</strong> variable. Otherwise, I assigned 6/3 to it.</p><p>A few syntactical things to note here. You might know this from dart language overview but I’m gonna mention this here again.</p><ol><li><strong>condition ? statement1 : statement2</strong> is way of writing a simple if else condition. If condition is true, statement1 is executed. Otherwise statement2 is executed.</li><li>Anything that begins with _ is local to that scope. It can’t be used or even referenced outside that scope. In this case, <strong>_isLandscape()</strong> function can’t be used outside this file even if the file is imported there. Basically, use _ to make something private to that scope.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7510753e65212b3afe6e95f732a84e2/href">https://medium.com/media/e7510753e65212b3afe6e95f732a84e2/href</a></iframe><p>The same thing was done for <strong>Expanded</strong> widgets further down the widget tree. I’m not going to provide the snippet for those here. Check the repository to see the changes I’ve made.</p><p>After the fix this is how the app looks like now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*LdwcTKt54pK12D6qPbI5FA.gif" /><figcaption>New UI that takes into account the orientation of the screen.</figcaption></figure><h4>Updating tests for PostCard widget</h4><p>As I’ve changed the <strong>PostCard</strong> widget I need to update the tests for it. The updated tests can be found in my project repository.</p><h3>Gesture Detection</h3><h4>What are gestures?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*nTCgdv1_AdT8p5sV.jpg" /><figcaption>Common gestures</figcaption></figure><h4>What are their usage?</h4><p><strong>Gestures are used to interact with the app in a meaningful way. We can bind a gesture to execute a specific function.</strong> For example: tapping could do one thing, while long pressing could do another thing. Gestures provide a powerful mechanism to add functionality to the user interface of an app.</p><h4>Widget needed to implement gestures</h4><p>Gestures can be implemented by using <a href="https://api.flutter.dev/flutter/widgets/GestureDetector-class.html"><strong>GestureDetector</strong></a>. It has options to implement many different gestures. Some other widgets exist that have some gesture mechanisms built-in like <strong>ListTile</strong>, <strong>InkWell</strong>, <strong>Button</strong> etc. Today, I’ll use <strong>GestureDetector</strong> to implement the necessary functionality.</p><h4>Coding the implementation</h4><p>I added <strong>GestureDetector</strong> to the top level of my <strong>PostCard</strong> widget as shown in the code snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9434e3954f8a58675bdd08d89c4f0177/href">https://medium.com/media/9434e3954f8a58675bdd08d89c4f0177/href</a></iframe><p>In the <strong>onTap</strong> parameter, I’m going to write a function that’ll show a new page. In this page, the full length of the post will be shown. The page will allow the user to react to and comment on the post. For today, I’m just going to create a very basic page and navigate to it. I’ll design the page in the later part of the series.</p><h3>Navigation</h3><h4>What is navigation?</h4><p>In mobile apps, navigation means going from one page to another.</p><h4>How to navigate in Flutter?</h4><p>To navigate use the <a href="https://api.flutter.dev/flutter/widgets/Navigator-class.html"><strong>Navigator</strong></a> class. <strong>push</strong> and <strong>pushNamed</strong> functions can be used to go to a new page. Because I haven’t touched routing names yet, I’m going to use <strong>push()</strong> function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a4284c9fc586b35eb76a93c6f5bc4d4/href">https://medium.com/media/4a4284c9fc586b35eb76a93c6f5bc4d4/href</a></iframe><p><strong>Line 10 to 14</strong> shows how I implemented navigation. When a navigation happens, Flutter needs to know where to go. This can be done in two ways.</p><ol><li>A name can be assigned to a route or</li><li>A route can be created using <strong>MaterialPageRoute</strong> class.</li></ol><p>Here, I created a route using the latter.</p><h4>Wait… what is a Route?</h4><p><a href="https://flutter.dev/docs/cookbook/navigation/navigation-basics">Lemme quote the official flutter documentation</a></p><blockquote>In Flutter, <em>screens</em> and <em>pages</em> are called <em>routes</em>.</blockquote><p>For now knowing this much is enough. We’ll delve more into routing and advanced navigation in future posts. Saving and running the app shows that navigation works as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*T_kE1nzTQxD-mUYd8xwy2w.gif" /><figcaption>Navigating to new page</figcaption></figure><p>I wrote tests for the new page. Not going to post the snippet here. Check the repository for the code.</p><h3>Summary</h3><p>That’s all for today. We made great progress. We learnt about the following things today:</p><ol><li>When changing the contents of a file, make sure to update the tests as well.</li><li>Check orientation when designing the user interface components.</li><li>Use gesture to add functionality to UI components.</li><li>Navigating to a new page using Navigator API.</li></ol><h3>Important Links</h3><ol><li><a href="https://api.flutter.dev/flutter/widgets/MediaQuery-class.html">MediaQuery</a> | <a href="https://api.flutter.dev/flutter/widgets/GestureDetector-class.html">GestureDetector</a> | <a href="https://api.flutter.dev/flutter/widgets/Navigator-class.html">Navigator</a></li><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cc4aea4f989" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ed2097df871</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Fri, 22 Nov 2019 16:49:02 GMT</pubDate>
            <atom:updated>2019-11-30T05:20:37.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello, good people! Hope you’re having a wonderful day!</p><h3>Today I’m writing about</h3><ol><li>Importing and working with image assets.</li><li>More built-in widgets such as ListView, Container, Card etc.</li><li>Writing clean and maintainable code.</li><li>Tips to make our development fast and easier.</li></ol><p>That’s a lot of stuff to cover. Hope you’re ready. Cause I sure am!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FY3MbPtRn74uR3Ziq4P%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FY3MbPtRn74uR3Ziq4P%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FY3MbPtRn74uR3Ziq4P%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f11b6f2ac7101cc161ca5b933def969/href">https://medium.com/media/4f11b6f2ac7101cc161ca5b933def969/href</a></iframe><h3>Folder Structure</h3><p>Maintaining a proper folder structure can really help you navigate and find stuff quickly. I like to keep things very organized. My folder structure is given below. If you’re following along try to keep the same structure as mine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*axrAh4TLVVH9GfwUrkYL-A.png" /></figure><p>Many of files in the folders aren’t created yet. We’ll be creating them in this post. The main thing I want to get across is that, you should maintain a proper folder structure to categorize your files. It makes them easier locate.</p><h3>Importing assets</h3><p>Assets are a fancy term for resources. Assets can be images, videos, icons, font files etc. We’ll be using static assets to design our widgets. Later we’ll dynamically update the images with whatever needs to be shown. To import the pictures I’m going to follow the <a href="https://flutter.dev/docs/development/ui/assets-and-images">documentation here</a>.</p><h4>Step by step</h4><ol><li>Get two pictures. One for the user and one for the post.</li><li>Create a folder called <strong>assets</strong> and in it another folder called <strong>images</strong>. And in <strong>images</strong> folder save these two pictures.</li><li>Now, we need to tell flutter that we have two pictures in the directory <strong>assets/images/</strong>. We do so by specifying it in <strong>pubspec.yaml</strong> file located in the root project directory. Find assets portion, un-comment it and then specify the path. See the picture below to get an idea of what to do.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FPWkdFPeRAu1uo0vru2WZQ.png" /><figcaption>The changes made are highlighted.</figcaption></figure><h3>final, const and static keyword in dart</h3><p>“<strong>static</strong>”, “<strong>final</strong>”, and “<strong>const</strong>” mean entirely distinct things in Dart.</p><h4>static</h4><p><strong>“static”</strong> means a member is available on the class itself instead of on instances of the class. That’s all it means and it isn’t used for anything else.</p><blockquote>static modifies *members*.</blockquote><h4>final</h4><p><strong>“final”</strong> means single-assignment: a final variable or field *must* have a function for initialization. Once assigned a value, a final variable’s value cannot be changed.</p><blockquote>final modifies *variables*.</blockquote><h4>const</h4><p><strong>“const”</strong> has a meaning that’s a bit more complex and subtle in Dart.</p><blockquote>const modifies *values*.</blockquote><p>You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object’s entire deep state can be determined entirely at compile time and that the object will be frozen and <strong>completely immutable</strong>.</p><p><a href="https://news.dartlang.org/2012/06/const-static-final-oh-my.html">This article talks more in-depth about these 3 keywords if you want to know more.</a></p><h3>Working with demo values</h3><h4>What are demo values?</h4><p>Values which are hard coded. Meaning, values that do not change.</p><h4>When to use of demo values?</h4><p>Demo values are useful when you’re first designing your user interface components. I like to place them all in a single class located in a separate file. Later I reference the values from the class to use them.</p><h4>What are the conveniences of grouping demo values in a class?</h4><ol><li><strong>All demo values are located in one place.</strong> Convenient. I don’t have to go through code to find their uses.</li><li><strong>Easy to change a single demo value used in multiple places.</strong> Changing one value results in all instances being changed.</li><li><strong>Easily test using these values.</strong> When writing tests I don’t have to write the same constant value multiple times. So it saves time and energy.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fh4NwfmFlCnlKAAqrBD%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fh4NwfmFlCnlKAAqrBD%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fh4NwfmFlCnlKAAqrBD%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/b624cad4c1f8967e218ad2742ac8d9f5/href">https://medium.com/media/b624cad4c1f8967e218ad2742ac8d9f5/href</a></iframe><p>Now, we’re going to create a <strong>demo_values.dart</strong> file in <strong>lib/helper/</strong> where we’ll write the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4967296d232ce26c3a8313db3e22b035/href">https://medium.com/media/4967296d232ce26c3a8313db3e22b035/href</a></iframe><p>This class will contain the demo values we’ll use to create our <strong>PostCard</strong> widget. Notice that, in the class, I’ve created static final properties. The reason is simple.</p><ol><li><strong>Static because</strong>, I want to access the values of the class without creating an instance every time.</li><li><strong>final because</strong>, I don’t want to ever change the value of these variables ever in my code. And final protects against this.</li></ol><h3>News feed</h3><h4>Basic structure of a news feed</h4><p>A news feed is basically a list of posts. A user can scroll vertically or horizontally to see the posts. There are options to interact with each individual post. In this part, I’m going to create the list of posts. In the next part I’ll create the interaction. The posts will be identical for now, later I’ll make them unique.</p><h4>Widgets needed for the job</h4><p>To create a news feed we need to do two things.</p><ol><li>Create a widget that will view our post details. This will be custom made.</li><li>A list widget to show many post widgets. This can be done using a <strong>ListView</strong>.</li></ol><h3>Step #1: Creating the post list</h3><p>Let’s tackle the easy thing first. Creating the news feed list. We’re going to edit our <strong>HomePage</strong> widget to make it show a list of posts. We usually create a list of any kind widgets using a <strong>ListView</strong>. So, modify your <strong>HomePage</strong> widget in<strong> home_page.dart</strong> file with the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56868584f0a857c38ef134f0106f6aa8/href">https://medium.com/media/56868584f0a857c38ef134f0106f6aa8/href</a></iframe><p>Notice in the code in lines 13 to 18 I’ve implemented a list using <strong>ListView</strong> by using a <strong>.builder()</strong> method. It takes two parameters which are</p><ol><li><strong>itemCount: </strong>The number of widgets to build.</li><li><strong>itemBuilder: </strong>A function to build that type of widget. The function itself takes two parameters which are <strong>BuildContext</strong> and <strong>int</strong> type objects. It returns a widget which in this case is a widget called <strong>PostCard</strong>. We’re going to make this next.</li></ol><blockquote><strong><em>Important to know: ListView</em></strong> widget is by default scroll enabled. So, we can scroll to see multiple widgets.</blockquote><h3>Step #2: Creating a widget for the post itself</h3><p>Each post in the feed list will be shown as a card. The card will have information related to a post.</p><h4>Widgets necessary</h4><p><a href="https://api.flutter.dev/flutter/widgets/Container-class.html">Container</a> | <a href="https://api.flutter.dev/flutter/material/Card-class.html">Card</a> | <a href="https://api.flutter.dev/flutter/widgets/Padding-class.html">Padding</a> | <a href="https://api.flutter.dev/flutter/widgets/Row-class.html">Row</a> | <a href="https://api.flutter.dev/flutter/widgets/Column-class.html">Column</a> | <a href="https://api.flutter.dev/flutter/widgets/Expanded-class.html">Expanded</a> | <a href="https://api.flutter.dev/flutter/widgets/SizedBox-class.html">SizedBox</a> | <a href="https://api.flutter.dev/flutter/material/CircleAvatar-class.html">CircleAvater</a> | <a href="https://api.flutter.dev/flutter/widgets/AspectRatio-class.html">AspectRatio</a></p><p>I’ve hyperlinked the official documentation of each widget. You don’t need to read the entire doc. Just the first few paragraphs to get a good understanding of what the widget does is enough. <a href="https://medium.com/@shakleenishfar/fundamental-flutter-widgets-part-1-482495624938">Here’s an article I wrote on about these widgets, their usage and their parameters.</a></p><h4>Proper way to write a widget that has many children widgets</h4><p>Below I give two codes of the <strong>PostCard</strong> widget. <strong>Just a disclaimer the codes will be slightly huge. You don’t have to understand everything here. Just give a quick glance over both the codes.</strong> My point isn’t syntactical. It’s more related to coding style.</p><h4>The Horrible Coding Style</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ac36e647b7f628910c9d9842196e42c/href">https://medium.com/media/4ac36e647b7f628910c9d9842196e42c/href</a></iframe><h4>The Good Coding Style</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2e113e552f02ec10fcf5786c452fef0/href">https://medium.com/media/a2e113e552f02ec10fcf5786c452fef0/href</a></iframe><h4>The 2nd one is better. But why?</h4><p>If you tried reading both the codes, you’ll see that the 2nd one is easier to understand. They both have the same exact widget tree. But the 2nd code snippet is much better because it is more <strong>READABLE</strong> and <strong>UNDERSTANDABLE</strong>. This is a huge factor in software development. This matters because:</p><ol><li>Easily read, understand and change code.</li><li>Easily isolate a specific part of a UI.</li><li>Easily test a portion of the UI.</li></ol><h4>The main takeaways</h4><ol><li><strong>Create smaller chunks of code with a definitive purpose.</strong> If your widget tree is going to be huge, it’s better to give each piece a name that reflects its purpose. Later, when you want to change a part of the UI, you can easily find it in the widget implementation. There’s no straight forward guide as to how to do it.<strong><em> I just follow this simple rule: “This portion of the widget does this, so I’ll name it this.”</em></strong> Like, a portion of the <strong>PostCard</strong> will show user info so I’ll name it <strong>UserDetails</strong> widget.</li><li><strong>Create separate widgets and don’t create functions that return a widget. </strong>Functions that return widgets need to be re-drawn every single time because they aren’t saved. And re-drawing is expensive.</li></ol><blockquote><strong>Useful tip: </strong>It’s actually not that hard to code. But to write code that’s easy for other people to understand is a form of art. If you’re interested in learning more about this consider reading the amazing books <strong>Clean Code</strong> and <strong>Pragmatic Programmer</strong>. In my opinion, these books are master pieces when it comes to writing clean and proper code.</blockquote><h3>Running the project</h3><p>So, if you’ve run the project then you should see something like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*18zDAAlq39zT3KK7HUV-6A.gif" /></figure><p>The project runs, the app doesn’t have errors. But that UI is just so damn ugly.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F67SXeoc8RLwvqCwn2F%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F67SXeoc8RLwvqCwn2F%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F67SXeoc8RLwvqCwn2F%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="348" frameborder="0" scrolling="no"><a href="https://medium.com/media/e32495a0b2e0cb68f6b2411109281f14/href">https://medium.com/media/e32495a0b2e0cb68f6b2411109281f14/href</a></iframe><p>Let’s try to make it look more appealing using some more widgets available to us</p><h3>User Interface Designing</h3><p>Three things to consider for a good user interface. Proper size, spacing and color.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FMeDzQck98BBpC%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FMeDzQck98BBpC%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FMeDzQck98BBpC%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fee88039dbae2b8322ab1aa4bf70837/href">https://medium.com/media/2fee88039dbae2b8322ab1aa4bf70837/href</a></iframe><p>To make the UI look better we need to play around with the parameters of the various widgets used. For example: To fix the spacing issues we’ll need to add padding and margin. This can be done with parameters in the<strong> Container</strong> widget. Setting <strong>MainAxisAlignment</strong> and <strong>CrossAxisAlignment</strong> will help in aligning the children widgets. Give a go at fixing up the UI. When you’re done come back here to read on and see how I’ve done.</p><p>So after tweaking around for like 30 minutes, I got the following UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*SFyBcuWyq8lErPfzqCbyBQ.png" /><figcaption>More decent looking UI</figcaption></figure><p>I’m not going to further tune this UI to perfection. But you’re encouraged to do so.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b0cf79add3c90433bb6999fe10af8f9/href">https://medium.com/media/1b0cf79add3c90433bb6999fe10af8f9/href</a></iframe><blockquote><strong>Useful tip: </strong>Creating a great looking user interface is art. Getting good at UI design requires looking at more designs and studying. <a href="https://www.creativebloq.com/advice/ui-design-rules">Here’s a great article on the topic of mastering user interface design.</a></blockquote><h3>Testing the widget</h3><p>So now that we’ve created our <strong>PostCard</strong> widget we’ll need to write tests for it. Remember when creating <strong>DemoValues</strong> class I said it would make testing easier? Well see for yourself in the code below. You can directly test static values without having to remember what they exactly were or having to copy paste them. Saving us a lot of time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c163958aca75c5fa395483d9f8681f0c/href">https://medium.com/media/c163958aca75c5fa395483d9f8681f0c/href</a></iframe><h3>Summary</h3><p>This was a long part I know. But we’re done.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FMp4hQy51LjY6A%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FMp4hQy51LjY6A%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FMp4hQy51LjY6A%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="290" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef9d3435ed3bc8df4158304f01dedcdb/href">https://medium.com/media/ef9d3435ed3bc8df4158304f01dedcdb/href</a></iframe><p>We learned the following things today:</p><ol><li>Importing assets into flutter and using them.</li><li>Fake (demo) data and how to use it to design, develop and test widgets.</li><li>ListView, Container, Padding, SizedBox and many more basic widgets.</li><li>Importance of clean, maintainable and self-documenting coding style.</li></ol><p>Remember to commit and push changes to your git repository. Some tests failed in Travis CI. I’ll take a look and fix it later.</p><h3>Important Links</h3><ol><li><a href="https://api.flutter.dev/flutter/widgets/Container-class.html">Container</a> | <a href="https://api.flutter.dev/flutter/material/Card-class.html">Card</a> | <a href="https://api.flutter.dev/flutter/widgets/Padding-class.html">Padding</a> | <a href="https://api.flutter.dev/flutter/widgets/Row-class.html">Row</a> | <a href="https://api.flutter.dev/flutter/widgets/Column-class.html">Column</a> | <a href="https://api.flutter.dev/flutter/widgets/Expanded-class.html">Expanded</a> | <a href="https://api.flutter.dev/flutter/widgets/SizedBox-class.html">SizedBox</a> | <a href="https://api.flutter.dev/flutter/material/CircleAvatar-class.html">CircleAvater</a> | <a href="https://api.flutter.dev/flutter/widgets/AspectRatio-class.html">AspectRatio</a> | <a href="https://api.flutter.dev/flutter/material/Card-class.html">Card</a> <a href="https://api.flutter.dev/flutter/widgets/ListView-class.html">| ListView </a>and <a href="https://medium.com/@DakshHub/flutter-displaying-dynamic-contents-using-listview-builder-f2cedb1a19fb">ListView.builder</a></li><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ed2097df871" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 3: Widget Tests]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a33da9092756</guid>
            <category><![CDATA[education]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Fri, 22 Nov 2019 16:45:49 GMT</pubDate>
            <atom:updated>2019-11-30T05:19:58.995Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 3: Widget Tests</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello good people! Hope you’re having a wonderful day! Last time we wrote our first few lines of Dart code to create a very basic UI.</p><h3>Today I’m writing about</h3><ol><li>Concept and importance of testing</li><li>Widget tests</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FKA3ZgtR2bfpIY%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FKA3ZgtR2bfpIY%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FKA3ZgtR2bfpIY%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/819e15fcd82ccf136fa01c75b1a018d8/href">https://medium.com/media/819e15fcd82ccf136fa01c75b1a018d8/href</a></iframe><h3>Testing</h3><h4>Why test?</h4><p>Testing is an important part of software development. It ensures that the application we create works the way it should be working. When the project becomes sufficiently huge it becomes hard to keep track of things. As a result when adding new features or changing existing ones, it is very hard to know whether the previously working feature got broken. So, we write tests to check the behavior. If the tests pass then the behavior of the feature is intact after modification. However, there could still be bugs not covered and tested by the tests themselves. That is why it is important to write proper tests and thoroughly test the application at every step of the way.</p><h4>Tests in Flutter</h4><p>There are many different variations of tests. However based on the scope of testing they are mainly categorized into 3 types which are</p><ol><li><strong>Unit tests: </strong>Tests a single function or class.</li><li><strong>Component tests:</strong> Tests multiple functions or classes and how they relate.</li><li><strong>Integration tests: </strong>Tests the entire app or a huge chunk of the app.</li></ol><p><a href="https://flutter.dev/docs/testing">Here’s a comparison taken directly from the official documentation</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*v3tLv8ZbjGIzPU6Elfm1lA.png" /><figcaption>Different tests types and their properties</figcaption></figure><p><a href="https://flutter.dev/docs/testing">Quoting from the official docs</a></p><blockquote>Generally speaking, a well-tested app has many unit and widget tests, tracked by <a href="https://en.wikipedia.org/wiki/Code_coverage">code coverage</a>, plus enough integration tests to cover all the important use cases.</blockquote><p>Writing tests are easy. But knowing what to test requires planning and experience. Let’s write some tests for the simple app we have so far.</p><h3>Writing tests</h3><h4>Leaf widget test</h4><p>All our tests should be placed in the <strong>test</strong> folder in the root of our project directory. I like to separate out my test types into folders of their own named <strong>unit_tests, widget_tests </strong>and<strong> integration_test.</strong> The convention to name test files is pretty simple. Say you are writing a test for a function in <strong>file.dart</strong>, then the test file should be named <strong>file_test.dart</strong>. So let’s create <strong>main_test.dart</strong> inside the widget_test folder and write the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ebfd3c971e1880be236db5627bea424/href">https://medium.com/media/9ebfd3c971e1880be236db5627bea424/href</a></iframe><p>Let’s go line by line.</p><p><strong>Line 7</strong> defines a test. The <strong>testWidgets()</strong> function is used to define tests. It takes two parameters. The first is a string use to specify either the name or scope of the test. The second is a function that actually tests the code. The function takes a <strong>WidgetTester</strong> object as input. This object allows the testing code to draw the widget being tested.</p><p><strong>async</strong> and <strong>await</strong> keywords here are especially important. <strong>async</strong> signifies that this function is asynchronous. Meaning, the execution of this function will be done on a different thread. While the <strong>await</strong> keyword says that the testing code shouldn’t proceed beyond line 8, unless it finishes execution. If you don’t understand this don’t worry. We’ll revisit them in later posts.</p><p><strong>Line 11 and 14 </strong>are codes that test the Leaf widget. Let’s go slowly here. <strong>find</strong> tries to find a widget using a mechanism (in this case <strong>byType()</strong>) and returns the result (a <strong>Matcher</strong> object) of the search. <strong>expect()</strong> compares this result with what it should actually be (In this case <strong>findsOneWidget</strong> which means only one widget should be found in both tests). [As for what a Matcher is and what it does isn’t necessary to understand. You can refer online to learn more about it if you want to.]</p><p>That’s it. That’s our simple test for <strong>Leaf</strong> widget. Similarly we can write one for <strong>HomePage</strong> widget as well.</p><h4>HomePage widget test</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82466fc1f46e88de814189dec6bcbcfd/href">https://medium.com/media/82466fc1f46e88de814189dec6bcbcfd/href</a></iframe><h4><strong>Two things to note here</strong></h4><p>First is why we wrapped our widget in <strong>MaterialApp</strong> here, but not in the previous widget. The reason is that <strong>MaterialApp</strong> as a root is necessary for widgets lower down the tree. And since we’re testing widgets, they need the <strong>MaterialApp</strong> widget as a root. In the previous example, the widget Leaf is itself returning a <strong>MaterialApp</strong>, where in this case it isn’t. So the simple thing to remember is, if the widget doesn’t have a <strong>MaterialApp</strong>, then enclose it one. What would happen if we didn’t do so? Why not try running the test without including it. You’ll get your answer right away. (The test fails and gives you a huge error message. But the message says what’s wrong and how to fix it.)</p><p>Second is the <strong>findsNWidgets()</strong> function. We use it to check if there are N instances of a certain widget. In this case, we tested to see if there are 2 Text widgets in <strong>HomePage</strong>.</p><h4>Running tests</h4><p><strong>VS Code:</strong> Go to debugging tab and start debugging. Gives the following output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*qJxZVZj-SDurATKtKTbtLg.png" /><figcaption>Running tests through VS Code</figcaption></figure><p><strong>Terminal:</strong> Type the following commands</p><pre>$ flutter test test/widget_tests/home_page_test.dart<br>$ flutter test test/widget_tests/main_test.dart</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*3JCA999VYb34n4lqIWEAnQ.png" /><figcaption>Running tests through terminal</figcaption></figure><h3>Committing and pushing our code</h3><p>Before we finish this part, we’re going to commit our code and push it to GitHub. But we’re going to create a branch first. When in development, we work in a branch. When developing an app never ever push to main directly. When our feature or app is finalized, we will merge the branch with main. So, let’s create a branch, commit changes and push our work.</p><pre>$ git checkout -b Home_Page_Dev # Create and switch to new branch<br>$ git add . # Stage all changes<br>$ git commit -m &quot;Basic HomePage created&quot; # commit changes<br>$ git push -u origin Home_Page_Dev # push changes</pre><h3>Summary</h3><p>That’s it for today. We learned about testing and wrote some widget tests of our own.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FZY2DmHSTFpmmEH5STD%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FZY2DmHSTFpmmEH5STD%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FZY2DmHSTFpmmEH5STD%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/926e1ca5c9cddba9637b0232790f5f0d/href">https://medium.com/media/926e1ca5c9cddba9637b0232790f5f0d/href</a></iframe><p>We also learned a little bit about using branches to maintain our development using GitHub and Git.</p><h3>Important Links</h3><ol><li><a href="https://flutter.dev/docs/testing">Testing Flutter Apps</a></li><li><a href="https://flutter.dev/docs/cookbook/testing/widget/introduction">An Introduction to widget tests</a></li><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a33da9092756" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 2: Widgets]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/99bd80491ba4</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 15:44:29 GMT</pubDate>
            <atom:updated>2019-11-30T05:18:55.795Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 2: Widgets Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello good people! Hope you’re having a wonderful day! Last time I wrote about creating a flutter project, setting up version control and CI.</p><h3>Today I’m writing about</h3><ol><li>Widgets basics</li><li>Widget tree</li><li>Stateless and stateful widgets</li><li>Creating new widgets</li></ol><h3>Umm… Widgets?</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl4JyOwGH26UNQebBK%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl4JyOwGH26UNQebBK%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl4JyOwGH26UNQebBK%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="217" frameborder="0" scrolling="no"><a href="https://medium.com/media/c24a7ce7e9f050c0bcc7c3e5cfbabebe/href">https://medium.com/media/c24a7ce7e9f050c0bcc7c3e5cfbabebe/href</a></iframe><h4>What are widgets?</h4><p><strong>In Flutter everything is a widget.</strong> Basically, the entire app is a widget that’s made up of more widgets. Here’s what the official documentation says:</p><blockquote>Widgets describe what their view should look like given their current configuration and state.</blockquote><h4>The App is actually a widget trees</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F10sMFmeZ0LUM7u%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F10sMFmeZ0LUM7u%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F10sMFmeZ0LUM7u%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="562" frameborder="0" scrolling="no"><a href="https://medium.com/media/374c85106265b75ccb93ab52581f8719/href">https://medium.com/media/374c85106265b75ccb93ab52581f8719/href</a></iframe><p>It’s easier to think of the app as a tree of widgets. The app is the root widget. All the widgets that make up the app is the child of the root. Here’s a photo, I got from google image search, to show you what a widget tree looks like. In fact, this is the widget tree of the default flutter app we got upon project creation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*Tc0kB9YXL4Bj6tonRJlH0w.png" /></figure><h3>Stateless and Stateful Widgets</h3><h4><strong>What in God’s name is a State?</strong></h4><p>State is just <em>data that changes</em> over the life-cycle of the app. Based on this there are two types of widgets in Flutter. A Stateless widget and a stateful widget.</p><h4>Stateless Widgets</h4><p>Widgets that work with immutable (not changing) states.</p><h4>Stateful Widgets</h4><p>Stateful widgets work with mutable (changing) states. When a state changes the stateful widget is re-drawn. When this happens, all the widgets that make this widget will also be re-drawn. Meaning, in the widget tree any widget that’s placed below this widget will be re-drawn. But the widgets above it won’t be re-drawn.</p><p>A stateful widget can be contained in a stateless widget and vise versa.</p><h4>Lemme give an example to clear things up</h4><p>Say we have an app where we show the time as a digital clock. In this case, when our app is running the data that’s constantly changing is <em>“Time”</em>. We need to update the UI to reflect this change in the app UI. Say when we opened the app the time was 9:52:45 AM. And after a second has passed the app UI should show 9:52:46 AM to reflect the change. To to implement this, we use a Stateful Widget. The stateful widget will see if the data (time in this case) has changed and then redraw the widget.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl1IY2dHLln5YPMrM4%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl1IY2dHLln5YPMrM4%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl1IY2dHLln5YPMrM4%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="288" frameborder="0" scrolling="no"><a href="https://medium.com/media/11fa0765faf53819cd2658c9bd23c745/href">https://medium.com/media/11fa0765faf53819cd2658c9bd23c745/href</a></iframe><h4>Optional Reading: Performance Consideration and Best Practices</h4><p><strong>Re-drawing is expensive. </strong>So we should re-draw less amount of widgets and do it as less frequently as possible. We never re-draw everything, only ones which need to be updated. In the previous example, time for instance is changing and we’re assuming the rest of the widgets need not change. <strong>Flutter doc states that it is best practice to push stateful widgets down to the leaf of the widget tree.</strong> Meaning, in the widget tree, we want to place stateful widgets towards the bottom of the widget tree. So that less amount of widgets need to be re-drawn when they change. For example, in the widget tree picture I gave above, if we needed to change the Text then we will make Text a stateful widget so that only it changes, instead of making say Container or Center a stateful widget.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FLG6mzmfXC3V1S%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FLG6mzmfXC3V1S%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FLG6mzmfXC3V1S%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="284" frameborder="0" scrolling="no"><a href="https://medium.com/media/85e51d42fdd93ef271ca794893449762/href">https://medium.com/media/85e51d42fdd93ef271ca794893449762/href</a></iframe><p>If all this seems tough and vague don’t worry. You won’t need it right now. We’ll revisit this topic in State Management again.</p><h3>Coding our first widget</h3><p>OK. Now we shall code. Delete everything in <strong>main.dart</strong> and <strong>widget_test.dart. </strong>We won’t need anything in these files. In fact delete <strong>widget_test.dart</strong> file completely. Now, let’s start coding. We’ll start coding in <strong>main.dart.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e75d1e34b31a772f745d7fcee24f54c8/href">https://medium.com/media/e75d1e34b31a772f745d7fcee24f54c8/href</a></iframe><p>Let me explain the code line by line.</p><p><strong>Line 1</strong> imports another dart file to use it’s contents in this file. The file in particular, <strong>material.dart</strong>, is a very important file. It contains the logic of a lot of prebuilt widgets available with flutter. So remember this file. You’ll need almost always.</p><p><strong>Line 3</strong> is the starting point of our app. The main function is the point where everything starts. And this main function starts our app by calling <strong>runApp()</strong> function by passing it a widget <strong>Leaf</strong>. Leaf is a custom made or user defined widget. We define what it is in <strong>lines 5 to 19.</strong></p><blockquote><strong>Useful tip: </strong>Leaf widget is the root widget of this app. It’s good practice to name the root widget as the name of the app itself. Because my app is called Leaf I named the widget so.</blockquote><p><strong>Line 5</strong> creates our root widget which is called Leaf. Leaf will be stateless widget.</p><p><strong>Line 6</strong> defines the Constructor for Leaf widget class. It takes an optional key parameter.</p><p><strong>Line 9</strong> overrides a function called build. Flutter is basically an engine that paints widgets to the screen. This function is pretty important. It returns a widget that should be painted. In this case we returned a <strong>MaterialApp</strong> widget.</p><p><strong>Line 10 to 18</strong> defines MaterialApp widget. <a href="https://api.flutter.dev/flutter/material/MaterialApp-class.html">Quoting from the official doc for MaterialApp widge</a>t</p><blockquote>An application that uses material design. A convenience widget that wraps a number of widgets that are commonly required for material design applications.</blockquote><p><strong>Line 11 to 14 </strong>defines the theme for our app. Themes are specified using <strong>ThemeData</strong> object. There are a lot of options to specify in it. Play around with the parameters. <a href="https://api.flutter.dev/flutter/material/ThemeData-class.html">Take a look here to know more about it.</a></p><p><strong>Line 16 </strong>defines our home page. This is the widget that’ll be shown upon starting of the app. In our case we will show HomePage widget. This is also a user defined widget that we’ll create now.</p><h3>HomePage widget</h3><p>Start by creating a folder in lib named view. Then create a folder in view called pages. In pages create home_page.dart file. Then write the following code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8388f3b34e7cab015a6ac4c2161f8239/href">https://medium.com/media/8388f3b34e7cab015a6ac4c2161f8239/href</a></iframe><p>Let me go through the code line by line again.</p><p><strong>Line 1</strong> is importing the same file as main.dart. like I said this file is very important and you’re gonna need it pretty frequently.</p><p><strong>Line 3</strong> defines our HomePage widget. And it is a stateless widget.</p><p><strong>Line 8</strong> creates and returns a Scaffold widget. <a href="https://api.flutter.dev/flutter/material/Scaffold-class.html">Quoting the official flutter docs a scaffold is</a></p><blockquote>Implements the basic material design visual layout structure. This class provides APIs for showing drawers, snack bars, and bottom sheets.</blockquote><p>In a sense, a Scaffold is basically a page. You use it to create anything related to a page such as app bars, navigation drawers, side drawers, bottom drawers etc. Here we implement the app bar and body. There are more options we will implement in future posts.</p><h3>Running the app</h3><p>Now let’s run our project. The app should look like the one shown below if everything goes right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*XCTsVzConr1ihUAC-FV6ow.png" /></figure><h3>Summary</h3><p>That’s it for this post. That’s a great start. We just wrote our very first lines of code!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FJE6xHkcUPtYs0%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FJE6xHkcUPtYs0%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FJE6xHkcUPtYs0%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="529" frameborder="0" scrolling="no"><a href="https://medium.com/media/07780cde9c587dfbdec4ba7ea1ece967/href">https://medium.com/media/07780cde9c587dfbdec4ba7ea1ece967/href</a></iframe><p>It wasn’t a whole lot of coding but we did learn (hopefully) the following things:</p><ol><li>Concept of widgets and widget tree</li><li>Stateless and stateful widgets</li></ol><h3>Important links</h3><ol><li><a href="https://flutter.dev/docs/development/ui/widgets-intro">Introduction to widgets</a></li><li><a href="https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html">Stateless</a> and <a href="https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html">Stateful Widgets</a></li><li><a href="https://github.com/Shakleen/Leaf">Project GitHub repository</a></li><li><a href="https://github.com/Shakleen">My GitHub profile</a></li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in GitHub and Medium.</li><li>Starring the GitHub project.</li><li>Starting a discussion regarding this project.</li></ol><p>Thanks for reading! Hope you have a great day. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99bd80491ba4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leaf: Flutter Social Media App — Part 1: Getting started]]></title>
            <link>https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=rss-8757dc4503d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a43673ae78e1</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[education]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Shakleen Ishfar]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 15:42:49 GMT</pubDate>
            <atom:updated>2019-12-01T03:05:10.726Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leaf: Flutter Social Media App — Part 1: Getting started</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*73IgUxPfyXUKZAaIXgutrw.png" /></figure><p>Hello, good people! Hope you’re having a wonderful day! Today I’m going to start building an app using Flutter. I encourage you to follow along with me.</p><h3>Today I’m writing about</h3><ol><li>Setting up the development environment</li><li>Creating a flutter project</li><li>Running the default flutter application</li><li>Setting up source control using Git</li><li>Hosting a public repository in GitHub</li><li>Setting up continuous integration using Travis CI</li></ol><p>Hope you’re excited. It’s going to be an amazing journey. We have a LOT of ground to cover. So, without any further delays, let’s get started!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FVgHQwrT6AE9UzGwVZu%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FVgHQwrT6AE9UzGwVZu%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FVgHQwrT6AE9UzGwVZu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="543" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4fe12f375a2f9a76eeab4c35b7bdc82/href">https://medium.com/media/b4fe12f375a2f9a76eeab4c35b7bdc82/href</a></iframe><h3>Step #1: Environment Setup</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F109fP7pua6Osgw%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F109fP7pua6Osgw%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F109fP7pua6Osgw%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c944e8f044c8ea811dfd0e6c0ccdbbe/href">https://medium.com/media/4c944e8f044c8ea811dfd0e6c0ccdbbe/href</a></iframe><p>First thing’s first. Before we can start building an app using Flutter, we need to setup the development environment. <em>This needs to be done only once</em>. For this we’ll need the following things:</p><ol><li><a href="https://flutter.dev/docs/get-started/install">Flutter SDK</a> (Duh!)</li><li>An IDE (Android Studio) or text editor (VS Code, Sublime, Atom etc. I’ll be using VS Code)</li><li>Extension of the IDE or text editor that’ll make it support flutter.</li><li>Set up an Android device for running the app (emulator or physical device)</li></ol><p>The official flutter doc covers the installation portion rather well. So, I’m not going to write about it again. Follow the <a href="https://flutter.dev/docs/get-started/install">documentation</a> and come back here when you’re all done. (Don’t worry I’m not going to get started without you. I promise to wait right here! :D )</p><p>Once you’ve installed everything try running the following command in your terminal.</p><pre>$ flutter doctor</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kePAouW8jpt8rE9F5S3pNA.png" /></figure><p>If the output look something like the following you’re good to go. Let’s move on to the next step!</p><h3>Project creation</h3><p>Got the environment setup? Nice.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F5GoVLqeAOo6PK%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F5GoVLqeAOo6PK%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F5GoVLqeAOo6PK%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="339" frameborder="0" scrolling="no"><a href="https://medium.com/media/3174f7570f1567cfe7f1b63461f3e5c6/href">https://medium.com/media/3174f7570f1567cfe7f1b63461f3e5c6/href</a></iframe><p>We can now proceed with creating the project. It’s relatively easy to do. I mention 2 ways here. Do either one to create the project.</p><ol><li><strong>VS Code:</strong> Press <strong><em>Ctrl + P</em></strong><em> </em>and then type the following and press enter:</li></ol><pre>&gt; flutter: New Project</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*NH9nQkv-j6pClaPlKij-Mw.png" /></figure><p>Then specify the name of the project and the directory where we want to put it.</p><p>2. <strong>Terminal</strong></p><pre>$ flutter create [app name]<br>$ cd [project directory]</pre><p>The [app name] will be <strong>leaf </strong>for my app. You can choose to name it whatever you want. This is the name your app will have throughout its life.</p><h3>Running the app</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o7ZetIsjtbkgNE1I4%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o7ZetIsjtbkgNE1I4%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3o7ZetIsjtbkgNE1I4%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="284" frameborder="0" scrolling="no"><a href="https://medium.com/media/b52ed2125d22dcac2909e7ca95ec9131/href">https://medium.com/media/b52ed2125d22dcac2909e7ca95ec9131/href</a></iframe><p>By default, flutter creates a counter app that can be run immediately. And we are going to run it, to make sure that there were no problems with the setup or other stuff (like Android SDK problems or device related issues). So, connect a physical device or run it in an emulated device. 2 ways to run the app are:</p><h4><strong>VS Code</strong></h4><p>Press <strong><em>Ctrl + F5</em></strong> or just <strong><em>F5</em></strong> to run the app. Select a device if multiple devices are found.</p><h4><strong>Terminal</strong></h4><pre><strong>$ flutter devices</strong> # Checks to see if there are any devices that can run the app<br><strong>$ flutter run</strong> # Runs the app on the device found</pre><p>Once you do either of the two, your app should be running and ready for use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*Df5X-mzzUEIho4n96frCDw.png" /><figcaption>Flutter Demo App</figcaption></figure><p>If the app doesn’t run and you’re facing some issues then it’s best to google about the issue. Stack Overflow and other sites will have tons of answers to any of the problems at this stage.</p><blockquote><strong>Useful tip:</strong> Get comfortable with googling, finding and browsing through answers on stack overflow and other sites. Believe it or not it’s an extremely useful skill to have. But if you’re really desperate consider commenting here. Others might have solution to the problem you’re facing.</blockquote><h3>Source control using Git</h3><p>So our project runs. Awesome! Now we need a way to manage the source code.</p><h4>Why?</h4><p>Because we’ll be constantly changing the project. So we need a way to maintain different snapshots of the projects. You could manually do this by making a separate copy every time you change something (Don’t ever do this. EVER!) Not only is this inefficient, it’s simply a waste of time and energy. It’s hard to maintain and even harder to search. Just use a source control system. Let it take control.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3oKGz7OdXsWlWZFMIg%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3oKGz7OdXsWlWZFMIg%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3oKGz7OdXsWlWZFMIg%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="285" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ff0661f2d6f1b0b86a41570e3432b64/href">https://medium.com/media/7ff0661f2d6f1b0b86a41570e3432b64/href</a></iframe><p>I’ll be using Git to manage my project and I suggest you do the same. But if you have experience with some other source control system, then by all means, use it. To setup project with git do the following:</p><ol><li><strong>Download and install git</strong> if it isn’t in your computer. <a href="https://git-scm.com/">Get it here!</a></li><li><strong>Setup your user name and email</strong> for git. To do so run the following commands in your terminal.</li></ol><pre>$ git config --global user.name [name]<br>$ git config --global user.email [email]</pre><p>3. Setup git <strong>source control for the project.</strong></p><pre>$ cd [project directory path]<br>$ git init # run while in project directory<br>$ git add . # Basically we’re staging everything in the project<br>$ git commit -m “initial commit” # Saving a snapshot of our project</pre><h3>Repository hosting using GitHub</h3><p>I suggest, using GitHub to host your git repositories. If anything were to happen to your local machine, your project would still be intact and OK.</p><h4>Hosting in GitHub</h4><ol><li><strong>Create an account</strong> in GitHub if you don’t have one already.</li><li><strong>Create a new repository</strong>.</li><li><strong>Copy the repository URL</strong> to add as a remote to your project.</li><li>Go to your project directory in your local machine and <strong>type the following command</strong> to setup the remote with the previously copied URL.</li></ol><pre>$ git remote add [remote name] [repository url]</pre><h3>Travis CI</h3><h4>What’s a CI?</h4><p>CI or Continuous Integration is a system to automate testing and deployment. Travis is one such system.</p><h4>What does Travis do?</h4><p>Travis will run the tests we will write and check if the code in the latest commit passes all these tests. Upon finishing the tests it’ll notify us about the result through mail. We use a CI to save us the trouble of manually running tests.</p><h4>Setting up Travis CI</h4><ol><li><strong>Create an account in Travis</strong> website online.</li><li><strong>Link your GitHub account</strong>.</li><li><strong>Import the repository</strong> created in the for this project.</li><li>In your local machine go to the project. In the main project directory <strong>create a file named <em>.travis.yml</em></strong></li><li>In it <strong>paste the code</strong> below and save it.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c867cdf7e8ba009f8db8e2aa84703d87/href">https://medium.com/media/c867cdf7e8ba009f8db8e2aa84703d87/href</a></iframe><p>I learnt about Travis from <a href="https://medium.com/u/338f470535d3">Yegor Jbanov</a>’s article. <a href="https://medium.com/flutter/test-flutter-apps-on-travis-3fd5142ecd8c">Read it here, if you want to know more.</a></p><h3>Pushing code to GitHub</h3><p>If your Travis CI is setup and connected with your repository run the following command to save your project into GitHub.</p><pre>$ git push -u [remote name] master</pre><p>Once you run this command you will see in your Travis account web page that the tests are being run. As we didn’t change anything on the default project, all the tests should pass and a green tick should be visible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gw-CKl5jKFEHtLYRSLqUeA.png" /><figcaption>Travis CI all tests passed indicator</figcaption></figure><p>If you take a look at your commit history in your GitHub project repository, you’ll also see a green tick there. This also signifies the same thing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*Z5O4UKecr12V4MLBka-7Hg.png" /></figure><p>Now, you should have your project backed up to GitHub. We can now experiment to our hearts content. Cause if we mess up, we can always go back to our previous commits.</p><blockquote><strong>Useful tip:</strong> Source control is an extremely useful tool. Learn how to use it. This series isn’t going to cover basics of source control. But don’t panic! Here are a few helpful articles to get started with Git and GitHub:<br>1.<a href="https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners"> An Intro to Git and GitHub for Beginners</a> by Meghan Nelson<br>2. <a href="https://medium.com/free-code-camp/follow-these-simple-rules-and-youll-become-a-git-and-github-master-e1045057468f">Follow these simple rules and you’ll become a Git and GitHub master</a> by <a href="https://medium.com/u/81ef90d0b982">Ariel Camus</a><br>3. <a href="https://medium.com/free-code-camp/a-developers-introduction-to-github-1034fa55c0db">A developer’s introduction to GitHub</a> by <a href="https://medium.com/u/fe1c14f6cde">Flavio Copes</a><br>Go through them to learn source control using GitHub and Git.</blockquote><h3>Summary</h3><p>That was a lot of stuff we just went through. But for now we’re done.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxXud4DaGlJoRy%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxXud4DaGlJoRy%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FxXud4DaGlJoRy%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="290" frameborder="0" scrolling="no"><a href="https://medium.com/media/aae3188e03bef9cd5c2f33c82b870599/href">https://medium.com/media/aae3188e03bef9cd5c2f33c82b870599/href</a></iframe><p>So whenever you’re starting with a new project you’ll do the following steps:</p><ol><li>Create the project.</li><li>Run the project to see if it works.</li><li>Setup source control.</li><li>Host the repository.</li><li>Setup CI (Optional)</li></ol><p>These are the steps you’re going to be doing for any project you create with flutter, regardless of what type of app it is. This is the foundation to everything that’s going to come afterwards. I know we haven’t coded anything at all. And we haven’t even touched flutter SDK. But this was important! A good foundation is critical to a strong building.</p><p>Thanks for reading. Stay happy. Stay safe. Happy coding! See you in tomorrow’s article.</p><h3>Important links</h3><ol><li><a href="https://github.com/Shakleen">GitHub Profile</a></li><li><a href="https://github.com/Shakleen/Leaf">Leaf GitHub Repository</a></li></ol><h3>Support me by</h3><ol><li>Applauding and sharing this article.</li><li>Following me in <a href="https://github.com/Shakleen">GitHub</a> and <a href="https://medium.com/@shakleenishfar">Medium</a>.</li><li>Starring the <a href="http://www.github.com/Shakleen/Leaf">GitHub project</a>.</li><li>Starting discussions regarding this project.</li></ol><h3>Parts of the series</h3><ol><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-0-954ab180d476?source=friends_link&amp;sk=d1dedb17824adf50c302a91eeb42ba1e">Leaf: Flutter Social Media App — Part 0</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-1-getting-started-a43673ae78e1?source=friends_link&amp;sk=78ea90a3c695faad2c9ca6769f251fbd">Leaf: Flutter Social Media App — Part 1: Getting started</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-2-widgets-99bd80491ba4?source=friends_link&amp;sk=287c81f3432465501db08406b1724e5b">Leaf: Flutter Social Media App — Part 2: Widgets</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-3-widget-tests-a33da9092756?sk=11667b69fef15332c563ae2837fcf754">Leaf: Flutter Social Media App — Part 3: Widget Tests</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-4-creating-news-feed-using-listview-1ed2097df871?sk=8f1957766d90b9f32c39991e14a722af">Leaf: Flutter Social Media App — Part 4: Creating news feed using ListView</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-5-orientation-gesture-and-navigation-6cc4aea4f989?source=friends_link&amp;sk=dbe6743c9a02de8267be02e9e69287a3">Leaf: Flutter Social Media App — Part 5: Orientation, Gestures and Navigation</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-6-models-and-inherited-widgets-to-pass-data-a19c3699a56e?source=friends_link&amp;sk=ddddad171cc9cfc8a645b479b29756af">Leaf: Flutter Social Media App — Part 6: Models and Inherited Widgets to Pass Data</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-7-widget-refactoring-and-testing-revisited-88bc7d131e5b?sk=3864abc14ea1fe17df55c0bc785f3208">Leaf: Flutter Social Media App — Part 7: Widget refactoring and testing revisited</a></li><li><a href="https://medium.com/@shakleenishfar/leaf-flutter-social-media-app-part-8-post-page-and-comments-78eefb8ff447?sk=9d67287d018943e8e3dbc8bc14e91af9">Leaf: Flutter Social Meida App — Part 8: Post Page and Comments</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a43673ae78e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>