Improve credentials form. Added shared_preferences.
This commit is contained in:
parent
1868f4e722
commit
4e7bec0f93
|
@ -1,18 +1,24 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
final log = Logger();
|
||||
|
||||
class CredentialsForm extends StatefulWidget {
|
||||
CredentialsForm({Key key}) : super(key: key);
|
||||
CredentialsForm({@required this.defaultNextRoute, Key key}) : super(key: key);
|
||||
final String defaultNextRoute;
|
||||
|
||||
@override
|
||||
_CredentialsState createState() => _CredentialsState();
|
||||
}
|
||||
|
||||
class _CredentialsState extends State<CredentialsForm> {
|
||||
static String title = "Credentials";
|
||||
String username;
|
||||
String password;
|
||||
bool _hidden = true;
|
||||
final _credFormKey = GlobalKey<FormState>();
|
||||
final _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
InputDecoration _textFieldDecoration(String label, IconData icon) {
|
||||
return InputDecoration(
|
||||
labelText: label,
|
||||
|
@ -35,9 +41,37 @@ class _CredentialsState extends State<CredentialsForm> {
|
|||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Form(
|
||||
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(
|
||||
|
@ -45,22 +79,23 @@ class _CredentialsState extends State<CredentialsForm> {
|
|||
autocorrect: false,
|
||||
decoration:
|
||||
this._textFieldDecoration("username", Icons.alternate_email),
|
||||
validator: this.validateUsername),
|
||||
validator: this.validateUsername,
|
||||
onSaved: (String user) {
|
||||
username = user;
|
||||
}),
|
||||
TextFormField(
|
||||
autocorrect: false,
|
||||
decoration: this._textFieldDecoration("password", Icons.lock),
|
||||
validator: this.validatePassword,
|
||||
obscureText: this._hidden),
|
||||
obscureText: this._hidden,
|
||||
onSaved: (String pass) {
|
||||
password = pass;
|
||||
}),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
RaisedButton(
|
||||
onPressed: () {
|
||||
if (_credFormKey.currentState.validate()) {
|
||||
Scaffold.of(context)
|
||||
.showSnackBar(SnackBar(content: Text('Processing Data')));
|
||||
}
|
||||
},
|
||||
onPressed: this._onPressed,
|
||||
child: Text('Validate'),
|
||||
),
|
||||
Row(children: <Widget>[
|
||||
|
@ -76,5 +111,10 @@ class _CredentialsState extends State<CredentialsForm> {
|
|||
])
|
||||
]),
|
||||
]));
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
key: _scaffoldKey, appBar: AppBar(title: Text(title)), body: theForm());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,6 +109,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
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:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
|
@ -172,3 +179,4 @@ packages:
|
|||
version: "2.0.8"
|
||||
sdks:
|
||||
dart: ">=2.2.2 <3.0.0"
|
||||
flutter: ">=1.5.0 <2.0.0"
|
||||
|
|
|
@ -23,6 +23,7 @@ dependencies:
|
|||
http: ^0.12.0+2
|
||||
logger: ^0.7.0+2
|
||||
dbcrypt: ^1.0.0
|
||||
shared_preferences: ^0.5.3+4
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^0.1.2
|
||||
|
|
Loading…
Reference in New Issue
Block a user