├── firebase.dart ├── generated_plugin_registrant.dart ├── home.dart ├── login.dart ├── main.dart ├── opening.dart └── register.dart /firebase.dart: -------------------------------------------------------------------------------- 1 | import 'package:firebase_auth/firebase_auth.dart'; 2 | import 'package:flutter/material.dart'; 3 | 4 | import 'home.dart'; 5 | 6 | class FlutterFireAuthService { 7 | final FirebaseAuth _firebaseAuth; 8 | 9 | FlutterFireAuthService(this._firebaseAuth); 10 | 11 | Stream get authStateChanges => _firebaseAuth.idTokenChanges(); 12 | 13 | Future signOut() async { 14 | await _firebaseAuth.signOut(); 15 | } 16 | 17 | Future signIn( 18 | {String email, String password, BuildContext context}) async { 19 | try { 20 | await _firebaseAuth.signInWithEmailAndPassword( 21 | email: email, password: password); 22 | print("Signed In"); 23 | Navigator.push( 24 | context, 25 | MaterialPageRoute( 26 | builder: (context) => HomeView(), 27 | ), 28 | ); 29 | return "Success"; 30 | } on FirebaseAuthException catch (e) { 31 | print(e.toString()); 32 | return e.message; 33 | } 34 | } 35 | 36 | Future signUp( 37 | {String email, String password, BuildContext context}) async { 38 | try { 39 | await _firebaseAuth.createUserWithEmailAndPassword( 40 | email: email, password: password); 41 | Navigator.push( 42 | context, 43 | MaterialPageRoute( 44 | builder: (context) => HomeView(), 45 | ), 46 | ); 47 | return "Success"; 48 | } on FirebaseAuthException catch (e) { 49 | print(e.toString()); 50 | return e.message; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /generated_plugin_registrant.dart: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | // ignore_for_file: lines_longer_than_80_chars 6 | 7 | import 'package:firebase_auth_web/firebase_auth_web.dart'; 8 | import 'package:firebase_core_web/firebase_core_web.dart'; 9 | 10 | import 'package:flutter_web_plugins/flutter_web_plugins.dart'; 11 | 12 | // ignore: public_member_api_docs 13 | void registerPlugins(Registrar registrar) { 14 | FirebaseAuthWeb.registerWith(registrar); 15 | FirebaseCoreWeb.registerWith(registrar); 16 | registrar.registerMessageHandler(); 17 | } 18 | -------------------------------------------------------------------------------- /home.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:provider/provider.dart'; 3 | 4 | import 'firebase.dart'; 5 | 6 | class HomeView extends StatefulWidget { 7 | HomeView({Key key}) : super(key: key); 8 | 9 | @override 10 | _HomeViewState createState() => _HomeViewState(); 11 | } 12 | 13 | class _HomeViewState extends State { 14 | @override 15 | Widget build(BuildContext context) { 16 | return Material( 17 | child: Container( 18 | width: MediaQuery.of(context).size.width, 19 | height: MediaQuery.of(context).size.height, 20 | decoration: BoxDecoration( 21 | image: DecorationImage( 22 | image: AssetImage("assets/backgroundImage.jpeg"), 23 | fit: BoxFit.cover, 24 | ), 25 | ), 26 | child: Column( 27 | mainAxisAlignment: MainAxisAlignment.center, 28 | children: [ 29 | Text('Home View'), 30 | IconButton( 31 | onPressed: () { 32 | print("Sign Out Pressed"); 33 | context.read().signOut(); 34 | Navigator.of(context).pop(); 35 | }, 36 | icon: Icon( 37 | Icons.exit_to_app, 38 | color: Colors.black, 39 | size: 35, 40 | ), 41 | ), 42 | ], 43 | ), 44 | ), 45 | ); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /login.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:provider/provider.dart'; 3 | 4 | import 'firebase.dart'; 5 | 6 | class LoginView extends StatefulWidget { 7 | LoginView({Key key}) : super(key: key); 8 | 9 | @override 10 | _LoginViewState createState() => _LoginViewState(); 11 | } 12 | 13 | class _LoginViewState extends State { 14 | final TextEditingController emailController = TextEditingController(); 15 | final TextEditingController passwordController = TextEditingController(); 16 | 17 | @override 18 | Widget build(BuildContext context) { 19 | return Scaffold( 20 | body: Container( 21 | width: MediaQuery.of(context).size.width, 22 | height: MediaQuery.of(context).size.height, 23 | decoration: BoxDecoration( 24 | image: DecorationImage( 25 | image: AssetImage("assets/backgroundImage.jpeg"), 26 | fit: BoxFit.cover, 27 | ), 28 | ), 29 | child: Column( 30 | mainAxisAlignment: MainAxisAlignment.center, 31 | children: [ 32 | Padding( 33 | padding: EdgeInsets.symmetric(horizontal: 20.0), 34 | child: TextField( 35 | controller: emailController, 36 | decoration: InputDecoration( 37 | labelText: "Email", 38 | ), 39 | ), 40 | ), 41 | Padding( 42 | padding: EdgeInsets.symmetric(horizontal: 20.0), 43 | child: TextField( 44 | controller: passwordController, 45 | obscureText: true, 46 | decoration: InputDecoration( 47 | labelText: "Password", 48 | ), 49 | ), 50 | ), 51 | SizedBox(height: MediaQuery.of(context).size.height / 50), 52 | Container( 53 | width: MediaQuery.of(context).size.width / 1.4, 54 | height: 44, 55 | decoration: BoxDecoration( 56 | borderRadius: BorderRadius.circular(10.0), 57 | color: Colors.blue, 58 | ), 59 | child: MaterialButton( 60 | onPressed: () { 61 | context.read().signIn( 62 | email: emailController.text.trim(), 63 | password: passwordController.text.trim(), 64 | context: context, 65 | ); 66 | }, 67 | child: Text( 68 | "Sign In", 69 | style: TextStyle( 70 | color: Colors.white, 71 | fontSize: 18.0, 72 | ), 73 | ), 74 | ), 75 | ), 76 | ], 77 | ), 78 | ), 79 | ); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /main.dart: -------------------------------------------------------------------------------- 1 | import 'package:firebase_auth/firebase_auth.dart'; 2 | import 'package:firebase_core/firebase_core.dart'; 3 | import 'package:flutter/material.dart'; 4 | import 'package:provider/provider.dart'; 5 | 6 | import 'firebase.dart'; 7 | import 'opening.dart'; 8 | 9 | void main() async { 10 | WidgetsFlutterBinding.ensureInitialized(); 11 | await Firebase.initializeApp(); 12 | runApp(MyApp()); 13 | } 14 | 15 | class MyApp extends StatelessWidget { 16 | @override 17 | Widget build(BuildContext context) { 18 | return MultiProvider( 19 | providers: [ 20 | Provider( 21 | create: (_) => FlutterFireAuthService(FirebaseAuth.instance), 22 | ), 23 | StreamProvider( 24 | create: (context) => 25 | context.read().authStateChanges, 26 | initialData: null, 27 | ) 28 | ], 29 | child: MaterialApp( 30 | title: 'FlutterFire Provider Template', 31 | home: OpeningView(), 32 | ), 33 | ); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /opening.dart: -------------------------------------------------------------------------------- 1 | import 'package:firebase_auth/firebase_auth.dart'; 2 | import 'package:flutter/material.dart'; 3 | import 'package:provider/provider.dart'; 4 | 5 | import 'home.dart'; 6 | import 'login.dart'; 7 | import 'register.dart'; 8 | 9 | class OpeningView extends StatefulWidget { 10 | OpeningView({Key key}) : super(key: key); 11 | 12 | @override 13 | _OpeningViewState createState() => _OpeningViewState(); 14 | } 15 | 16 | class _OpeningViewState extends State { 17 | @override 18 | Widget build(BuildContext context) { 19 | final firebaseUser = context.watch(); 20 | 21 | if (firebaseUser != null) { 22 | print("Home View"); 23 | return HomeView(); 24 | } 25 | 26 | print("Not Authenticated"); 27 | 28 | return Scaffold( 29 | body: Container( 30 | width: MediaQuery.of(context).size.width, 31 | height: MediaQuery.of(context).size.height, 32 | decoration: BoxDecoration( 33 | image: DecorationImage( 34 | image: AssetImage("assets/backgroundImage.jpeg"), 35 | fit: BoxFit.cover, 36 | ), 37 | ), 38 | child: Column( 39 | mainAxisAlignment: MainAxisAlignment.center, 40 | children: [ 41 | SizedBox(height: MediaQuery.of(context).size.height / 2.5), 42 | Container( 43 | width: MediaQuery.of(context).size.width / 1.4, 44 | height: 44, 45 | decoration: BoxDecoration( 46 | borderRadius: BorderRadius.circular(10.0), 47 | color: Colors.blue, 48 | ), 49 | child: MaterialButton( 50 | onPressed: () { 51 | Navigator.push( 52 | context, 53 | MaterialPageRoute( 54 | builder: (context) => RegisterView(), 55 | ), 56 | ); 57 | }, 58 | child: Text( 59 | "Sign Up", 60 | style: TextStyle( 61 | color: Colors.white, 62 | fontSize: 18.0, 63 | ), 64 | ), 65 | ), 66 | ), 67 | SizedBox(height: MediaQuery.of(context).size.height / 50), 68 | Container( 69 | width: MediaQuery.of(context).size.width / 1.4, 70 | height: 44, 71 | decoration: BoxDecoration( 72 | borderRadius: BorderRadius.circular(10.0), 73 | border: Border.all( 74 | color: Colors.white, 75 | width: 2.0, 76 | ), 77 | ), 78 | child: MaterialButton( 79 | onPressed: () { 80 | Navigator.push( 81 | context, 82 | MaterialPageRoute( 83 | builder: (context) => LoginView(), 84 | ), 85 | ); 86 | }, 87 | child: Text( 88 | "Sign In", 89 | style: TextStyle( 90 | color: Colors.white, 91 | fontSize: 18.0, 92 | fontWeight: FontWeight.bold, 93 | ), 94 | ), 95 | ), 96 | ), 97 | ], 98 | ), 99 | ), 100 | ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /register.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:provider/provider.dart'; 3 | 4 | import 'firebase.dart'; 5 | 6 | class RegisterView extends StatefulWidget { 7 | RegisterView({Key key}) : super(key: key); 8 | 9 | @override 10 | _RegisterViewState createState() => _RegisterViewState(); 11 | } 12 | 13 | class _RegisterViewState extends State { 14 | final TextEditingController emailController = TextEditingController(); 15 | final TextEditingController passwordController = TextEditingController(); 16 | 17 | @override 18 | Widget build(BuildContext context) { 19 | return Scaffold( 20 | body: Container( 21 | width: MediaQuery.of(context).size.width, 22 | height: MediaQuery.of(context).size.height, 23 | decoration: BoxDecoration( 24 | image: DecorationImage( 25 | image: AssetImage("assets/backgroundImage.jpeg"), 26 | fit: BoxFit.cover, 27 | ), 28 | ), 29 | child: Column( 30 | mainAxisAlignment: MainAxisAlignment.center, 31 | children: [ 32 | Padding( 33 | padding: EdgeInsets.symmetric(horizontal: 20.0), 34 | child: TextField( 35 | controller: emailController, 36 | decoration: InputDecoration( 37 | labelText: "Email", 38 | ), 39 | ), 40 | ), 41 | Padding( 42 | padding: EdgeInsets.symmetric(horizontal: 20.0), 43 | child: TextField( 44 | controller: passwordController, 45 | obscureText: true, 46 | decoration: InputDecoration( 47 | labelText: "Password", 48 | ), 49 | ), 50 | ), 51 | SizedBox(height: MediaQuery.of(context).size.height / 50), 52 | Container( 53 | width: MediaQuery.of(context).size.width / 1.4, 54 | height: 44, 55 | decoration: BoxDecoration( 56 | borderRadius: BorderRadius.circular(10.0), 57 | color: Colors.blue, 58 | ), 59 | child: MaterialButton( 60 | onPressed: () { 61 | context.read().signUp( 62 | email: emailController.text.trim(), 63 | password: passwordController.text.trim(), 64 | context: context, 65 | ); 66 | }, 67 | child: Text( 68 | "Sign Up", 69 | style: TextStyle( 70 | color: Colors.white, 71 | fontSize: 18.0, 72 | ), 73 | ), 74 | ), 75 | ), 76 | ], 77 | ), 78 | ), 79 | ); 80 | } 81 | } 82 | --------------------------------------------------------------------------------