workouttest_app/lib/view/login.dart
2020-07-12 14:17:59 +02:00

162 lines
7.1 KiB
Dart

import 'package:aitrainer_app/localization/app_localization.dart';
import 'package:aitrainer_app/model/auth.dart';
import 'package:aitrainer_app/view/account.dart';
import 'package:aitrainer_app/viewmodel/customer_changing_view_model.dart';
import 'package:aitrainer_app/viewmodel/user_changing_view_model.dart';
import 'package:aitrainer_app/viewmodel/user_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
class LoginPage extends StatefulWidget{
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final UserViewModel user = UserViewModel();
final bool _obscureText = true;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
UserChangingViewModel model = UserChangingViewModel(user);
CustomerChangingViewModel customerChangingViewModel = Provider.of<CustomerChangingViewModel>(context, listen: false);
user.createNew();
Future<dynamic> customer;
final State<AccountPage> stateAccount = ModalRoute.of(context).settings.arguments;
return Scaffold(
key: _scaffoldKey,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/image/WT_login.png'),
fit: BoxFit.cover,
//height: double.infinity,
//width: double.infinity,
alignment: Alignment.center,
),
),
child: Form(
key: _formKey,
child: Container(
padding: const EdgeInsets.only (left: 25, right: 100),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Spacer(flex: 4),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
new InkWell(
child: new Text(
AppLocalizations.of(context).translate(
'Login'),
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 24)),
),
],
),
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
labelText: 'Email',
),
validator: (String input) {
RegExp exp = new RegExp(r"[\w._]+\@[\w._]+.[a-z]+",
caseSensitive: false,
multiLine: false,);
String ret = exp.hasMatch(input) == true ?
null :
AppLocalizations.of(context).translate(
'Please type an email address');
return ret;
},
onChanged: (input) => user.setEmail(input),
),
Spacer(flex: 1),
new TextFormField(
decoration: const InputDecoration(
filled: true,
labelText: "Password",
fillColor: Colors.white,
focusColor: Colors.white,
),
validator: (val) => val.length < 6
? AppLocalizations.of(context).translate(
'Password too short')
: null,
obscureText: _obscureText,
onChanged: (input) => user.setPassword(input),
),
Spacer(flex: 1),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[ new FlatButton(
child: Image.asset('asset/image/WT_OK.png',
width: 100,
height: 100
),
onPressed: () =>
{
if (_formKey.currentState.validate()) {
model = UserChangingViewModel(user),
model.getUser().then((_) =>
{
if ( stateAccount != null ) {
stateAccount.setState(() {
print("update account");
}),
},
customerChangingViewModel.customer.setCustomer(Auth().userLoggedIn),
Navigator.pop(context),
}).catchError(( error, stackTrace )=> showInSnackBar(error)
),
}
}),
]),
Spacer(flex: 2),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
new InkWell(
child: new Text(
AppLocalizations.of(context).translate(
'SignUp')),
onTap: () =>
Navigator.of(context).pushNamed(
'registration'),
),
Spacer(flex: 1),
new InkWell(
child: new Text(
AppLocalizations.of(context).translate(
'Privacy')),
onTap: () =>
Navigator.of(context).pushNamed('gdpr'),
),
Spacer(flex: 2),
]),
Spacer(flex: 2),
])
),
),
)
);
}
void showInSnackBar(String error) {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
backgroundColor: Colors.orange,
content: Text(
AppLocalizations.of(context).translate("Customer does not exist or the password is wrong") + " " + error,
style: TextStyle(color: Colors.white))
)
);
}
}