This repository has been archived on 2022-04-20. You can view files and clone it, but cannot push or open issues or pull requests.
app/lib/pages/register_page.dart

156 lines
6.4 KiB
Dart
Raw Normal View History

2018-10-08 14:26:01 +00:00
import 'package:flutter/material.dart';
import 'package:vikunja_app/global.dart';
2019-03-11 20:38:05 +00:00
import 'package:vikunja_app/theme/button.dart';
import 'package:vikunja_app/theme/buttonText.dart';
import 'package:vikunja_app/theme/constants.dart';
2018-10-08 14:26:01 +00:00
import 'package:vikunja_app/utils/validator.dart';
class RegisterPage extends StatefulWidget {
@override
_RegisterPageState createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
final _formKey = GlobalKey<FormState>();
final passwordController = TextEditingController();
String _server, _username, _email, _password;
bool _loading = false;
@override
Widget build(BuildContext ctx) {
return Scaffold(
appBar: AppBar(
2019-03-14 21:12:02 +00:00
title: Text('Register'),
2018-10-08 14:26:01 +00:00
),
body: Builder(
builder: (BuildContext context) => SafeArea(
top: false,
bottom: false,
child: Form(
key: _formKey,
child: ListView(
2019-03-14 21:12:02 +00:00
padding: const EdgeInsets.all(16),
2018-10-08 14:26:01 +00:00
children: <Widget>[
Padding(
2019-03-11 20:38:05 +00:00
padding: vStandardVerticalPadding,
2018-10-08 14:26:01 +00:00
child: TextFormField(
onSaved: (serverAddress) => _server = serverAddress,
validator: (address) {
return isUrl(address) ? null : 'Invalid URL';
},
decoration: new InputDecoration(
2019-03-11 20:38:05 +00:00
border: OutlineInputBorder(),
2018-10-08 14:26:01 +00:00
labelText: 'Server Address'),
),
),
Padding(
2019-03-11 20:38:05 +00:00
padding: vStandardVerticalPadding,
2018-10-08 14:26:01 +00:00
child: TextFormField(
onSaved: (username) => _username = username.trim(),
validator: (username) {
2018-11-05 15:43:30 +00:00
return username.trim().isNotEmpty
? null
: 'Please specify a username';
2018-10-08 14:26:01 +00:00
},
2019-03-11 20:38:05 +00:00
decoration: new InputDecoration(
border: OutlineInputBorder(),
labelText: 'Username'),
2018-10-08 14:26:01 +00:00
),
),
Padding(
2019-03-11 20:38:05 +00:00
padding: vStandardVerticalPadding,
2018-10-08 14:26:01 +00:00
child: TextFormField(
onSaved: (email) => _email = email,
validator: (email) {
return isEmail(email)
? null
: 'Email adress is invalid';
},
2019-03-11 20:38:05 +00:00
decoration: new InputDecoration(
border: OutlineInputBorder(),
labelText: 'Email Address'),
2018-10-08 14:26:01 +00:00
),
),
Padding(
2019-03-11 20:38:05 +00:00
padding: vStandardVerticalPadding,
2018-10-08 14:26:01 +00:00
child: TextFormField(
controller: passwordController,
onSaved: (password) => _password = password,
validator: (password) {
2018-11-05 15:43:30 +00:00
return password.length >= 8
? null
: 'Please use at least 8 characters';
2018-10-08 14:26:01 +00:00
},
2019-03-11 20:38:05 +00:00
decoration: new InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password'),
2018-10-08 14:26:01 +00:00
obscureText: true,
),
),
Padding(
2019-03-11 20:38:05 +00:00
padding: vStandardVerticalPadding,
2018-10-08 14:26:01 +00:00
child: TextFormField(
validator: (password) {
return passwordController.text == password
? null
: 'Passwords don\'t match.';
},
decoration: new InputDecoration(
2019-03-11 20:38:05 +00:00
border: OutlineInputBorder(),
2018-10-08 14:26:01 +00:00
labelText: 'Repeat Password'),
obscureText: true,
),
),
Builder(
2019-03-11 20:38:05 +00:00
builder: (context) => FancyButton(
2018-10-08 14:26:01 +00:00
onPressed: !_loading
? () {
if (_formKey.currentState
.validate()) {
Form.of(context).save();
_registerUser(context);
} else {
print("awhat");
}
}
: null,
child: _loading
? CircularProgressIndicator()
2019-03-11 20:38:05 +00:00
: VikunjaButtonText('Register'),
)),
2018-10-08 14:26:01 +00:00
],
)),
),
));
}
_registerUser(BuildContext context) async {
setState(() => _loading = true);
try {
var vGlobal = VikunjaGlobal.of(context);
var newUserLoggedIn = await vGlobal
.newUserService(_server)
.register(_username, _email, _password);
vGlobal.changeUser(newUserLoggedIn.user,
token: newUserLoggedIn.token, base: _server);
} catch (ex) {
showDialog(
context: context,
builder: (context) => new AlertDialog(
2019-03-15 22:14:37 +00:00
title: Text(
'Registration failed! Please check your server url and credentials. ' +
ex.toString()),
2018-10-08 14:26:01 +00:00
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'))
2018-10-08 14:26:01 +00:00
],
));
} finally {
setState(() {
_loading = false;
});
}
}
}