2019-05-08 18:54:14 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:local_spend/common/apifunctions/request_login_api.dart';
|
|
|
|
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
|
|
|
import 'package:local_spend/common/platform/platform_scaffold.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2019-07-09 10:52:25 +00:00
|
|
|
import 'package:local_spend/common/widgets/labeled_checkbox.dart';
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
const URL = "https://flutter.io/";
|
|
|
|
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return new LoginPageState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LoginPageState extends State<LoginPage> {
|
2019-07-16 11:09:10 +00:00
|
|
|
final TextEditingController _emailController = TextEditingController(/*text: 'test@example.com'*/); // remove
|
|
|
|
final TextEditingController _passwordController = TextEditingController(/*text: 'abc123'*/); // remove
|
2019-07-09 10:52:25 +00:00
|
|
|
bool _saveLoginDetails = true; // I am extremely sorry for the placement of this variable
|
|
|
|
// it will be fixed soon I promise
|
2019-07-05 21:56:15 +00:00
|
|
|
|
2019-07-03 11:41:51 +00:00
|
|
|
FocusNode focusNode; // added so focus can move automatically
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
Future launchURL(String url) async {
|
|
|
|
if (await canLaunch(url)) {
|
|
|
|
await launch(url, forceSafariVC: true, forceWebView: true);
|
|
|
|
} else {
|
|
|
|
showDialogSingleButton(
|
|
|
|
context,
|
|
|
|
"Unable to reach your website.",
|
|
|
|
"Currently unable to reach the website $URL. Please try again at a later time.",
|
|
|
|
"OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_saveCurrentRoute("/LoginPage");
|
2019-07-03 11:41:51 +00:00
|
|
|
|
|
|
|
focusNode = FocusNode();
|
2019-07-16 11:09:10 +00:00
|
|
|
|
|
|
|
_fillLoginDetails();
|
2019-07-03 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
focusNode.dispose(); //disposes focus node when form disposed
|
|
|
|
super.dispose();
|
2019-05-08 18:54:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 11:09:10 +00:00
|
|
|
_fillLoginDetails() async {
|
|
|
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
var username = await preferences.get('username');
|
|
|
|
var password = await preferences.get('password');
|
|
|
|
|
|
|
|
_emailController.text = await username;
|
|
|
|
_passwordController.text = await password;
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
_saveCurrentRoute(String lastRoute) async {
|
|
|
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
|
|
|
await preferences.setString('LastPageRoute', lastRoute);
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:09:10 +00:00
|
|
|
login(String username, String password) async {
|
2019-07-05 14:34:39 +00:00
|
|
|
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
2019-07-16 11:09:10 +00:00
|
|
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
if (_saveLoginDetails) {
|
|
|
|
await preferences.setString('username', username);
|
|
|
|
await preferences.setString('password', password);
|
|
|
|
print("details saved");
|
|
|
|
} else {
|
|
|
|
await preferences.setString('username', "");
|
|
|
|
await preferences.setString('password', "");
|
|
|
|
print("details cleared");
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:34:39 +00:00
|
|
|
requestLoginAPI(context, username,
|
|
|
|
password);
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () {
|
|
|
|
if (Navigator.canPop(context)) {
|
|
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
2019-07-17 11:54:31 +00:00
|
|
|
'/ReceiptPage', (Route<dynamic> route) => false);
|
2019-05-08 18:54:14 +00:00
|
|
|
} else {
|
2019-07-17 11:54:31 +00:00
|
|
|
Navigator.of(context).pushReplacementNamed('/ReceiptPage');
|
2019-05-08 18:54:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
child: PlatformScaffold(
|
2019-07-05 12:39:24 +00:00
|
|
|
// drawer: BasicDrawer(),
|
2019-07-09 09:26:33 +00:00
|
|
|
// body: Container(
|
|
|
|
// decoration: BoxDecoration(color: Colors.white),
|
|
|
|
// margin: const EdgeInsets.all(20),
|
|
|
|
// child: Padding(
|
|
|
|
// padding: EdgeInsets.fromLTRB(30.0, 170.0, 30.0, 0.0),
|
|
|
|
// child: ListView(
|
|
|
|
// children: <Widget>[
|
2019-05-08 18:54:14 +00:00
|
|
|
body: Container(
|
2019-07-16 10:28:36 +00:00
|
|
|
decoration: new BoxDecoration(
|
|
|
|
gradient: new LinearGradient(
|
|
|
|
colors: [Colors.white, Colors.blue[50]],
|
|
|
|
stops: [0,1],
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
end: Alignment.bottomCenter,
|
|
|
|
),
|
|
|
|
), child: Container(
|
2019-07-09 11:44:05 +00:00
|
|
|
margin: EdgeInsets.fromLTRB(60,30,60,0),
|
2019-07-09 09:26:33 +00:00
|
|
|
child: Column(
|
2019-05-08 18:54:14 +00:00
|
|
|
children: <Widget>[
|
2019-07-09 09:26:33 +00:00
|
|
|
Expanded(
|
|
|
|
child: Container(
|
|
|
|
margin: EdgeInsets.fromLTRB(15,0,15,0),
|
|
|
|
// alignment: FractionalOffset(0.5, 0.3), // not sure what this does ngl :/
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
image: DecorationImage(
|
|
|
|
image: AssetImage('assets/images/launch_image.png')
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
2019-07-09 09:26:33 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
|
2019-05-08 18:54:14 +00:00
|
|
|
child: TextField(
|
2019-07-09 09:26:33 +00:00
|
|
|
autocorrect: false,
|
|
|
|
textAlign: TextAlign.center,
|
2019-05-08 18:54:14 +00:00
|
|
|
controller: _emailController,
|
|
|
|
decoration: InputDecoration(
|
2019-07-09 09:26:33 +00:00
|
|
|
hintText: "EMAIL",
|
|
|
|
hintStyle: TextStyle(fontSize: 15),
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
2019-05-16 16:04:08 +00:00
|
|
|
color: Colors.grey[800],
|
2019-05-08 18:54:14 +00:00
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2019-07-03 11:41:51 +00:00
|
|
|
onSubmitted: (_) {
|
|
|
|
FocusScope.of(context).requestFocus(focusNode);
|
|
|
|
},
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
2019-07-09 09:26:33 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
|
2019-05-08 18:54:14 +00:00
|
|
|
child: TextField(
|
2019-07-09 09:26:33 +00:00
|
|
|
autocorrect: false,
|
|
|
|
textAlign: TextAlign.center,
|
2019-05-08 18:54:14 +00:00
|
|
|
controller: _passwordController,
|
2019-07-03 11:41:51 +00:00
|
|
|
focusNode: focusNode,
|
2019-05-08 18:54:14 +00:00
|
|
|
decoration: InputDecoration(
|
2019-07-09 09:26:33 +00:00
|
|
|
hintText: 'PASSWORD',
|
|
|
|
hintStyle: TextStyle(fontSize: 15),
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
obscureText: true,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
2019-05-16 16:04:08 +00:00
|
|
|
color: Colors.grey[800],
|
2019-05-08 18:54:14 +00:00
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
2019-07-09 09:26:33 +00:00
|
|
|
onSubmitted: (_) {
|
|
|
|
login( _emailController.text,
|
|
|
|
_passwordController.text);
|
|
|
|
},
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
2019-07-16 10:28:36 +00:00
|
|
|
|
2019-07-09 09:26:33 +00:00
|
|
|
Padding(
|
2019-07-09 11:44:05 +00:00
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 30.0),
|
2019-07-09 09:26:33 +00:00
|
|
|
|
|
|
|
child : Material(
|
2019-07-16 10:28:36 +00:00
|
|
|
child: new Container(
|
|
|
|
child : InkWell(
|
2019-07-09 09:26:33 +00:00
|
|
|
onTap: () => login( _emailController.text, _passwordController.text),
|
|
|
|
child: new Container(
|
|
|
|
width: 100,
|
|
|
|
height: 50,
|
|
|
|
child: new Center(
|
|
|
|
child: new Text(
|
|
|
|
'GO', style: new TextStyle(fontSize: 18, color: Colors.white),),
|
|
|
|
),
|
|
|
|
),
|
2019-07-16 10:28:36 +00:00
|
|
|
splashColor: Colors.lightBlueAccent,
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
2019-07-16 10:28:36 +00:00
|
|
|
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
border: new Border.all(color : Colors.transparent, width: 2),
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(2)),
|
|
|
|
gradient: new LinearGradient(
|
|
|
|
colors: [
|
|
|
|
Colors.blue[300],
|
2019-07-16 11:09:10 +00:00
|
|
|
Colors.blue[500],
|
2019-07-16 10:28:36 +00:00
|
|
|
],
|
|
|
|
stops: [0,1],
|
|
|
|
begin: Alignment.topLeft,
|
|
|
|
end: Alignment.bottomRight,
|
|
|
|
),
|
|
|
|
),
|
2019-05-08 18:54:14 +00:00
|
|
|
),
|
|
|
|
),
|
2019-07-16 10:28:36 +00:00
|
|
|
),
|
2019-07-09 10:52:25 +00:00
|
|
|
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0, 10, 0, 50),
|
|
|
|
|
2019-07-09 11:44:05 +00:00
|
|
|
child: LabeledCheckbox(
|
2019-07-09 10:52:25 +00:00
|
|
|
label : "SAVE LOGIN",
|
|
|
|
textStyle: TextStyle(fontSize: 18, color: Colors.black54, fontWeight: FontWeight.bold),
|
|
|
|
padding: const EdgeInsets.fromLTRB(0,0,0,0),
|
|
|
|
value : _saveLoginDetails,
|
|
|
|
|
|
|
|
onChanged: (bool newValue) {
|
|
|
|
setState(() {
|
|
|
|
_saveLoginDetails = newValue;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2019-07-09 11:44:05 +00:00
|
|
|
|
|
|
|
/*child: LabeledCheckboxWithIcon(
|
|
|
|
label : "SAVE LOGIN",
|
|
|
|
textStyle: TextStyle(fontSize: 18, color: Colors.black54, fontWeight: FontWeight.bold),
|
|
|
|
icon: Icons.account_box, // need to remove icon padding!!
|
|
|
|
iconSize: 18,
|
|
|
|
iconColor: Colors.black54,
|
|
|
|
padding: const EdgeInsets.fromLTRB(0,0,0,0),
|
|
|
|
value : _saveLoginDetails,
|
|
|
|
|
|
|
|
onChanged: (bool newValue) {
|
|
|
|
setState(() {
|
|
|
|
_saveLoginDetails = newValue;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),*/
|
2019-07-09 10:52:25 +00:00
|
|
|
),
|
2019-05-08 18:54:14 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|