• Docs
  • Showcase
  • Community
  • DMCA
Friday, February 26, 2021
Flutter Website
No Result
View All Result
  • Login
  • Register
Flutter Website
  • Home
  • Categories
    • Flutter App
    • Flutter Examples
    • Flutter Github
    • Flutter News
    • Flutter PDF
    • Flutter Tips & Trick
    • Flutter Tutorial
    • Flutter UI
    • Flutter Video
    • Flutter VS
    • Flutter Wiki
    • Flutter With
  • Flutter
    • Flutter for Mobile
    • Flutter for Web
      • Widget Sample
    • Flutter for Desktop
    • Tools
      • Codemagic
      • Flutter Studio
      • Supernova
  • Showcase
  • Community
  • Advertisement
  • Hire Us
No Result
View All Result
Flutter Website
Home Flutter Tutorial

Level up your Flutter apps with autofill

By TruongSinh Tran-Nguyen

flutter by flutter
Reading Time: 15min read
452
425
SHARES
607
VIEWS
Share on FacebookShare on TwitterShare on LinkedinShare to Whatsapp

Table of Contents

  • What/Why autofill?
  • Get started with a form with NO autofill
  • Install Android Autofill Framework sample
  • First autofill popup in our Flutter app
  • AutofillGroup
  • Add autofill capability to the rest of our app
  • Nested AutofillGroup
  • Some gotchas to keep in mind
  • Conclusion

It’s official, Flutter 1.20 is here. Among all the goodies and improvements, mobile autofill gets highlighted in the tagline, and even has a gif demonstrating how slick the UX is. Unfortunately, there are not yet many examples, tutorials, or gotchas available, hence the born of this article.

What/Why autofill?

Image for post

Autofill in action, credit Flutter team

From Android Developer Guide:

Filling out forms is a time-consuming and error-prone task. Users can easily get frustrated with apps that require such actions. The autofill framework improves the user experience by providing the following benefits:

Less time spent in filling fields. Autofill saves users from re-typing information.

Minimize user input errors. Typing is prone to errors, especially on mobile devices. Minimizing the need to type information also minimizes typos.

Get started with a form with NO autofill

We will start with a simple form with addresses, a phone number, and a field that should not be autofilled later.

Image for post

The code is available on this repo. If you focus on any of these fields, you should see the cursor blinking, but no autofill popup.

Install Android Autofill Framework sample

You might already have some autofill services (like LastPass, Bitwarden) or you might not. Either way, it’s recommended that you don’t use them during development, but use a reference implementation of the service instead. Follow the instruction from step 1 of this CodeLabs to have the Android Autofill Framework sample setup on your device.

Image for post

After the sample is installed, choose the Debug Autofill Service . Now, go to any app written using OEM widgets (such as those written with Android SDK, React Native, or Ionic, but not Flutter nor Unity), focus on any text field, you will see the autofill service appears undesirably. Generally, we want our app to be autofill-able in certain fields only, such as name, address, credit card, password, etc, but not others, such as note, search, etc.

Image for post

Unexpected autofill popup never happens in Flutter apps

The reason autofill service appears somehow unexpectedly is that Android apps utilizing OEM widgets can infer autofill semantics from its widget ID if the developers do not explicitly code so. The inference works most of the time, but not always.

Image for post

Android XML can infer autofill semantics from widget ID. Screenshot from Quick Ways to Ensure App Compatibility with Android Autofill, 2:11

Unlike Android XML, your Flutter app does not automatically have autofill capability, even if you recompile your codebase against Flutter 1.20. In fact, there’s no equivalent of android:importantForAutofill tag at all. The reason is that, while in Android, an element can have android:id tag, it’s not the case with Flutter.

Image for post

There is no android:importantForAutofill equivalent in Flutter. Screenshot from Quick Ways to Ensure App Compatibility with Android Autofill, 4:03

First autofill popup in our Flutter app

The first step is quite easy, all of the widgets composed of EditableText or its ancestors, such as TextFormField or TextField have autofillHints parameter that we can set.

Even though this param accepts a list (to be exact, iterable) of string, it’s recommended that you don’t pass in an arbitrary string, but make use of static values from AutofillHints, a collection of commonly used autofill hints. It will help you avoid typos, and also ensure cross-platform compatibility.

Image for post

AutofillHints, a collection of commonly used autofill hint, helps avoid typo and ensures cross-platform compatibility.

Now, go back to our app and focus on the “Shipping address 1”, we’ll see autofill popup appears. Let’s tap on that, and do the same for “Shipping address 2”.

Image for post

First autofill popup in our Flutter app, yay!

AutofillGroup

So far, to fill those 2 lines of address, the user has to tap on the autofill popup twice. It’s more laborious than ideal, and also more error-prone. If the user, say, have more than one address, for example:

11 Wall St
New York, NY 10005

and

1600 Pennsylvania Avenue NW
Washington, DC 20500

there’s a chance the user mistakenly choose different entries for different fields, causing corrupted data:

11 Wall St
Washington, DC 20500

AutofillGroup can be used in this situation so that the user tap only once, and the service will autofill all related fields that are children of the nearest AutofillGroup in the widget tree.

Add autofill capability to the rest of our app

This step is quite straight-forward, and is similar to previous ones.

Nested AutofillGroup

At this point, we can be quite happy with our app. However, the phone number is usually filled together with the shipping address. In other words, the phone number field should be in the same a AutofillGroup with address line 1 and address line 2. But, in our widget, it’s not quite easy, because, in the middle, we have the billing address fields as well.

At this point, I was about to use GlobalKey to get AutofillGroupState, but it turned out that there’s a simpler way. AutofillGroup can be nested, and the descendant fields will only be grouped together under the nearest AutofillGroup widget in the widget tree.

We can see that credit card fields are still grouped correctly, even though it’s nested inside another AutofillGroup meant to be for the shipping address and phone number.

Some gotchas to keep in mind

In the talk Quick Ways to Ensure App Compatibility with Android Autofill there was a warning that an anti-pattern in Android SDK, view hierarchy is created onStart() life cycle, will break the UX of autofill by making the autofill service loopingly asking the user authentication. Luckily, we cannot have it in Flutter, as Flutter is (semi-)declarative, and it’s impossible to imperatively create/paint view on life cycle events.

However, be careful with the nested AutofillGroup in step 4 above, if you have conditionally build widgets, and in our case, if (!isSameAddress). Try autofill the credit card field, and then uncheck the box, we can see the value of the credit card fields gets moved to the billing address fields.

Image for post

The solution is to use UniqueKey. The explanation deserves a whole article, [Flutter] Keys! What are they good for?, but the short answer is that due to the way Flutter framework do the tree-diffing for the rebuild, it cannot reliably distinguish widgets of the same type (in this case, TextFormField) without extra help from key.

Conclusion

As mentioned in the release announcement, autofill has been one of the #1 most requested Flutter features for a while, and we finally get it, for both Android and iOS (web is on the way, and we don’t have autofill ecosystem on desktop yet). It turns out that, unlike on Android, the developers have to explicitly modify their code to support autofill. Besides, gotchas with fields and autofill in Flutter are quite different from those in Android.

The example code and diff used in this article is available at

https://github.com/truongsinh/flutter-form-autofill.

https://www.twitter.com/FlutterComm

https://www.twitter.com/FlutterComm

Tags: Medium

Related Posts

Flutter ui tip 3: popup card
Flutter Tutorial

Flutter UI Tip 3: Popup Card

515
[4k] pbo dart 22. Operator overriding
Erico Darmawan

[4K] PBO DART 22. Operator Overriding

515
Built-time vs  run-time #shorts
Flutter Explained

Built-Time vs Run-Time #Shorts

517
Staggered animations made simple – flutter simple animations tutorial
Flutter Tutorial

Staggered Animations Made Simple – Flutter Simple Animations Tutorial

523
#askflutter at flutter engage
Flutter Official

#AskFlutter at Flutter Engage

520
Spread operator & null-aware operator on lists, maps and sets in dart
Flutter Explained

Spread operator & Null-aware operator on Lists, Maps and Sets in Dart

518
Next Post

Movie Ticketing App Animation Using Flutter

Flutter animation tutorial #9 – animated lists

Flutter Animation Tutorial #9 - Animated Lists

Flutter animation tutorial #10 – stagger list animations

Flutter Animation Tutorial #10 - Stagger List Animations

  • Flutter & dart – the complete guide [2020 edition]

    Flutter & Dart – The Complete Guide [2020 Edition]

    2267 shares
    Share 907 Tweet 567
  • The Complete 2020 Flutter Development Bootcamp with Dart

    1989 shares
    Share 796 Tweet 497
  • Flutter & Firebase: Build a Complete App for iOS & Android

    1413 shares
    Share 565 Tweet 353
  • Flutter Bloc & Cubit Tutorial

    1285 shares
    Share 514 Tweet 321
  • 40 Beautiful Flutter UI Themes For Developers

    1065 shares
    Share 426 Tweet 266

Made by Google

Flutter is Google’s portable UI toolkit for building beautiful, natively-compiled applications for mobile, web, and desktop from a single codebase.

Follow us

Recent Post

  • Unsplash iOS app – UI ideas for flutter
  • Flutter UI Tip 3: Popup Card
  • Quill marketing site – UI ideas for flutter
  • [4K] PBO DART 22. Operator Overriding
  • Microsoft Outlook iOS app – UI ideas for flutter

Popular Post

Snackbar (flutter widget of the week)

SnackBar (Flutter Widget of the Week)

532
Make your first simple android app with kotlin (android kotlin tutorial for beginners)

Make Your First Simple Android App with Kotlin (Android Kotlin Tutorial for Beginners)

524

Review Post

Flutter themeswitcher template in flutter

Flutter ThemeSwitcher Template in Flutter

Congratulations, Nice Work, GLWS $7
Rosen – flutter ecommerce ui

Rosen - Flutter Ecommerce UI

Nice Product I am gonna love it. $18
  • [email protected]
  • Flutter Terms
  • Flutter Packages
  • Dart

Copyright © 2021 Flutter Website - by Flutter Team.

No Result
View All Result
  • Home
  • Categories
    • Flutter App
    • Flutter Examples
    • Flutter Github
    • Flutter News
    • Flutter PDF
    • Flutter Tips & Trick
    • Flutter Tutorial
    • Flutter UI
    • Flutter Video
    • Flutter VS
    • Flutter Wiki
    • Flutter With
  • Flutter
    • Flutter for Mobile
    • Flutter for Web
      • Widget Sample
    • Flutter for Desktop
    • Tools
      • Codemagic
      • Flutter Studio
      • Supernova
  • Showcase
  • Community
  • Advertisement
  • Hire Us
  • Login
  • Sign Up

Copyright © 2021 Flutter Website - by Flutter Team.

Welcome Back!

Sign In with Facebook
Sign In with Google
OR

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Sign Up with Facebook
Sign Up with Google
OR

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.
Go to mobile version