Skip to content
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

LoginApp Diosaban #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions LoginApp-Diosaban/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:login_app/models/user.dart';
import 'package:login_app/screens/wrapper.dart';
import 'package:login_app/services/auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';
import 'package:login_app/screens/about_screen.dart';
import 'package:login_app/screens/home_screen.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<User>.value(
value:AuthService().user,
child: MaterialApp(
home: Wrapper()
),
);
}
}
5 changes: 5 additions & 0 deletions LoginApp-Diosaban/models/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User {
final String uid;

User({this.uid});
}
26 changes: 26 additions & 0 deletions LoginApp-Diosaban/screens/about_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:login_app/screens/home_screen.dart';

class AboutScreen extends StatefulWidget {
@override
_AboutScreenState createState() => _AboutScreenState();
}
class _AboutScreenState extends State<AboutScreen> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("About this app: I have no idea what to put in this app", style: TextStyle(fontSize: 18),),
FloatingActionButton(onPressed: () {
Navigator.pop(context);
},
child: Text("Go Back"),)
],
),
);
}
}
57 changes: 57 additions & 0 deletions LoginApp-Diosaban/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:login_app/screens/about_screen.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
@override
Future navigateToSubPage(context) async {
Navigator.push(
context, MaterialPageRoute(builder: (context) => AboutScreen()));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home Screen"),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/Home.png"),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Welcome to the department.",
style: TextStyle(color: Colors.white, fontSize: 18),
),
RaisedButton(
color: Colors.blue,
onPressed: () {
navigateToSubPage(context);
},
child: Text("About Us"),
),
RaisedButton(
color: Colors.red,
onPressed: () {
FirebaseAuth.instance.signOut();
},
child: Text("Sign out"),
),
],
),
)));
}
}


32 changes: 32 additions & 0 deletions LoginApp-Diosaban/screens/signin_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:login_app/services/auth.dart';
import 'package:flutter/material.dart';

class SignInScreen extends StatefulWidget {
@override
_SignInScreenState createState() => _SignInScreenState();
}

class _SignInScreenState extends State<SignInScreen> {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
onPressed: () async {
dynamic result = await _auth.signInAnon();
if(result == null) {
print("Error Signing in");
} else {
print("Signed in");
}
},
child: Text("Signin"),
)
],
),
);
}
}
24 changes: 24 additions & 0 deletions LoginApp-Diosaban/screens/wrapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:login_app/models/user.dart';
import 'package:login_app/screens/home_screen.dart';
import 'package:login_app/screens/signin_screen.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Wrapper extends StatefulWidget {
@override
_WrapperState createState() => _WrapperState();
}

class _WrapperState extends State<Wrapper> {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
//TODO if no user data signin, home_screen otherwise
if(user == null) {
return SignInScreen();
} else {
return HomeScreen();
}
}
}

34 changes: 34 additions & 0 deletions LoginApp-Diosaban/services/auth.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:login_app/models/user.dart';
import 'package:firebase_auth/firebase_auth.dart' as fire;

class AuthService {
final fire.FirebaseAuth _auth = fire.FirebaseAuth.instance;

User _getUserFromFirebaseUser(fire.User user) {
return user != null ? User(uid: user.uid) : null;
}

Stream<User> get user {
return _auth.authStateChanges().map(_getUserFromFirebaseUser);
}

Future signInAnon() async {
try {
fire.UserCredential result = await _auth.signInAnonymously();
fire.User user = result.user;
return _getUserFromFirebaseUser(user);
} catch (e)
{
print(e.toString());
}
}

Future signOut() async {
try {
return await _auth.signOut();
} catch (e) {
print(e.toString());
return null;
}
}
}