diff options
| -rw-r--r-- | lib/firebase/auth.dart | 37 | ||||
| -rw-r--r-- | lib/firebase/firestore.dart | 0 | ||||
| -rw-r--r-- | lib/main.dart | 87 | ||||
| -rw-r--r-- | lib/screens/home_screen.dart | 46 | ||||
| -rw-r--r-- | lib/screens/login_screen.dart | 40 | ||||
| -rw-r--r-- | pubspec.lock | 58 | ||||
| -rw-r--r-- | pubspec.yaml | 3 |
7 files changed, 189 insertions, 82 deletions
diff --git a/lib/firebase/auth.dart b/lib/firebase/auth.dart new file mode 100644 index 0000000..a48243e --- /dev/null +++ b/lib/firebase/auth.dart @@ -0,0 +1,37 @@ +import 'package:cloud_firestore/cloud_firestore.dart';
+import 'package:firebase_auth/firebase_auth.dart';
+import 'package:google_sign_in/google_sign_in.dart';
+
+class AuthMethords{
+ final FirebaseAuth _auth = FirebaseAuth.instance;
+ final FirebaseFirestore _firestore = FirebaseFirestore.instance;
+
+ Future <bool> signInWIthGoogle() async {
+ bool result = false ;
+ try{
+ final GoogleSignInAccount ? googleUser = await GoogleSignIn().signIn();
+ final GoogleSignInAuthentication ? googleAuth = await googleUser?.authentication;
+ final credential = GoogleAuthProvider.credential(accessToken: googleAuth?.accessToken,idToken: googleAuth?.idToken);
+
+ UserCredential userCredential = await _auth.signInWithCredential(credential);
+ User? user = userCredential.user;
+
+ if(user != null){
+ if(userCredential.additionalUserInfo!.isNewUser){
+ //adding data to firebase
+ await _firestore.collection("users").doc(user.uid).set({
+ "username" : user.displayName,
+ "uid" : user.uid,
+ "profilePhoto" : user.photoURL,
+ "email" : user.email,
+ });
+ }
+ result = true;
+ }
+ return result;
+ } catch (e) {
+ print(e);
+ }
+ return result;
+ }
+}
\ No newline at end of file diff --git a/lib/firebase/firestore.dart b/lib/firebase/firestore.dart new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/firebase/firestore.dart diff --git a/lib/main.dart b/lib/main.dart index c613188..85019cc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,10 @@ +import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
+import 'package:project_residue/screens/login_screen.dart';
-void main() {
+void main() async {
WidgetsFlutterBinding.ensureInitialized();
+ await Firebase.initializeApp();
runApp(const MyApp());
}
@@ -14,16 +17,7 @@ class MyApp extends StatelessWidget { return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
- // This is the theme of your application.
- //
- // Try running your application with "flutter run". You'll see the
- // application has a blue toolbar. Then, without quitting the app, try
- // changing the primarySwatch below to Colors.green and then invoke
- // "hot reload" (press "r" in the console where you ran "flutter run",
- // or simply save your changes to "hot reload" in a Flutter IDE).
- // Notice that the counter didn't reset back to zero; the application
- // is not restarted.
- primarySwatch: Colors.blue,
+ primarySwatch: Colors.green,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
@@ -32,16 +26,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
-
- // This widget is the home page of your application. It is stateful, meaning
- // that it has a State object (defined below) that contains fields that affect
- // how it looks.
-
- // This class is the configuration for the state. It holds the values (in this
- // case the title) provided by the parent (in this case the App widget) and
- // used by the build method of the State. Fields in a Widget subclass are
- // always marked "final".
-
final String title;
@override
@@ -49,68 +33,9 @@ class MyHomePage extends StatefulWidget { }
class _MyHomePageState extends State<MyHomePage> {
- int _counter = 0;
-
- void _incrementCounter() {
- setState(() {
- // This call to setState tells the Flutter framework that something has
- // changed in this State, which causes it to rerun the build method below
- // so that the display can reflect the updated values. If we changed
- // _counter without calling setState(), then the build method would not be
- // called again, and so nothing would appear to happen.
- _counter++;
- });
- }
@override
Widget build(BuildContext context) {
- // This method is rerun every time setState is called, for instance as done
- // by the _incrementCounter method above.
- //
- // The Flutter framework has been optimized to make rerunning build methods
- // fast, so that you can just rebuild anything that needs updating rather
- // than having to individually change instances of widgets.
- return Scaffold(
- appBar: AppBar(
- // Here we take the value from the MyHomePage object that was created by
- // the App.build method, and use it to set our appbar title.
- title: Text(widget.title),
- ),
- body: Center(
- // Center is a layout widget. It takes a single child and positions it
- // in the middle of the parent.
- child: Column(
- // Column is also a layout widget. It takes a list of children and
- // arranges them vertically. By default, it sizes itself to fit its
- // children horizontally, and tries to be as tall as its parent.
- //
- // Invoke "debug painting" (press "p" in the console, choose the
- // "Toggle Debug Paint" action from the Flutter Inspector in Android
- // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
- // to see the wireframe for each widget.
- //
- // Column has various properties to control how it sizes itself and
- // how it positions its children. Here we use mainAxisAlignment to
- // center the children vertically; the main axis here is the vertical
- // axis because Columns are vertical (the cross axis would be
- // horizontal).
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- const Text(
- 'You have pushed the button this many times:',
- ),
- Text(
- '$_counter',
- style: Theme.of(context).textTheme.headline4,
- ),
- ],
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: _incrementCounter,
- tooltip: 'Increment',
- child: const Icon(Icons.add),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
+ return login_screen();
}
}
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart new file mode 100644 index 0000000..e7b296e --- /dev/null +++ b/lib/screens/home_screen.dart @@ -0,0 +1,46 @@ +import 'package:flutter/material.dart';
+
+class HomeScreen extends StatefulWidget {
+ static const String id = 'HomeScreen';
+ const HomeScreen({super.key});
+
+ @override
+ State<HomeScreen> createState() => _HomeScreenState();
+}
+
+class _HomeScreenState extends State<HomeScreen> {
+
+ int _counter = 0;
+
+ void _incrementCounter() {
+ setState(() {
+ _counter++;
+ });
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ backgroundColor: const Color(0xff002922),
+ body: Center(
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: <Widget>[
+ const Text(
+ 'You have pushed the button this many times:',
+ ),
+ Text(
+ '$_counter',
+ style: Theme.of(context).textTheme.headline4,
+ ),
+ ],
+ ),
+ ),
+ floatingActionButton: FloatingActionButton(
+ onPressed: _incrementCounter,
+ tooltip: 'Increment',
+ child: const Icon(Icons.add),
+ ), // This trailing comma makes auto-formatting nicer for build methods.
+ );
+ }
+}
\ No newline at end of file diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart new file mode 100644 index 0000000..ab0e67d --- /dev/null +++ b/lib/screens/login_screen.dart @@ -0,0 +1,40 @@ +
+
+import 'package:flutter/material.dart';
+import 'package:flutter_signin_button/flutter_signin_button.dart';
+import 'package:project_residue/firebase/auth.dart';
+import 'package:project_residue/screens/home_screen.dart';
+
+
+class login_screen extends StatelessWidget {
+ static const String id = 'login_screen';
+ login_screen({Key? key}) : super(key: key);
+ final AuthMethords _authMethords = AuthMethords();
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ body: Center(
+ child: Column(
+ children: [
+ SizedBox(height: 40,),
+ SignInButton(
+ Buttons.Google,
+ onPressed: () async {
+ bool result = await _authMethords.signInWIthGoogle();
+ if(result){
+ // ignore: use_build_context_synchronously
+ Navigator.push(
+ context,
+ MaterialPageRoute(builder: (context) => const HomeScreen()),
+ );
+ }
+
+ },
+ )
+ ],
+ ),
+ ),
+ );
+ }
+}
\ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index d770cac..852d33c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -153,6 +153,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_signin_button: + dependency: "direct main" + description: + name: flutter_signin_button + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -163,6 +170,48 @@ packages: description: flutter source: sdk version: "0.0.0" + font_awesome_flutter: + dependency: transitive + description: + name: font_awesome_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "9.2.0" + google_sign_in: + dependency: "direct main" + description: + name: google_sign_in + url: "https://pub.dartlang.org" + source: hosted + version: "5.4.2" + google_sign_in_android: + dependency: transitive + description: + name: google_sign_in_android + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.2" + google_sign_in_ios: + dependency: transitive + description: + name: google_sign_in_ios + url: "https://pub.dartlang.org" + source: hosted + version: "5.5.1" + google_sign_in_platform_interface: + dependency: transitive + description: + name: google_sign_in_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" + google_sign_in_web: + dependency: transitive + description: + name: google_sign_in_web + url: "https://pub.dartlang.org" + source: hosted + version: "0.10.2" http: dependency: transitive description: @@ -233,6 +282,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + quiver: + dependency: transitive + description: + name: quiver + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" sky_engine: dependency: transitive description: flutter @@ -296,4 +352,4 @@ packages: version: "2.1.2" sdks: dart: ">=2.18.4 <3.0.0" - flutter: ">=1.20.0" + flutter: ">=2.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index 628c52e..b37d524 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,9 @@ dependencies: firebase_storage:
cloud_firestore:
+ google_sign_in:
+ flutter_signin_button:
+
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
|
