-
Notifications
You must be signed in to change notification settings - Fork 269
feat(authenticator): Add TextEditingController support to form fields #6424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
angelorodem
wants to merge
5
commits into
aws-amplify:main
Choose a base branch
from
angelorodem:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23d0f31
feat: Add AuthenticatorTextFieldController and integrate with form fi…
angelorodem 2a801d5
feat(authenticator): expose optional TextEditingController and improv…
angelorodem 0db4572
feat(authenticator): add AuthenticatorTextFieldController and TextEdi…
angelorodem 7e71fa7
fix(authenticator): defer controller->state updates to post-frame and…
angelorodem f17198a
feat(authenticator): allow SignUp form fields to be disabled or hidden
angelorodem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
packages/authenticator/amplify_authenticator/example/lib/authenticator_with_controllers.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; | ||
| import 'package:amplify_authenticator/amplify_authenticator.dart'; | ||
| import 'package:amplify_flutter/amplify_flutter.dart'; | ||
| import 'package:flutter/material.dart'; | ||
|
|
||
| import 'amplifyconfiguration.dart'; | ||
|
|
||
| /// Example demonstrating the use of TextEditingController with | ||
| /// Amplify Authenticator form fields. | ||
| /// | ||
| /// This allows programmatic control over form field values, enabling | ||
| /// use cases such as: | ||
| /// - Pre-populating fields with data from APIs (e.g., GPS location) | ||
| /// - Auto-filling verification codes from SMS | ||
| /// - Dynamic form validation and manipulation | ||
| class AuthenticatorWithControllers extends StatefulWidget { | ||
| const AuthenticatorWithControllers({super.key}); | ||
|
|
||
| @override | ||
| State<AuthenticatorWithControllers> createState() => | ||
| _AuthenticatorWithControllersState(); | ||
| } | ||
|
|
||
| class _AuthenticatorWithControllersState | ||
| extends State<AuthenticatorWithControllers> { | ||
| // Controllers for programmatic access to form fields | ||
| final _usernameController = AuthenticatorTextFieldController(); | ||
| final _emailController = AuthenticatorTextFieldController(); | ||
| final _addressController = AuthenticatorTextFieldController(); | ||
| final _phoneController = AuthenticatorTextFieldController(); | ||
|
|
||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| _configureAmplify(); | ||
|
|
||
| // Example: Pre-populate fields with default/fetched data | ||
| _usernameController.text = 'amplify_user'; | ||
| _emailController.text = '[email protected]'; | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| // Clean up controllers when the widget is disposed | ||
| _usernameController.dispose(); | ||
| _emailController.dispose(); | ||
| _addressController.dispose(); | ||
| _phoneController.dispose(); | ||
| super.dispose(); | ||
| } | ||
|
|
||
| void _configureAmplify() async { | ||
| final authPlugin = AmplifyAuthCognito( | ||
| // FIXME: In your app, make sure to remove this line and set up | ||
| /// Keychain Sharing in Xcode as described in the docs: | ||
| /// https://docs.amplify.aws/lib/project-setup/platform-setup/q/platform/flutter/#enable-keychain | ||
| secureStorageFactory: AmplifySecureStorage.factoryFrom( | ||
| macOSOptions: | ||
| // ignore: invalid_use_of_visible_for_testing_member | ||
| MacOSSecureStorageOptions(useDataProtection: false), | ||
| ), | ||
| ); | ||
| try { | ||
| await Amplify.addPlugin(authPlugin); | ||
| await Amplify.configure(amplifyconfig); | ||
| safePrint('Successfully configured'); | ||
| } on Exception catch (e) { | ||
| safePrint('Error configuring Amplify: $e'); | ||
| } | ||
| } | ||
|
|
||
| /// Simulates fetching user location and populating the address field | ||
| Future<void> _fetchAndPopulateAddress() async { | ||
| // In a real app, you would use a geolocation service here | ||
| await Future<void>.delayed(const Duration(seconds: 1)); | ||
|
|
||
| // Simulate fetched address | ||
| final fetchedAddress = '123 Main Street, Seattle, WA 98101'; | ||
|
|
||
| // Update the address field programmatically | ||
| _addressController.text = fetchedAddress; | ||
|
|
||
| if (mounted) { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar(content: Text('Address populated: $fetchedAddress')), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Authenticator( | ||
| // Custom sign-up form with controller support | ||
| signUpForm: SignUpForm.custom( | ||
| fields: [ | ||
| // Username field with controller - can be pre-populated or modified | ||
| SignUpFormField.username(controller: _usernameController), | ||
|
|
||
| // Email field with controller | ||
| SignUpFormField.email(controller: _emailController, required: true), | ||
|
|
||
| SignUpFormField.password(), | ||
| SignUpFormField.passwordConfirmation(), | ||
|
|
||
| // Address field with controller - can be populated from GPS/API | ||
| SignUpFormField.address(controller: _addressController), | ||
|
|
||
| // Phone number field with controller | ||
| SignUpFormField.phoneNumber(controller: _phoneController), | ||
| ], | ||
| ), | ||
|
|
||
| child: MaterialApp( | ||
| title: 'Authenticator with Controllers', | ||
| theme: ThemeData.light(useMaterial3: true), | ||
| darkTheme: ThemeData.dark(useMaterial3: true), | ||
| debugShowCheckedModeBanner: false, | ||
| builder: Authenticator.builder(), | ||
| home: Scaffold( | ||
| appBar: AppBar(title: const Text('Controller Example')), | ||
| body: Center( | ||
| child: Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: [ | ||
| const Text( | ||
| 'You are logged in!', | ||
| style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), | ||
| ), | ||
| const SizedBox(height: 20), | ||
|
|
||
| // Display current controller values | ||
| Card( | ||
| margin: const EdgeInsets.all(16), | ||
| child: Padding( | ||
| padding: const EdgeInsets.all(16), | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| const Text( | ||
| 'Form Field Values:', | ||
| style: TextStyle( | ||
| fontSize: 18, | ||
| fontWeight: FontWeight.bold, | ||
| ), | ||
| ), | ||
| const SizedBox(height: 10), | ||
| Text('Username: ${_usernameController.text}'), | ||
| Text('Email: ${_emailController.text}'), | ||
| Text('Address: ${_addressController.text}'), | ||
| Text('Phone: ${_phoneController.text}'), | ||
| ], | ||
| ), | ||
| ), | ||
| ), | ||
|
|
||
| const SizedBox(height: 20), | ||
|
|
||
| ElevatedButton.icon( | ||
| onPressed: _fetchAndPopulateAddress, | ||
| icon: const Icon(Icons.location_on), | ||
| label: const Text('Fetch GPS Address'), | ||
| ), | ||
|
|
||
| const SizedBox(height: 20), | ||
| const SignOutButton(), | ||
| ], | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| void main() { | ||
| runApp(const AuthenticatorWithControllers()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...icator/amplify_authenticator/lib/src/controllers/authenticator_text_field_controller.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| /// Controller for text driven Authenticator form fields. | ||
| /// | ||
| /// Wraps Flutter's [TextEditingController] so developers can opt in to | ||
| /// programmatic control over prebuilt Authenticator fields while keeping | ||
| /// identical semantics to a regular controller. | ||
| class AuthenticatorTextFieldController extends TextEditingController { | ||
| AuthenticatorTextFieldController({super.text}); | ||
|
|
||
| AuthenticatorTextFieldController.fromValue(super.value) : super.fromValue(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.