2019-08-18 23:05:24 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2019-08-22 02:44:24 +02:00
|
|
|
import 'package:logger/logger.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2019-08-18 23:05:24 +02:00
|
|
|
|
2019-08-22 02:44:24 +02:00
|
|
|
final log = Logger();
|
2019-08-18 23:05:24 +02:00
|
|
|
|
|
|
|
class CredentialsForm extends StatefulWidget {
|
2019-08-22 02:44:24 +02:00
|
|
|
CredentialsForm({@required this.defaultNextRoute, Key key}) : super(key: key);
|
|
|
|
final String defaultNextRoute;
|
2019-08-18 23:05:24 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
_CredentialsState createState() => _CredentialsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CredentialsState extends State<CredentialsForm> {
|
2019-08-22 02:44:24 +02:00
|
|
|
static String title = "Credentials";
|
2019-08-18 23:05:24 +02:00
|
|
|
String username;
|
|
|
|
String password;
|
|
|
|
bool _hidden = true;
|
|
|
|
final _credFormKey = GlobalKey<FormState>();
|
2019-08-22 02:44:24 +02:00
|
|
|
final _scaffoldKey = GlobalKey<ScaffoldState>();
|
2019-08-18 23:05:24 +02:00
|
|
|
InputDecoration _textFieldDecoration(String label, IconData icon) {
|
|
|
|
return InputDecoration(
|
|
|
|
labelText: label,
|
|
|
|
icon: Icon(icon),
|
|
|
|
hintText: 'Please, insert your zucchetti account $label',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String validateUsername(String username) {
|
|
|
|
if (username.isEmpty) {
|
|
|
|
return 'Username must not be empty';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String validatePassword(String password) {
|
|
|
|
if (password.isEmpty) {
|
|
|
|
return 'Password must not be empty';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-22 02:44:24 +02:00
|
|
|
Future<String> getNextRoute() async {
|
|
|
|
var persistence = await SharedPreferences.getInstance();
|
|
|
|
String nextRoute = persistence.getString("nextRoute");
|
|
|
|
if (nextRoute != null) {
|
|
|
|
log.d("nextRoute: $nextRoute");
|
|
|
|
return nextRoute;
|
|
|
|
}
|
|
|
|
log.d("defaultNextRoute: ${widget.defaultNextRoute}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onPressed() async {
|
|
|
|
var persistence = await SharedPreferences.getInstance();
|
|
|
|
if (_credFormKey.currentState.validate()) {
|
|
|
|
_scaffoldKey.currentState
|
|
|
|
.showSnackBar(SnackBar(content: Text('Processing Data')));
|
|
|
|
}
|
|
|
|
_credFormKey.currentState.save();
|
|
|
|
log.d("username: ${this.username}\tpassword: ${this.password}");
|
|
|
|
persistence.setString("username", this.username);
|
|
|
|
persistence.setString("password", this.password);
|
|
|
|
String nextRoute = await getNextRoute();
|
|
|
|
if (nextRoute == null) {
|
|
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
|
|
context, widget.defaultNextRoute, (Route<dynamic> route) => false);
|
|
|
|
} else {
|
|
|
|
Navigator.pushNamed(context, nextRoute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Form theForm() => Form(
|
|
|
|
key: _credFormKey,
|
|
|
|
child: Column(children: <Widget>[
|
|
|
|
TextFormField(
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: false,
|
|
|
|
decoration:
|
|
|
|
this._textFieldDecoration("username", Icons.alternate_email),
|
|
|
|
validator: this.validateUsername,
|
|
|
|
onSaved: (String user) {
|
|
|
|
username = user;
|
|
|
|
}),
|
|
|
|
TextFormField(
|
|
|
|
autocorrect: false,
|
|
|
|
decoration: this._textFieldDecoration("password", Icons.lock),
|
|
|
|
validator: this.validatePassword,
|
|
|
|
obscureText: this._hidden,
|
|
|
|
onSaved: (String pass) {
|
|
|
|
password = pass;
|
|
|
|
}),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: <Widget>[
|
|
|
|
RaisedButton(
|
|
|
|
onPressed: this._onPressed,
|
|
|
|
child: Text('Validate'),
|
|
|
|
),
|
|
|
|
Row(children: <Widget>[
|
|
|
|
Text('Reveal password'),
|
|
|
|
Checkbox(
|
|
|
|
value: !this._hidden,
|
|
|
|
onChanged: (bool value) {
|
|
|
|
setState(() {
|
|
|
|
_hidden = !value;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
})
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
]));
|
|
|
|
|
2019-08-18 23:05:24 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2019-08-22 02:44:24 +02:00
|
|
|
return Scaffold(
|
|
|
|
key: _scaffoldKey, appBar: AppBar(title: Text(title)), body: theForm());
|
2019-08-18 23:05:24 +02:00
|
|
|
}
|
|
|
|
}
|