From 4e7bec0f937b5593950538c0808da51340a1e29a Mon Sep 17 00:00:00 2001 From: blallo Date: Wed, 21 Aug 2019 21:44:24 -0300 Subject: [PATCH] Improve credentials form. Added shared_preferences. --- lib/ui/credentials_form.dart | 120 +++++++++++++++++++++++------------ pubspec.lock | 8 +++ pubspec.yaml | 1 + 3 files changed, 89 insertions(+), 40 deletions(-) diff --git a/lib/ui/credentials_form.dart b/lib/ui/credentials_form.dart index 4e4c2dd..fd7bc07 100644 --- a/lib/ui/credentials_form.dart +++ b/lib/ui/credentials_form.dart @@ -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 { + static String title = "Credentials"; String username; String password; bool _hidden = true; final _credFormKey = GlobalKey(); + final _scaffoldKey = GlobalKey(); InputDecoration _textFieldDecoration(String label, IconData icon) { return InputDecoration( labelText: label, @@ -35,46 +41,80 @@ class _CredentialsState extends State { return null; } + Future 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 _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 route) => false); + } else { + Navigator.pushNamed(context, nextRoute); + } + } + + Form theForm() => Form( + key: _credFormKey, + child: Column(children: [ + 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: [ + RaisedButton( + onPressed: this._onPressed, + child: Text('Validate'), + ), + Row(children: [ + Text('Reveal password'), + Checkbox( + value: !this._hidden, + onChanged: (bool value) { + setState(() { + _hidden = !value; + }); + return; + }) + ]) + ]), + ])); + @override Widget build(BuildContext context) { - return Form( - key: _credFormKey, - child: Column(children: [ - 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: [ - RaisedButton( - onPressed: () { - if (_credFormKey.currentState.validate()) { - Scaffold.of(context) - .showSnackBar(SnackBar(content: Text('Processing Data'))); - } - }, - child: Text('Validate'), - ), - Row(children: [ - Text('Reveal password'), - Checkbox( - value: !this._hidden, - onChanged: (bool value) { - setState(() { - _hidden = !value; - }); - return; - }) - ]) - ]), - ])); + return Scaffold( + key: _scaffoldKey, appBar: AppBar(title: Text(title)), body: theForm()); } } diff --git a/pubspec.lock b/pubspec.lock index 9a6ded2..eb4cace 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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" diff --git a/pubspec.yaml b/pubspec.yaml index ec82414..7448598 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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