209 lines
5.4 KiB
Dart
209 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:messaging_app/components/messaging_provider.dart';
|
|
import 'package:messaging_app/service/navigation_service.dart';
|
|
import 'package:messaging_app/service/snackbar_service.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key,});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return _LoginPageState();
|
|
}
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
//
|
|
var _deviceHeight;
|
|
var _deviceWidth;
|
|
|
|
String? _email;
|
|
String? _password;
|
|
|
|
late GlobalKey<FormState> _formKey;
|
|
late MessagingProviders _auth;
|
|
|
|
_LoginPageState() {
|
|
_formKey = GlobalKey<FormState>();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
//
|
|
_deviceHeight = MediaQuery.of(context).size.height;
|
|
_deviceWidth = MediaQuery.of(context).size.width;
|
|
|
|
return Scaffold(
|
|
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
|
|
body: Align(
|
|
child: ChangeNotifierProvider<MessagingProviders>.value(
|
|
value: MessagingProviders.instance,
|
|
child: _loginPageUI(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _loginPageUI() {
|
|
return Builder(builder: (BuildContext _context) {
|
|
SnackBarService.instance.buildContext = _context;
|
|
_auth = Provider.of<MessagingProviders>(_context);
|
|
return Container(
|
|
height: _deviceHeight * 0.60,
|
|
// color: Colors.red,
|
|
padding: EdgeInsets.symmetric(horizontal: _deviceWidth * 0.10),
|
|
alignment: Alignment.center,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
mainAxisSize: MainAxisSize.max,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
_headingWidget(),
|
|
_inputForm(),
|
|
_loginButton(),
|
|
_registerButton(),
|
|
],
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
Widget _headingWidget() {
|
|
return SizedBox(
|
|
height: _deviceHeight * 0.12,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
mainAxisSize: MainAxisSize.max,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Welcome Back!',
|
|
style: TextStyle(fontSize: 35, fontWeight: FontWeight.w700),
|
|
),
|
|
Text(
|
|
'Please Login to your Account',
|
|
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w200),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _inputForm() {
|
|
return SizedBox(
|
|
height: _deviceHeight * 0.16,
|
|
child: Form(
|
|
key: _formKey,
|
|
onChanged: () {
|
|
_formKey.currentState?.save();
|
|
},
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
mainAxisSize: MainAxisSize.max,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
_emailTextField(),
|
|
_passwordTextField(),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _emailTextField() {
|
|
return TextFormField(
|
|
autocorrect: false,
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
),
|
|
validator: (_input) {
|
|
return _input!.isNotEmpty && _input.contains("@")
|
|
? null
|
|
: "Please enter a valid Email";
|
|
},
|
|
onSaved: (_input) {
|
|
setState(() {
|
|
_email = _input!;
|
|
});
|
|
},
|
|
cursorColor: Colors.white,
|
|
decoration: InputDecoration(
|
|
hintText: "Email Address",
|
|
focusedBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(color: Colors.white))),
|
|
);
|
|
}
|
|
|
|
Widget _passwordTextField() {
|
|
return TextFormField(
|
|
autocorrect: false,
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
),
|
|
validator: (_input) {
|
|
return _input!.isNotEmpty ? null : "Please enter a Password";
|
|
},
|
|
onSaved: (_input) {
|
|
setState(() {
|
|
_password = _input!;
|
|
});
|
|
},
|
|
obscureText: true,
|
|
cursorColor: Colors.white,
|
|
decoration: InputDecoration(
|
|
hintText: "Password",
|
|
focusedBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(color: Colors.white))),
|
|
);
|
|
}
|
|
|
|
Widget _loginButton() {
|
|
return _auth.status == AuthStatus.authenticating
|
|
? Align(
|
|
alignment: Alignment.center,
|
|
child: CircularProgressIndicator(),
|
|
)
|
|
: SizedBox(
|
|
height: _deviceHeight * 0.06,
|
|
width: _deviceWidth,
|
|
child: MaterialButton(
|
|
onPressed: () {
|
|
if (_formKey.currentState!.validate()) {
|
|
//Login User
|
|
_auth.login(_email!, _password!);
|
|
}
|
|
},
|
|
color: Colors.blue,
|
|
child: Text(
|
|
'LOGIN',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _registerButton() {
|
|
return GestureDetector(
|
|
onTap: () {
|
|
NavigationService.instance.navigateTo("register");
|
|
},
|
|
child: SizedBox(
|
|
height: _deviceHeight * 0.06,
|
|
width: _deviceWidth,
|
|
child: Text(
|
|
'REGISTER',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|