aggiunto schermate login e register

This commit is contained in:
saphi
2023-09-28 13:38:12 +02:00
parent e0a8559cd1
commit 11b10b7587
8 changed files with 344 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
{ {
"file_generated_by": "FlutterFire CLI", "file_generated_by": "FlutterFire CLI",
"purpose": "FirebaseAppID & ProjectID for this Firebase app in this directory", "purpose": "FirebaseAppID & ProjectID for this Firebase app in this directory",
"GOOGLE_APP_ID": "1:164857321043:ios:b497e5d24254d60e1ad8a6", "GOOGLE_APP_ID": "1:23966937248:ios:d7915a6ac5618efeb599c5",
"FIREBASE_PROJECT_ID": "progetto-m335", "FIREBASE_PROJECT_ID": "m-335-3d14b",
"GCM_SENDER_ID": "164857321043" "GCM_SENDER_ID": "23966937248"
} }

68
lib/firebase_options.dart Normal file
View File

@@ -0,0 +1,68 @@
// File generated by FlutterFire CLI.
// ignore_for_file: lines_longer_than_80_chars, avoid_classes_with_only_static_members
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;
/// Default [FirebaseOptions] for use with your Firebase apps.
///
/// Example:
/// ```dart
/// import 'firebase_options.dart';
/// // ...
/// await Firebase.initializeApp(
/// options: DefaultFirebaseOptions.currentPlatform,
/// );
/// ```
class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for web - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for macos - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.windows:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for windows - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
case TargetPlatform.linux:
throw UnsupportedError(
'DefaultFirebaseOptions have not been configured for linux - '
'you can reconfigure this by running the FlutterFire CLI again.',
);
default:
throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}
}
static const FirebaseOptions android = FirebaseOptions(
apiKey: 'AIzaSyA5Mz_Az4vCKnFl_qJwiQ07cJzXSYDAqmw',
appId: '1:23966937248:android:9694d7a3e940cff3b599c5',
messagingSenderId: '23966937248',
projectId: 'm-335-3d14b',
storageBucket: 'm-335-3d14b.appspot.com',
);
static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'AIzaSyDEJpIubqU-jFzjLP2s8S01B3Gh6W0eNgM',
appId: '1:23966937248:ios:d7915a6ac5618efeb599c5',
messagingSenderId: '23966937248',
projectId: 'm-335-3d14b',
storageBucket: 'm-335-3d14b.appspot.com',
iosBundleId: 'ch.ameti.progettoM335Flutter',
);
}

View File

@@ -1,6 +1,15 @@
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'myApp.dart'; import 'myApp.dart';
void main() { void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp()); runApp(MyApp());
} }

View File

@@ -4,13 +4,13 @@ import 'navigation.dart';
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({ Key? key }) : super(key: key); const MyApp({ Key? key }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'My App', title: 'My App',
theme: ThemeData( theme: ThemeData(
useMaterial3: true, useMaterial3: true
primaryColor: Colors.red,
), ),
home: Navigation() home: Navigation()
); );

View File

@@ -1,8 +1,13 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:progetto_m335_flutter/pages/LoginView.dart';
import 'package:progetto_m335_flutter/pages/RegisterView.dart';
import 'pages/TodayView.dart'; import 'pages/TodayView.dart';
import 'pages/InboxView.dart'; import 'pages/InboxView.dart';
import 'pages/NotesView.dart'; import 'pages/NotesView.dart';
class Navigation extends StatefulWidget { class Navigation extends StatefulWidget {
const Navigation({super.key}); const Navigation({super.key});
@@ -12,12 +17,15 @@ class Navigation extends StatefulWidget {
class _NavigationState extends State<Navigation> { class _NavigationState extends State<Navigation> {
int _selectedIndex = 0; int _selectedIndex = 1;
static const List<Widget> _widgetOptions = <Widget>[ static const List<Widget> _widgetOptions = <Widget>[
LoginView(),
RegisterView(),
TodayView(), TodayView(),
InboxView(), InboxView(),
NotesView(), NotesView(),
]; ];
FirebaseAuth auth = FirebaseAuth.instance;
void _onItemTapped(int index) { void _onItemTapped(int index) {
setState(() { setState(() {
@@ -25,6 +33,20 @@ class _NavigationState extends State<Navigation> {
}); });
} }
@override
void initState() {
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user == null) {
print('User is currently signed out!');
} else {
print('User is signed in!');
}
});
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -34,6 +56,8 @@ class _NavigationState extends State<Navigation> {
body: Center(child: _widgetOptions.elementAt(_selectedIndex)), body: Center(child: _widgetOptions.elementAt(_selectedIndex)),
bottomNavigationBar: BottomNavigationBar( bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[ items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.login), label: "Login"),
BottomNavigationBarItem(icon: Icon(Icons.app_registration), label: "Register"),
BottomNavigationBarItem( BottomNavigationBarItem(
icon: Icon(Icons.calendar_today), label: "today"), icon: Icon(Icons.calendar_today), label: "today"),
BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"), BottomNavigationBarItem(icon: Icon(Icons.inbox), label: "Inbox"),
@@ -41,6 +65,7 @@ class _NavigationState extends State<Navigation> {
], ],
currentIndex: _selectedIndex, currentIndex: _selectedIndex,
onTap: _onItemTapped, onTap: _onItemTapped,
type: BottomNavigationBarType.fixed,
), ),
); );
} }

45
lib/pages/LoginView.dart Normal file
View File

@@ -0,0 +1,45 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class LoginView extends StatefulWidget {
const LoginView({super.key});
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
@override
void initState() {
signInWithGoogle().then((value) => print(value));
super.initState();
}
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Icon(Icons.login)
)
);
}
}

186
lib/pages/RegisterView.dart Normal file
View File

@@ -0,0 +1,186 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:form_field_validator/form_field_validator.dart';
import 'package:flutter/foundation.dart';
import 'package:progetto_m335_flutter/pages/LoginView.dart';
class RegisterView extends StatefulWidget {
const RegisterView({Key? key}) : super(key: key);
@override
State<RegisterView> createState() => _RegisterViewState();
}
class _RegisterViewState extends State<RegisterView> {
String email = '';
String password = '';
bool showPassword = false;
Color buttonColor = Colors.blue[200]!;
void register() async {
print("$email $password");
if (_formkey.currentState!.validate()) {
try {
final credential =
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
}
}
Map userData = {};
final _formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Register'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Form(
key: _formkey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
onChanged: (val) {
setState(() {
email = val;
if (password.length > 6 && _formkey.currentState!.validate()) {
buttonColor = Colors.blue[400]!;
} else {
buttonColor = Colors.blue[200]!;
}
});
},
validator: MultiValidator([
RequiredValidator(errorText: 'Enter email address'),
EmailValidator(
errorText: 'Please correct email filled'),
]),
decoration: const InputDecoration(
hintText: 'Email',
labelText: 'Email',
prefixIcon: Icon(
Icons.email,
color: Colors.grey,
),
errorStyle: TextStyle(fontSize: 18.0),
border: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius:
BorderRadius.all(Radius.circular(9.0)))),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: !showPassword,
onChanged: (val) {
setState(() {
password = val;
if (password.length > 6 && _formkey.currentState!.validate()) {
buttonColor = Colors.blue[400]!;
} else {
buttonColor = Colors.blue[200]!;
}
});
},
validator: MultiValidator([
RequiredValidator(errorText: 'Enter a password'),
PatternValidator(
r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$',
errorText: 'enter a valid password'),
]),
decoration: InputDecoration(
hintText: 'Password',
labelText: 'Password',
prefixIcon: const Icon(
Icons.key,
color: Colors.grey,
),
suffixIcon: Padding(
padding: const EdgeInsets.only(right: 20.0),
child: IconButton(
icon: Icon(
showPassword
? Icons.visibility_off
: Icons.visibility,
color: Colors.grey,
),
onPressed: () {
setState(() {
showPassword = !showPassword;
});
}),
),
errorStyle: const TextStyle(fontSize: 18.0),
border: const OutlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius:
BorderRadius.all(Radius.circular(9.0)))),
)),
Center(
child: Padding(
padding: const EdgeInsets.all(18.0),
child: SizedBox(
// margin: EdgeInsets.fromLTRB(200, 20, 50, 0),
width: MediaQuery.of(context).size.width,
height: 50,
// margin: EdgeInsets.fromLTRB(200, 20, 50, 0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: buttonColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'Register',
style: TextStyle(color: Colors.white, fontSize: 22),
),
onPressed: () {
register();
},
),
),
)),
Center(
child: Padding(
padding: const EdgeInsets.only(top: 20),
child: Center(
child: TextButton(
child: const Text('Already have an account? Login'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const LoginView()),
);
},
),
),
),
),
],
)),
),
));
}
}

View File

@@ -31,12 +31,16 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
google_sign_in: ^6.1.5
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
firebase_core: ^2.16.0 firebase_core: ^2.16.0
url_launcher: ^6.1.14 url_launcher: ^6.1.14
firebase_auth: ^4.10.0
form_field_validator: ^1.1.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: