Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Created April 30, 2026 18:22
Show Gist options
  • Select an option

  • Save loic-sharma/3c4e67d3fab6c6f162f18501c5a15fd9 to your computer and use it in GitHub Desktop.

Select an option

Save loic-sharma/3c4e67d3fab6c6f162f18501c5a15fd9 to your computer and use it in GitHub Desktop.
Autofill test app
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MaterialApp(home: RegisterScreen()));
class RegisterScreen extends StatelessWidget {
const RegisterScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Register')),
body: AutofillGroup(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
autofillHints: const [AutofillHints.email],
decoration: const InputDecoration(labelText: 'Email'),
),
PasswordField(newPassword: true),
const SizedBox(height: 20),
ElevatedAction(
label: "Register & Save",
onPressed: () {
// Triggers the OS to 'remember' the credentials
TextInput.finishAutofillContext();
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const LoginScreen()),
);
},
),
],
),
),
),
);
}
}
class LoginScreen extends StatelessWidget {
const LoginScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Login")),
body: AutofillGroup(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
autofillHints: const [AutofillHints.email],
decoration: const InputDecoration(labelText: 'Email'),
),
PasswordField(newPassword: false),
const SizedBox(height: 20),
ElevatedAction(
label: 'Login',
onPressed: () => TextInput.finishAutofillContext(),
),
],
),
),
),
);
}
}
class PasswordField extends StatefulWidget {
const PasswordField({super.key, required this.newPassword});
final bool newPassword;
@override
State<PasswordField> createState() => _PasswordFieldState();
}
class _PasswordFieldState extends State<PasswordField> {
bool _obscureText = true;
@override
Widget build(BuildContext context) {
return TextField(
autofillHints: widget.newPassword ? const [AutofillHints.newPassword] : const [AutofillHints.password],
obscureText: _obscureText,
decoration: InputDecoration(
labelText: 'Password',
suffixIcon: IconButton(
icon: Icon(_obscureText ? Icons.visibility_off : Icons.visibility),
onPressed: () => setState(() => _obscureText = !_obscureText),
),
),
);
}
}
class ElevatedAction extends StatelessWidget {
final String label;
final VoidCallback onPressed;
const ElevatedAction({super.key, required this.label, required this.onPressed});
@override
Widget build(BuildContext context) => ElevatedButton(onPressed: onPressed, child: Text(label));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment