flutterfire_ui 0.4.1 flutterfire_ui: ^0.4.1 copied to clipboard
UI library built on top of firebase services
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutterfire_ui/auth.dart';
import 'package:flutterfire_ui/i10n.dart';
import 'init.dart'
if (dart.library.html) 'web_init.dart'
if (dart.library.io) 'io_init.dart';
import 'config.dart';
import 'decorations.dart';
final actionCodeSettings = ActionCodeSettings(
url: 'https://reactnativefirebase.page.link',
handleCodeInApp: true,
androidMinimumVersion: '1',
androidPackageName:
'io.flutter.plugins.flutterfire_ui.flutterfire_ui_example',
iOSBundleId: 'io.flutter.plugins.flutterfireui.flutterfireUIExample',
);
final emailLinkProviderConfig = EmailLinkProviderConfiguration(
actionCodeSettings: actionCodeSettings,
);
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFirebase();
FlutterFireUIAuth.configureProviders([
const EmailProviderConfiguration(),
emailLinkProviderConfig,
const PhoneProviderConfiguration(),
const GoogleProviderConfiguration(clientId: GOOGLE_CLIENT_ID),
const AppleProviderConfiguration(),
const FacebookProviderConfiguration(clientId: FACEBOOK_CLIENT_ID),
const TwitterProviderConfiguration(
apiKey: TWITTER_API_KEY,
apiSecretKey: TWITTER_API_SECRET_KEY,
redirectUri: TWITTER_REDIRECT_URI,
),
]);
runApp(FirebaseAuthUIExample());
}
// Overrides a label for en locale
// To add localization for a custom language follow the guide here:
// https://flutter.dev/docs/development/accessibility-and-localization/internationalization#an-alternative-class-for-the-apps-localized-resources
class LabelOverrides extends DefaultLocalizations {
const LabelOverrides();
@override
String get emailInputLabel => 'Enter your email';
}
class FirebaseAuthUIExample extends StatelessWidget {
String get initialRoute {
final auth = FirebaseAuth.instance;
if (auth.currentUser == null) {
return '/';
}
if (!auth.currentUser!.emailVerified && auth.currentUser!.email != null) {
return '/verify-email';
}
return '/profile';
}
@override
Widget build(BuildContext context) {
final buttonStyle = ButtonStyle(
padding: MaterialStateProperty.all(const EdgeInsets.all(12)),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
),
);
return MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
visualDensity: VisualDensity.standard,
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
),
elevatedButtonTheme: ElevatedButtonThemeData(style: buttonStyle),
textButtonTheme: TextButtonThemeData(style: buttonStyle),
outlinedButtonTheme: OutlinedButtonThemeData(style: buttonStyle),
),
initialRoute: initialRoute,
routes: {
'/': (context) {
return SignInScreen(
actions: [
ForgotPasswordAction((context, email) {
Navigator.pushNamed(
context,
'/forgot-password',
arguments: {'email': email},
);
}),
VerifyPhoneAction((context, _) {
Navigator.pushNamed(context, '/phone');
}),
AuthStateChangeAction<SignedIn>((context, state) {
if (!state.user!.emailVerified) {
Navigator.pushNamed(context, '/verify-email');
} else {
Navigator.pushReplacementNamed(context, '/profile');
}
}),
EmailLinkSignInAction((context) {
Navigator.pushReplacementNamed(context, '/email-link-sign-in');
}),
],
styles: const {
EmailFormStyle(signInButtonVariant: ButtonVariant.filled),
},
headerBuilder: headerImage('assets/images/flutterfire_logo.png'),
sideBuilder: sideImage('assets/images/flutterfire_logo.png'),
subtitleBuilder: (context, action) {
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
action == AuthAction.signIn
? 'Welcome to FlutterFire UI! Please sign in to continue.'
: 'Welcome to FlutterFire UI! Please create an account to continue',
),
);
},
footerBuilder: (context, action) {
return Center(
child: Padding(
padding: const EdgeInsets.only(top: 16),
child: Text(
action == AuthAction.signIn
? 'By signing in, you agree to our terms and conditions.'
: 'By registering, you agree to our terms and conditions.',
style: const TextStyle(color: Colors.grey),
),
),
);
},
);
},
'/verify-email': (context) {
return EmailVerificationScreen(
headerBuilder: headerIcon(Icons.verified),
sideBuilder: sideIcon(Icons.verified),
actionCodeSettings: actionCodeSettings,
actions: [
EmailVerified(() {
Navigator.pushReplacementNamed(context, '/profile');
}),
Cancel((context) {
FlutterFireUIAuth.signOut(context: context);
Navigator.pushReplacementNamed(context, '/');
}),
],
);
},
'/phone': (context) {
return PhoneInputScreen(
actions: [
SMSCodeRequestedAction((context, action, flowKey, phone) {
Navigator.of(context).pushReplacementNamed(
'/sms',
arguments: {
'action': action,
'flowKey': flowKey,
'phone': phone,
},
);
}),
],
headerBuilder: headerIcon(Icons.phone),
sideBuilder: sideIcon(Icons.phone),
);
},
'/sms': (context) {
final arguments = ModalRoute.of(context)?.settings.arguments
as Map<String, dynamic>?;
return SMSCodeInputScreen(
actions: [
AuthStateChangeAction<SignedIn>((context, state) {
Navigator.of(context).pushReplacementNamed('/');
})
],
flowKey: arguments?['flowKey'],
action: arguments?['action'],
headerBuilder: headerIcon(Icons.sms_outlined),
sideBuilder: sideIcon(Icons.sms_outlined),
);
},
'/forgot-password': (context) {
final arguments = ModalRoute.of(context)?.settings.arguments
as Map<String, dynamic>?;
return ForgotPasswordScreen(
email: arguments?['email'],
headerMaxExtent: 200,
headerBuilder: headerIcon(Icons.lock),
sideBuilder: sideIcon(Icons.lock),
);
},
'/email-link-sign-in': (context) {
return EmailLinkSignInScreen(
actions: [
AuthStateChangeAction<SignedIn>((context, state) {
Navigator.pushReplacementNamed(context, '/');
}),
],
config: emailLinkProviderConfig,
headerMaxExtent: 200,
headerBuilder: headerIcon(Icons.link),
sideBuilder: sideIcon(Icons.link),
);
},
'/profile': (context) {
return ProfileScreen(
actions: [
SignedOutAction((context) {
Navigator.pushReplacementNamed(context, '/');
}),
],
actionCodeSettings: actionCodeSettings,
);
},
},
title: 'Firebase UI demo',
debugShowCheckedModeBanner: false,
locale: const Locale('en'),
localizationsDelegates: [
FlutterFireUILocalizations.withDefaultOverrides(const LabelOverrides()),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
FlutterFireUILocalizations.delegate,
],
);
}
}