Untitled
unknown
dart
3 years ago
4.3 kB
5
Indexable
import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'firebase_options.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Firebase Auth App', theme: ThemeData.light().copyWith( colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.purple) .copyWith(secondary: Colors.purple)), home: BlocProvider<AuthBloc>( create: (context) => AuthBloc(), child: LoginScreen(), ), ); } } // class MainPage extends StatelessWidget { // const MainPage({super.key}); // @override // Widget build(BuildContext context) => Scaffold( // body: StreamBuilder<User?>( // stream: FirebaseAuth.instance.authStateChanges(), // builder: (context, snapshot) { // if (snapshot.connectionState == ConnectionState.waiting) { // return const Center(child: CircularProgressIndicator()); // } else if (snapshot.hasError) { // return const Center(child: Text('Algo deu errado :(')); // } else if (snapshot.hasData) { // return const HomePage(); // } else { // return const AuthPage(); // } // }, // ), // ); // } class LoginScreen extends StatelessWidget { final _emailController = TextEditingController(); final _passwordController = TextEditingController(); LoginScreen({super.key}); @override Widget build(BuildContext context) { return BlocBuilder<AuthBloc, AuthState>( builder: (context, state) { if (state is AuthenticatedState) { return Column( children: <Widget>[ Text('Welcome, ${state.user?.displayName}'), Text('Your email is ${state.user?.email}'), ], ); } else { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ TextField( controller: _emailController, decoration: const InputDecoration( hintText: 'Enter your email', ), ), TextField( controller: _passwordController, decoration: const InputDecoration( hintText: 'Enter your password', ), ), ElevatedButton( onPressed: () { BlocProvider.of<AuthBloc>(context).add( LoginEvent( email: _emailController.text, password: _passwordController.text, ), ); }, child: const Text('Login'), ), ], ), ), ); } } ); } } class AuthBloc extends Bloc<LoginEvent, AuthState> { final _firebaseAuth = FirebaseAuth.instance; AuthBloc() : super(InitialAuthState()); // AuthState get initialState => InitialAuthState(); Stream<AuthState> mapEventToState(LoginEvent event) async* { try { await _firebaseAuth.signInWithEmailAndPassword( email: event.email, password: event.password, ); yield AuthenticatedState(); } catch (error) { yield AuthenticationErrorState(error: error.toString()); } } } abstract class AuthState {} class InitialAuthState extends AuthState {} class AuthenticatedState extends AuthState { final User? user; AuthenticatedState({this.user}); } class AuthenticationErrorState extends AuthState { final String error; AuthenticationErrorState({required this.error}); } class LoginEvent { final String email; final String password; LoginEvent({required this.email, required this.password}); }
Editor is loading...