1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/routes/login_route.dart

138 lines
3.8 KiB
Dart
Raw Normal View History

2019-04-22 17:16:26 +00:00
import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/models/authentication_details.dart';
import 'package:comunic/ui/routes/home_route.dart';
2019-04-21 09:44:07 +00:00
import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
2019-04-22 17:16:26 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-04-21 08:34:27 +00:00
import 'package:flutter/material.dart';
/// Login route
///
/// @author Pierre HUBERT
class LoginRoute extends StatefulWidget {
@override
State<StatefulWidget> createState() => _LoginRouteState();
}
class _LoginRouteState extends State<LoginRoute> {
2019-04-21 09:44:07 +00:00
String _currEmail;
String _currPassword;
2019-04-22 17:16:26 +00:00
AuthResult _authResult;
2019-04-23 06:46:50 +00:00
bool _loading = false;
2019-04-21 09:44:07 +00:00
@override
void initState() {
super.initState();
_currEmail = "";
_currPassword = "";
}
void _emailChanged(String newEmail) {
setState(() {
_currEmail = newEmail;
});
}
void _passwordChanged(String newValue) {
setState(() {
_currPassword = newValue;
});
}
/// Call this whenever the user request to perform login
2019-04-22 17:16:26 +00:00
Future<void> _submitForm(BuildContext context) async {
2019-04-23 06:46:50 +00:00
setState(() {
_loading = true;
});
2019-04-22 17:16:26 +00:00
final loginResult = await AccountHelper().signIn(
AuthenticationDetails(email: _currEmail, password: _currPassword));
2019-04-21 09:44:07 +00:00
2019-04-22 17:16:26 +00:00
if (loginResult == AuthResult.SUCCESS)
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (b) => HomeRoute()));
else
setState(() {
_authResult = loginResult;
2019-04-23 06:46:50 +00:00
_loading = false;
2019-04-22 17:16:26 +00:00
});
}
// Build error card
Widget _buildErrorCard() {
2019-04-23 06:46:50 +00:00
if (_authResult == null) return null;
2019-04-22 17:16:26 +00:00
//Determine the right message
2019-04-23 06:46:50 +00:00
final message = (_authResult == AuthResult.INVALID_CREDENTIALS
? tr("Invalid credentials!")
: (_authResult == AuthResult.TOO_MANY_ATTEMPTS
? tr(
"Too many unsuccessfull login attempts! Please try again later...")
: tr("A network error occured!")));
2019-04-22 17:16:26 +00:00
return buildErrorCard(message);
2019-04-21 09:44:07 +00:00
}
/// Build login form
2019-04-22 17:16:26 +00:00
Widget _buildLoginForm() {
// Whether the form can be submitted or not
final valid = validateEmail(_currEmail) && _currPassword.length >= 3;
2019-04-21 09:44:07 +00:00
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Text(tr("Please sign into your Comunic account: ")),
2019-04-22 17:16:26 +00:00
Container(
child: _buildErrorCard(),
),
2019-04-21 09:44:07 +00:00
//Email address
TextField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: tr("Email address"),
alignLabelWithHint: true,
2019-04-22 17:16:26 +00:00
errorText: _currEmail.length > 0 && !validateEmail(_currEmail)
2019-04-21 09:44:07 +00:00
? tr("Invalid email address!")
: null),
onChanged: _emailChanged,
),
//Password
TextField(
obscureText: true,
decoration: InputDecoration(
labelText: tr("Password"),
alignLabelWithHint: true,
),
onChanged: _passwordChanged,
onSubmitted: valid ? (s) => _submitForm(context) : null,
2019-04-21 09:44:07 +00:00
),
2019-04-23 06:46:50 +00:00
Container(
padding: EdgeInsets.all(8.0),
child: _loading
? CircularProgressIndicator()
: RaisedButton(
child: Text(tr("Sign in")),
onPressed: valid ? () => _submitForm(context) : null,
),
2019-04-21 09:44:07 +00:00
)
],
),
),
);
}
2019-04-21 08:34:27 +00:00
@override
Widget build(BuildContext context) {
2019-04-21 09:44:07 +00:00
return Scaffold(
2019-04-22 17:16:26 +00:00
appBar: AppBar(
title: Text("Comunic"),
),
body: _buildLoginForm());
2019-04-21 08:34:27 +00:00
}
2019-04-21 09:44:07 +00:00
}