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'; class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { @override void initState() { super.initState(); _configureAmplify(); } void _configureAmplify() async { try { await Amplify.addPlugin(AmplifyAuthCognito()); await Amplify.configure(amplifyconfig); safePrint('Successfully configured'); } on Exception catch (e) { safePrint('Error configuring Amplify: $e'); } } @override Widget build(BuildContext context) { return Authenticator( // `authenticatorBuilder` is used to customize the UI for one or more steps authenticatorBuilder: (BuildContext context, AuthenticatorState state) { switch (state.currentStep) { case AuthenticatorStep.signIn: return CustomScaffold( state: state, // A prebuilt Sign In form from amplify_authenticator body: SignInForm(), // A custom footer with a button to take the user to sign up footer: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('Don\'t have an account?'), TextButton( onPressed: () => state.changeStep( AuthenticatorStep.signUp, ), child: const Text('Sign Up'), ), ], ), ); case AuthenticatorStep.signUp: return CustomScaffold( state: state, // A prebuilt Sign Up form from amplify_authenticator body: SignUpForm(), // A custom footer with a button to take the user to sign in footer: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('Already have an account?'), TextButton( onPressed: () => state.changeStep( AuthenticatorStep.signIn, ), child: const Text('Sign In'), ), ], ), ); case AuthenticatorStep.confirmSignUp: return CustomScaffold( state: state, // A prebuilt Confirm Sign Up form from amplify_authenticator body: ConfirmSignUpForm(), ); case AuthenticatorStep.resetPassword: return CustomScaffold( state: state, // A prebuilt Reset Password form from amplify_authenticator body: ResetPasswordForm(), ); case AuthenticatorStep.confirmResetPassword: return CustomScaffold( state: state, // A prebuilt Confirm Reset Password form from amplify_authenticator body: const ConfirmResetPasswordForm(), ); default: // Returning null defaults to the prebuilt authenticator for all other steps return null; } }, child: MaterialApp( builder: Authenticator.builder(), home: const Scaffold( body: Center( child: Text('You are logged in!'), ), ), ), ); } } /// A widget that displays a logo, a body, and an optional footer. class CustomScaffold extends StatelessWidget { const CustomScaffold({ super.key, required this.state, required this.body, this.footer, }); final AuthenticatorState state; final Widget body; final Widget? footer; @override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: const EdgeInsets.all(16), child: SingleChildScrollView( child: Column( children: [ // App logo const Padding( padding: EdgeInsets.only(top: 32), child: Center(child: FlutterLogo(size: 100)), ), Container( constraints: const BoxConstraints(maxWidth: 600), child: body, ), ], ), ), ), persistentFooterButtons: footer != null ? [footer!] : null, ); } }