/flutter_autofill

Flutter plugin providing Android Autofill support for text fields.

Primary LanguageJavaBSD 2-Clause "Simplified" LicenseBSD-2-Clause

flutter_autofill

Provides Android Autofill support for Flutter text fields.

Supports all Android hint types.

Add dependency

dependencies:
  flutter_autofill: ^0.4.1

Usage

At its simplest form, you can just wrap your TextField with Autofill widget:

import 'package:flutter_autofill/flutter_autofill.dart';
          Autofill(
                onAutofilled: (val) {
                   // set value in controller & cursor position after auto-filled value
                  _emailController.value = TextEditingValue(text: val, selection: TextSelection.fromPosition(TextPosition(offset: val.length)));
                },
                autofillHints: [FlutterAutofill.AUTOFILL_HINT_EMAIL_ADDRESS],
                autofillType: FlutterAutofill.AUTOFILL_TYPE_TEXT,
                textController: _emailController,
                child: TextField(
                  controller: _emailController,
                  decoration: InputDecoration(hintText: "Please enter your email"),
                ),
              ),

For Autofill to function properly and be available on subsequent visits of screen, you must notify Autofill when data has been submitted or cancelled.

  bool commited = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Autofill Widget screen'),
      ),
      body: WillPopScope(
        onWillPop: () async {
          if (!commited) {
            await FlutterAutofill.cancel();
          }
          return true;
        },
        child: ...
  void _onSubmit() async {
    await FlutterAutofill.commit();
    commited = true;
    ...
  }