<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luiz Eduardo</title>
    <description>The latest articles on DEV Community by Luiz Eduardo (@luizeduardotj).</description>
    <link>https://dev.to/luizeduardotj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F413753%2Fde443dd6-2ed0-4118-98c1-7fcbd88c991e.jpg</url>
      <title>DEV Community: Luiz Eduardo</title>
      <link>https://dev.to/luizeduardotj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luizeduardotj"/>
    <language>en</language>
    <item>
      <title>Search Bar in flutter</title>
      <dc:creator>Luiz Eduardo</dc:creator>
      <pubDate>Tue, 23 Jun 2020 13:46:36 +0000</pubDate>
      <link>https://dev.to/luizeduardotj/search-bar-in-flutter-33e1</link>
      <guid>https://dev.to/luizeduardotj/search-bar-in-flutter-33e1</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/luizeduardotj/searchbarflutterexample"&gt;Link for the code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey devs, today I'll show how to implement a search bar in flutter. This beautiful framework has tools to help us in this and other cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(Don't need any state management)&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Let's see a simple example&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In this example, I going to implement a search field to open a text from a simple text list. The list will be generated.&lt;/p&gt;

&lt;p&gt;Demo: &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---pzcObmD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gfsnwfq2meaxdwe0jj3j.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---pzcObmD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gfsnwfq2meaxdwe0jj3j.gif" alt="Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;What we need?&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;One Stateful widget&lt;/li&gt;
&lt;li&gt;One Scaffold&lt;/li&gt;
&lt;li&gt;One AppBar&lt;/li&gt;
&lt;li&gt;Data to be searched&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just it?&lt;/p&gt;

&lt;p&gt;No. I'll create a class that will manage our search mechanics. First of all, this is our basic struct:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--55cQXj8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ce4p1l0c9px6itevrbs.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--55cQXj8m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ce4p1l0c9px6itevrbs.PNG" alt="Basic struct of exemple"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the next step? Let's call the function &lt;code&gt;showSearch()&lt;/code&gt; in &lt;code&gt;onPressed&lt;/code&gt; parament of &lt;code&gt;IconButton&lt;/code&gt; on &lt;code&gt;actions&lt;/code&gt; of the &lt;code&gt;AppBar&lt;/code&gt;. This function needs two paramete, the first is the context, just pass the BuildContext inherited of Scaffold. The second and the key to this implementation is the delegate, where I need to pass a SearchDelegate that I'll create now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1GxPZD-L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g0pj9h0678hjp3g640ws.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1GxPZD-L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g0pj9h0678hjp3g640ws.PNG" alt="IconButton finished"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The key&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Now, let's create a &lt;a href="https://dart.dev/guides/language/language-tour#extending-a-class"&gt;class inherited&lt;/a&gt; from &lt;code&gt;SearchDelegate&lt;/code&gt; class and do an override of the four main methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXx-CFVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v6io7b8z8atzvzqsa1zv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZXx-CFVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v6io7b8z8atzvzqsa1zv.PNG" alt="SearchDelegate class created"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first method is &lt;code&gt;buildActions()&lt;/code&gt; that return a list of widgets. These widgets are on the right side of the text field. In this example, I'll create a single widget. It'll an IconButton that will clean up our query text.&lt;br&gt;
For this, I created IconButton and on the icon parament I give her the close icon, and in onPressed, I just pass a &lt;code&gt;VoidCallback&lt;/code&gt; that will set the &lt;code&gt;query&lt;/code&gt; (the text of built-in &lt;code&gt;TextField()&lt;/code&gt;) to an empty string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8ReCCF76--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/orids5arzdwvz0lvfwpj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8ReCCF76--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/orids5arzdwvz0lvfwpj.PNG" alt="buildActions method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's implement the &lt;code&gt;buildLeading()&lt;/code&gt;. It's a single widget on the left of the search field. Usually, it's used the &lt;code&gt;arrow_back&lt;/code&gt; icon with &lt;code&gt;Navigator.pop()&lt;/code&gt; in the &lt;code&gt;onPressed&lt;/code&gt; parament.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3XrKstQC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2ib5awh69i1cznelx86q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3XrKstQC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2ib5awh69i1cznelx86q.PNG" alt="buildLeading method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next method to be overridden is &lt;code&gt;buildResult()&lt;/code&gt;. It returns a widget that it'll cover the body of scaffold when the function &lt;code&gt;showResult()&lt;/code&gt; was called. In this simples example, I'll just display a &lt;code&gt;Container&lt;/code&gt; with a &lt;code&gt;Text&lt;/code&gt; in a &lt;code&gt;Center&lt;/code&gt;. For the text, I'll create a string that will receive the selected text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9aIXLmvY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8idy344fzxm8rirlxyoh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9aIXLmvY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8idy344fzxm8rirlxyoh.PNG" alt="buildResults method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last method is &lt;code&gt;buildSuggestion()&lt;/code&gt; that also is a "single widget". In fact, I'll return a &lt;code&gt;ListView.builder()&lt;/code&gt;. And for this, I need a list, that I already have. So, I just will create a &lt;code&gt;final&lt;/code&gt; variable and create a class constructor to pass this list through a parameter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CCzSe4YU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3h7y11nrhsiwgkg6r09r.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CCzSe4YU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3h7y11nrhsiwgkg6r09r.PNG" alt="Class constructor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--svdcnZpY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wdkd860amk37gb7msdr6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--svdcnZpY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wdkd860amk37gb7msdr6.PNG" alt="New call for showSearch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BrlOuv-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6pblf72n7mgg7hgjg4g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BrlOuv-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6pblf72n7mgg7hgjg4g.PNG" alt="The generated list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, in the &lt;code&gt;buildSuggestion()&lt;/code&gt; I'll create an auxiliary list, that will receive different values depending on a condition. In the case of the search field is empty (or &lt;code&gt;query.isEmpty == true&lt;/code&gt;), the auxiliary list will receive a list of recent items searched. In the other case, it'll receive a list of items that contain the text of &lt;code&gt;query&lt;/code&gt;. After, I'll create a &lt;code&gt;ListView.build&lt;/code&gt; that returns the &lt;code&gt;suggestionList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aKjE1KrD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8jkogap58wfhymf13edm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aKjE1KrD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8jkogap58wfhymf13edm.PNG" alt="buildSuggestion method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;onTap&lt;/code&gt; parameter of &lt;code&gt;ListTile&lt;/code&gt;, I'll define the String &lt;code&gt;selectedResult&lt;/code&gt; equal the text pressed and call the function &lt;code&gt;showResults(context)&lt;/code&gt; that will call the &lt;code&gt;buildResult&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hN9RnUTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ivkz8gtddpe678p2fch0.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hN9RnUTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ivkz8gtddpe678p2fch0.PNG" alt="ListView.builder()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now is the time for your developer superpower to take action. I just pass how works the native search mechanic of flutter with a very simple example. You can customize many of these widgets. &lt;/p&gt;

&lt;p&gt;Have a suggestion? Leave a comment!&lt;/p&gt;

</description>
      <category>ios</category>
      <category>dart</category>
      <category>flutter</category>
      <category>android</category>
    </item>
  </channel>
</rss>
