Improve credentials form. Added shared_preferences.

This commit is contained in:
sfigato 2019-08-21 21:44:24 -03:00
parent 1868f4e722
commit 4e7bec0f93
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 89 additions and 40 deletions

View File

@ -1,18 +1,24 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
final log = Logger();
class CredentialsForm extends StatefulWidget { class CredentialsForm extends StatefulWidget {
CredentialsForm({Key key}) : super(key: key); CredentialsForm({@required this.defaultNextRoute, Key key}) : super(key: key);
final String defaultNextRoute;
@override @override
_CredentialsState createState() => _CredentialsState(); _CredentialsState createState() => _CredentialsState();
} }
class _CredentialsState extends State<CredentialsForm> { class _CredentialsState extends State<CredentialsForm> {
static String title = "Credentials";
String username; String username;
String password; String password;
bool _hidden = true; bool _hidden = true;
final _credFormKey = GlobalKey<FormState>(); final _credFormKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();
InputDecoration _textFieldDecoration(String label, IconData icon) { InputDecoration _textFieldDecoration(String label, IconData icon) {
return InputDecoration( return InputDecoration(
labelText: label, labelText: label,
@ -35,46 +41,80 @@ class _CredentialsState extends State<CredentialsForm> {
return null; return null;
} }
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;
})
])
]),
]));
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Form( return Scaffold(
key: _credFormKey, key: _scaffoldKey, appBar: AppBar(title: Text(title)), body: theForm());
child: Column(children: <Widget>[
TextFormField(
autofocus: true,
autocorrect: false,
decoration:
this._textFieldDecoration("username", Icons.alternate_email),
validator: this.validateUsername),
TextFormField(
autocorrect: false,
decoration: this._textFieldDecoration("password", Icons.lock),
validator: this.validatePassword,
obscureText: this._hidden),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RaisedButton(
onPressed: () {
if (_credFormKey.currentState.validate()) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
child: Text('Validate'),
),
Row(children: <Widget>[
Text('Reveal password'),
Checkbox(
value: !this._hidden,
onChanged: (bool value) {
setState(() {
_hidden = !value;
});
return;
})
])
]),
]));
} }
} }

View File

@ -109,6 +109,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.3" version: "2.0.3"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.3+4"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -172,3 +179,4 @@ packages:
version: "2.0.8" version: "2.0.8"
sdks: sdks:
dart: ">=2.2.2 <3.0.0" dart: ">=2.2.2 <3.0.0"
flutter: ">=1.5.0 <2.0.0"

View File

@ -23,6 +23,7 @@ dependencies:
http: ^0.12.0+2 http: ^0.12.0+2
logger: ^0.7.0+2 logger: ^0.7.0+2
dbcrypt: ^1.0.0 dbcrypt: ^1.0.0
shared_preferences: ^0.5.3+4
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2