First,
We installed flutterfire cli for our computer(or you can do it manually, but I recommend practicing for new method)
second,
Install flutterfire cli for your project article here (step by step simple guide)
Next,
and install these packages,
firebase_auth (here)
firebase_core (here)
main. dart file:
import 'package:demo/firebase_options.dart';
import 'package:demo/screens/home.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'screens/login_screen.dart';
import 'screens/register_screen.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginView(),
routes: {
'/reg': (context) => const RegisterView(),
'/log': (context) => const LoginView(),
'/home': (context) => const HomePage(),
},
);
}
}
screens/login_screen.dart file
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import '../firebase_options.dart';
class LoginView extends StatefulWidget {
const LoginView({super.key});
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
late final TextEditingController _emailController;
late final TextEditingController _passwordController;
@override
void initState() {
_emailController = TextEditingController();
_passwordController = TextEditingController();
super.initState();
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.done:
return SafeArea(
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
children: [
Text(
'Login Screen',
style:
TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
),
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
SizedBox(
height: 20,
),
Container(
child: ElevatedButton(
child: const Text('Login'),
onPressed: () async {
final email = _emailController.text;
final password = _passwordController.text;
try {
final UserCredential = await FirebaseAuth.instance
.signInWithEmailAndPassword(
email: email,
password: password,
);
//add to go your home page ----- (login UI)
} on FirebaseAuthException catch (e) {
print(e);
}
},
)),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/reg', (Route<dynamic> route) => false);
},
child: Text('Go to Register view'))
],
),
),
));
default:
return const SafeArea(
child: Center(
child: CircularProgressIndicator(
color: Colors.green,
),
),
);
}
},
));
}
}
screens/register_screen.dart file
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import '../firebase_options.dart';
class RegisterView extends StatefulWidget {
const RegisterView({super.key});
@override
State<RegisterView> createState() => _RegisterViewState();
}
//13:41:36
class _RegisterViewState extends State<RegisterView> {
late final TextEditingController _emailController;
late final TextEditingController _passwordController;
@override
void initState() {
_emailController = TextEditingController();
_passwordController = TextEditingController();
super.initState();
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.done:
return SafeArea(
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
children: [
Text(
'Register Screen',
style: TextStyle(
fontSize: 40, fontWeight: FontWeight.bold),
),
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
TextField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 18,
),
),
),
SizedBox(
height: 20,
),
Container(
child: ElevatedButton(
child: const Text('Register'),
onPressed: () async {
final email = _emailController.text;
final password = _passwordController.text;
try {
final UserCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email,
password: password,
);
} on FirebaseAuthException catch (e) {
print(e);
}
},
)),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/log', (Route<dynamic> route) => false);
},
child: Text('Go to Login view'))
],
),
),
),
);
default:
return const Text('Loading ....');
}
},
));
}
}
This comment has been removed by a blog administrator.
ReplyDelete