<?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 Danvick Miller on Medium]]></title>
        <description><![CDATA[Stories by Danvick Miller on Medium]]></description>
        <link>https://medium.com/@danvickmiller?source=rss-147bde7a6199------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BYyB0l3RcptSg3qVYn0WvQ.png</url>
            <title>Stories by Danvick Miller on Medium</title>
            <link>https://medium.com/@danvickmiller?source=rss-147bde7a6199------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:26:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danvickmiller/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[Building a Custom Field with FormBuilder Flutter Package]]></title>
            <link>https://medium.com/@danvickmiller/building-a-custom-flutter-form-builder-field-c67e2b2a27f4?source=rss-147bde7a6199------2</link>
            <guid isPermaLink="false">https://medium.com/p/c67e2b2a27f4</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutterformbuilder]]></category>
            <category><![CDATA[flutterformbuilderfield]]></category>
            <dc:creator><![CDATA[Danvick Miller]]></dc:creator>
            <pubDate>Sat, 23 Apr 2022 18:06:19 GMT</pubDate>
            <atom:updated>2022-11-21T18:10:00.434Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ajl6saf1g73TQtxSLd20EA.png" /></figure><h3><strong>Introduction</strong></h3><p><a href="https://pub.dev/packages/flutter_form_builder">flutter_form_builder</a> is a Flutter package that simplifies user input by eliminating most of the boilerplate needed to build a form, validate fields, react to changes, and collect the final form input.</p><p>The package provides some of the most common user input fields that you could need — including but not limited to TextField, CheckBox, Switch, Slider, RangeSlider, DropDown, DateTimePicker, etc — out-of-box.</p><p>There are, however, so many other widgets you may want to use as input components within your applications’ forms — some provided by Flutter, others by packages on <a href="https://pub.dev/">Pub.dev</a> from the Flutter community or even your own custom input widgets.</p><p>In this article, we will explore how you could use any user input widget as a custom field within <strong><em>FormBuilder</em></strong>.</p><h3>Return your widget from FormBuilderField’s builder</h3><p>First, for you to use any widget as an input field within your <strong><em>FormBuilder</em></strong>, make sure that the input widget has a value-change callback method such as an <strong><em>onChanged</em></strong>, <strong><em>onSelected,</em></strong> etc. Such a method would be helpful to notify the form that the input value for the field has changed.</p><p><a href="https://pub.dev/documentation/flutter_form_builder/latest/flutter_form_builder/FormBuilderField-class.html">FormBuilderField</a> — which is the base class of all FormBuilder fields — maintains the current state of the form field and handles the input field’s interaction with the encompassing FormBuilder widget. The <strong><em>FormBuilderField</em></strong> widget provides a <strong><em>builder</em></strong> method within which you will return your widget.</p><p>Now, let’s assume we need to use a <strong><em>CupertinoTextField</em></strong> instead of the standard provided <strong><em>FormBuilderTextField</em></strong> widget (based on Material Design’s TextField) and also make a FormBuilder input field out of a CheckboxListTile widget.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4e45fe249f910334a9621739d74bc95/href">https://medium.com/media/f4e45fe249f910334a9621739d74bc95/href</a></iframe><p>In the above code snippet, we returned the widgets we intend to convert to FormBuilder input fields from the <strong><em>builder</em></strong> method of <strong><em>FormBuilderField</em></strong>. Note that we listen to the changes in the CupertinoTextField and CheckBoxListTile then pass the new values along to the FormBuilderField using the <strong><em>didChange()</em></strong> method.</p><figure><img alt="Custom flutter_form_builder fields without styling" src="https://cdn-images-1.medium.com/max/750/1*iIgcA8JjlX_Tuj380zeSYA.png" /><figcaption>Custom flutter_form_builder fields without styling</figcaption></figure><p>At this point, everything should be working fine, and FormBuilder should be able to handle the input from the field. The issue now is that our field doesn’t look much like a regular form field — it doesn’t have a label, hints, or helpers, but most importantly — there is no place to display validation errors if form validation fails.</p><h3>Make the input look more like a field</h3><p>Flutter’s Material Design provides an <a href="https://api.flutter.dev/flutter/material/InputDecorator-class.html">InputDecorator</a> widget that displays the visual elements of a Material Design field around its child component.</p><p>When using the Cupertino Design system, you can use <a href="https://api.flutter.dev/flutter/cupertino/CupertinoFormRow-class.html">CupertinoFormRow</a>, which creates an iOS-style split form row with a standard <em>prefix</em>, a <em>child </em>input<em> </em>widget, and space for <em>error</em> and <em>helper</em> widgets that appear underneath.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32851331ebf7680080878cd511fb2618/href">https://medium.com/media/32851331ebf7680080878cd511fb2618/href</a></iframe><p>In the above code, we’ve wrapped the <strong><em>CupertinoTextField</em></strong> with a <strong><em>CupertinoFormRow</em></strong><em> </em>and the <strong><em>CheckboxListTile </em></strong>with an<strong><em> InputDecorator </em></strong>respectively<strong><em>, </em></strong>both of<strong><em> </em></strong>which allow us to display the field’s <strong><em>errorText</em></strong> and the fields’ label. Note that the <strong><em>FormFieldState</em></strong>, passed into the builder method supplies the <strong><em>errorText </em></strong>to be displayed if validation fails.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*U5B2eJY9NNJQDy5rwGwVyA.png" /></figure><p>As illustrated in the image above the <em>CupertinoTextField</em> and <em>CheckboxListTile</em> are now styled with Cupertino and Material design styles respectively; and if there are errors in the fields, they are displayed on the screen.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c67e2b2a27f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Turn any Flutter Widget into a Form Input]]></title>
            <link>https://medium.com/@danvickmiller/turn-any-flutter-widget-into-a-form-input-c23223042e3b?source=rss-147bde7a6199------2</link>
            <guid isPermaLink="false">https://medium.com/p/c23223042e3b</guid>
            <category><![CDATA[formfield]]></category>
            <category><![CDATA[flutter-form]]></category>
            <category><![CDATA[form-validation]]></category>
            <dc:creator><![CDATA[Danvick Miller]]></dc:creator>
            <pubDate>Sat, 05 Sep 2020 19:50:04 GMT</pubDate>
            <atom:updated>2020-09-05T19:59:21.180Z</atom:updated>
            <content:encoded><![CDATA[<p>Getting started with forms in Flutter is not really that hard. It’s as easy as having one or more <em>FormField</em> widgets or widgets that extend the <em>FormField</em> class — all contained in a <em>Form</em> widget.</p><h4>FormField</h4><p>A <a href="https://api.flutter.dev/flutter/widgets/FormField-class.html"><em>FormField</em></a> is a widget that maintains the current state of the field contained within it, so that updates and validation errors are visually reflected in the UI. Having a <em>Form</em> widget as part of a <em>FormField</em>’s ancestry is not really necessary. However, having a <em>Form</em> widget as an ancestor of several <em>FormField</em> enables you to query and manipulate all the data fields as a single unit — just as you would in a HTML form.</p><p>Flutter does not have so many built-in Form Inputs that extend FormField class. As at the time of writing, we only have <em>TextFormField</em> and (a recently added) <em>DropdownFormField</em>.</p><p>But we don’t need Flutter to build every possible form input, especially since the possibilities are endless and we have the <em>FormField</em> Widget which you can use to turn almost any widget into a form input.</p><p>Well, I may have oversold you on the title a bit, the <em>FormField</em> widget can transform any widget <strong>that has a changing value and preferably accepts user input </strong>into a form control<strong>. </strong>You can therefore return any widget with a changing value in <em>FormField</em>’s <em>builder</em> function and call <em>didChange() </em>function when the value of the widget changes to propagate the change to the <em>FormField</em>.</p><p>Let’s take the example for a SwitchListTile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9450ada21a54cacfe49f5224f2618d02/href">https://medium.com/media/9450ada21a54cacfe49f5224f2618d02/href</a></iframe><p>In the above example, we have wrapped our <em>SwitchListTile</em> inside a <em>FormField</em> widget. Notice that the Switch’s <em>value</em> attribute will now take the value of the <em>FormField’s </em>state passed in with the <em>field</em> parameter on the <em>builder</em> and that the <em>onChanged() </em>callback will update the value of the <em>FormField</em>’s state using <em>FormFieldState.didChange()</em> function instead of manipulating the Switch value directly.</p><p>Now you may be asking yourself where’s the advantage there, well first of all hold your horses!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*HHvzhVyqyQyH2t4QFYg05g.jpeg" /></figure><p>Okay then, the advantage comes when we have multiple fields in our <em>Form</em> and we need to save or validate them at once. So now we are going to add more fields and try to validate the form:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6db6da057b217e950b44a16093b9c43/href">https://medium.com/media/a6db6da057b217e950b44a16093b9c43/href</a></iframe><p>Now we have a <em>TextFormField</em> and our original <em>SwitchListTile</em>, both of which have <em>validator</em> attributes that for the <em>TextFormField</em> ensures that the user enters a string value and for the <em>SwitchListTile</em> we want to ensure that the user subscribes to our mailing list. The validator attribute in a <em>FormField</em> takes in a <a href="https://api.flutter.dev/flutter/widgets/FormFieldValidator.html"><em>FormFieldValidator</em>,</a> which is a function used to evaluate the value of a FormField against some predefined condidions and return a string describing the input error if any, and <em>null</em> if the value is valid.</p><p>One thing to note in this code snippet is that the <em>TextFormField</em> does not have a <em>FormField</em> widget wrapped around it; this is because it already extends the FormField class. A TextFormField widget is simply just a <em>TextField</em> widget that already conveniently extends <em>FormField</em> widget and hence comes with all the functionality of the <em>FormField </em>class.</p><p>On running the above code — then immediately clicking the Submit button without adding any input — you’ll notice the <em>TextFormField</em>’s error text showing just below the input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2SZUmzczTQFT_QIn5N8Ckg.png" /></figure><p>But where’s the error text for the <em>SwitchListTile</em>? Does this mean that it’s valid? Well, remember that we said if the value of the Switch is not <em>true</em> (<em>i.e. the switch is not ON</em>) — which it’s clearly not, then validation of the field should fail. But why is the error not shown to the user?</p><h4>InputDecorator</h4><p>That’s because there’s nowhere to display the error in our custom field. Lucky for us, <em>FormFieldState</em> comes with a convenient <em>FormFieldState.errorText </em>property. In essence we could display the <em>errorText</em> anywhere on the screen with a <em>Text</em> widget, but the most convenient way is to wrap our SwitchListTile with an <em>InputDecorator</em> widget</p><p>An <a href="https://api.flutter.dev/flutter/material/InputDecorator-class.html"><em>InputDecorator</em></a><em> </em>is a widget that displays the visual elements of a Material Design field around its input <a href="https://api.flutter.dev/flutter/material/InputDecorator/child.html"><em>child</em></a><em> — </em>these visual effects may include an <a href="https://api.flutter.dev/flutter/material/InputDecoration-class.html"><em>InputDecoration</em></a><em> </em>object which can be used to define the field’s error text, label, hints, prefix and suffix icons just to name a few. Let’s see how we can use it to enhance the functionality as well as the looks of our form.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29d97796569aabc248348fae7d51d665/href">https://medium.com/media/29d97796569aabc248348fae7d51d665/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tAD165p6bALd8GSbGMcQLQ.png" /></figure><h4>Getting the input values from the form</h4><p>Now that we’ve been able to create a ‘complete’ form how do we then get the values from the different input controls for processing?</p><p>The easiest way is to use Flutter’s <a href="https://api.flutter.dev/flutter/foundation/Key-class.html"><em>Key</em>s</a>. You may have noticed that for the <em>Form</em> widget we used a <em>GlobalKey</em> to get its current state and call the validation function.</p><p>Similarly, for the inputs, we can define keys for each of the fields, attach the keys to the respective inputs and then use them to get a hold of the values after they have been submitted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7e15e2ac67f4524778fd177da76e8b4/href">https://medium.com/media/c7e15e2ac67f4524778fd177da76e8b4/href</a></iframe><p>In the above snippet, you’ll note that the the <em>Form</em> widget’s <em>GlobalKey</em> is of type <a href="https://api.flutter.dev/flutter/widgets/FormState-class.html"><em>FormState</em></a> while the form input keys are of type <a href="https://api.flutter.dev/flutter/widgets/FormFieldState-class.html"><em>FormFieldState</em></a>.</p><p>Both the <em>FormState</em> and <em>FormFieldState</em> classes have a <strong><em>validate()</em></strong> function and a <strong><em>save()</em></strong> function. The <em>validate()</em> function is used to validate the widget’s user input to make sure it’s valid according to the rules defined by the <em>validator</em> callback in the <em>FormField</em>(s) — and returns a boolean value indicating whether the validation was successful or not; while the <em>save()</em> function is used to commit the user-entered input to the widgets’ state. The <em>Form</em>’s <em>save()</em> and <em>validate()</em> functions will affect each <em>FormField</em> in within it by in turn calling their corresponding <em>save()</em> or <em>validate()</em> functions.</p><p>The <em>FormFieldState</em> class also contains an extra property named <strong><em>value </em></strong>which will return the saved value of the corresponding FormField.</p><p>We, therefore, within the Submit button — are calling the <em>FormState</em>’s <em>validate()</em> function that will validate each field and return <em>true</em> if all fields have valid inputs then call <em>save()</em> to commit the values of each field. And since we don’t have much to do with the inputs we’ll just be printing them to console; this is where you would say, store the values or send them to your backend service for processing.</p><h4><strong>Summary</strong></h4><p>So, in summary, to convert a widget into a form input, we need to first have a widget that has a changing value that accepts user input, wrap it inside a <em>FormField</em> and react to changes in user input by calling the <em>FormFieldState</em>’s <em>didChange()</em> method with the new value to propagate it for the <em>FormField</em>.</p><p>It also helps to wrap the widget inside an <em>InputDecorator</em> widget with which we can show the field’s <em>errorText</em>, <em>labelText</em>, <em>hintText</em> etc.</p><p>Finally to get the value of the field, we’d need to call the <em>save()</em> function of the field (or the whole <em>Form</em> widget if present) and get the values using <em>Key</em>s.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c23223042e3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>