Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:blogging_app/Firebase/google_signIn.dart';
- import 'package:blogging_app/Firebase/main_tgglePage.dart';
- import 'package:blogging_app/Other_screens/splash_screen.dart';
- import 'package:flutter/material.dart';
- import 'package:font_awesome_flutter/font_awesome_flutter.dart';
- class LoginScreen extends StatefulWidget {
- final GoogleSignin googleSignin;
- final String username;
- final String userId;
- final String userEmail;
- final String userPhoto;
- LoginScreen(
- {this.googleSignin,
- this.username,
- this.userId,
- this.userEmail,
- this.userPhoto});
- @override
- _LoginScreenState createState() => _LoginScreenState();
- }
- class _LoginScreenState extends State<LoginScreen> {
- final scaffoldKey = GlobalKey<ScaffoldState>();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.white,
- key: scaffoldKey,
- body: Hero(
- tag: 'nepal',
- child: Container(
- decoration: BoxDecoration(
- image: DecorationImage(
- image: AssetImage(
- "assets/images/bgLight.png",
- ),
- fit: BoxFit.contain),
- ),
- child: ListView(
- physics: BouncingScrollPhysics(),
- reverse: true,
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.only(bottom: 60.0),
- child: LoginPage(
- scaffoldKey: scaffoldKey,
- googleSignin: widget.googleSignin,
- username: widget.username,
- userEmail: widget.userEmail,
- userId: widget.userId,
- userPhoto: widget.userPhoto),
- ),
- ],
- )),
- ));
- }
- }
- class LoginPage extends StatefulWidget {
- final GlobalKey scaffoldKey;
- final GoogleSignin googleSignin;
- final String username;
- final String userId;
- final String userEmail;
- final String userPhoto;
- LoginPage(
- {this.scaffoldKey,
- this.googleSignin,
- this.username,
- this.userId,
- this.userEmail,
- this.userPhoto});
- @override
- _LoginPageState createState() => _LoginPageState();
- }
- class _LoginPageState extends State<LoginPage> {
- String errorText = "";
- final TextEditingController _pwController = TextEditingController();
- Progress progress = Progress.notLoading;
- String _email = "";
- String _password = "";
- final _formKey = GlobalKey<FormState>();
- @override
- Widget build(BuildContext context) {
- return Column(children: <Widget>[
- Text(errorText),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- _raisedButton(" LogIn ", Icons.lock_open, () {
- _showForm(widget.scaffoldKey.currentState.context, 0);
- }),
- SizedBox(
- width: 30.0,
- ),
- _raisedButton(" SignUp ", Icons.note_add, () {
- _showForm(widget.scaffoldKey.currentState.context, 1);
- }),
- ],
- ),
- SizedBox(height: 30.0),
- _outlineButton(
- " Continue with Google",
- Icon(
- FontAwesomeIcons.google,
- color: Colors.red,
- ), () async {
- await widget.googleSignin.signIn().whenComplete(() {
- Navigator.pushReplacement(
- context,
- MaterialPageRoute(
- builder: (context) => TogglePage(
- googleSignin: widget.googleSignin,
- )));
- });
- })
- ]);
- }
- Widget _raisedButton(String label, IconData icons, ontap()) {
- return SizedBox(
- height: 50.0,
- child: RaisedButton.icon(
- icon: Icon(
- icons,
- color: Colors.white,
- ),
- onPressed: () {
- ontap();
- },
- label: Text(
- label,
- ),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
- ),
- );
- }
- Widget _outlineButton(String label, Icon icons, ontap()) {
- return SizedBox(
- height: 55.0,
- child: OutlineButton.icon(
- borderSide: BorderSide(color: Colors.red, width: 2.0),
- highlightedBorderColor: Colors.red,
- disabledBorderColor: Colors.green,
- padding: EdgeInsets.symmetric(horizontal: 25.0),
- icon: icons,
- onPressed: () {
- ontap();
- },
- label: Text(label, style: TextStyle(color: Colors.black)),
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
- ),
- );
- }
- Future _showForm(BuildContext context, int number) {
- return showDialog(
- context: context,
- builder: (context) {
- return Container(
- child: Dialog(
- child: Container(
- padding: EdgeInsets.symmetric(vertical: 10.0),
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(300.0),
- ),
- child: Form(
- key: _formKey,
- child: ListView(
- shrinkWrap: true,
- physics: BouncingScrollPhysics(),
- padding:
- EdgeInsets.symmetric(vertical: 30.0, horizontal: 8.0),
- children: <Widget>[
- TextFormField(
- validator: (label) => label.contains("@gmail.com")
- ? null
- : "Enter valid email",
- onSaved: (label) => _email = label,
- decoration: InputDecoration(
- hintText: "Email",
- icon: Icon(
- Icons.email,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- )),
- SizedBox(
- height: 10.0,
- ),
- TextFormField(
- validator: (label) => label.length >= 6
- ? null
- : "Try using longer password",
- onSaved: (password) => _password = password,
- controller: number == 1 ? _pwController : null,
- obscureText: true,
- decoration: InputDecoration(
- hintText: "Password",
- icon: Icon(
- Icons.lock_outline,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- )),
- number == 1
- ? TextFormField(
- obscureText: true,
- validator: (confirm) =>
- confirm == _pwController.text
- ? null
- : "Password doesnt match",
- decoration: InputDecoration(
- hintText: "Confirm Password",
- icon: Icon(
- Icons.lock,
- color: Colors.grey,
- ),
- focusedBorder: UnderlineInputBorder(
- borderSide:
- BorderSide(color: Colors.black)),
- border: UnderlineInputBorder(),
- enabledBorder: UnderlineInputBorder(
- borderSide: BorderSide(color: Colors.grey),
- ),
- ))
- : Container(),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 25.0),
- child: SizedBox(
- height: 55.0,
- child: RaisedButton(
- color: Colors.blue.shade900,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(25.0)),
- child: Text(
- number == 0 ? "LogIn" : "Create account",
- style: TextStyle(color: Colors.white)),
- onPressed: () async {
- if (_formKey.currentState.validate()) {
- _formKey.currentState.save();
- if (number == 0) {
- try {
- await widget.googleSignin
- .firebaseSignIn(_email, _password)
- .whenComplete(() {
- Navigator.pushReplacement(
- context,
- MaterialPageRoute(
- builder: (context) => TogglePage(
- googleSignin:
- widget.googleSignin,
- )));
- });
- print("LoggedIn: $_email and $_password");
- } catch (e) {
- setState(() {
- errorText = e.toString();
- });
- }
- } else {
- try {
- await widget.googleSignin
- .firebaseRegister(_email, _password);
- } catch (e) {
- setState(() {
- errorText = e.toString();
- });
- }
- Navigator.pop(context);
- _signUpShow(_email);
- print("Registered: $_email and $_password");
- }
- _formKey.currentState.reset();
- } else {
- print("Error");
- }
- },
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- });
- }
- Future _signUpShow(String email) {
- return showDialog(
- context: context,
- child: AlertDialog(
- title: Text(
- "Created user successfully. Now you can login with your account",
- style: TextStyle(color: Colors.black)),
- content: Text(email,
- style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold)),
- actions: <Widget>[
- FlatButton(
- child: Text("OK"),
- onPressed: () {
- Navigator.pop(context);
- },
- )
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement