This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
LocalSpend-Tracker/lib/pages/login_page.dart

221 lines
7.8 KiB
Dart
Raw Normal View History

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';
import 'package:local_spend/common/widgets/labeled_checkbox.dart';
2019-08-20 15:01:56 +00:00
import 'package:local_spend/common/widgets/animatedGradientButton.dart';
const URL = "https://flutter.io/";
class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new LoginPageState();
}
}
class LoginPageState extends State<LoginPage> {
2019-08-20 12:54:45 +00:00
bool _isLoggingIn = false;
2019-07-16 11:09:10 +00:00
final TextEditingController _emailController = TextEditingController(/*text: 'test@example.com'*/); // remove
final TextEditingController _passwordController = TextEditingController(/*text: 'abc123'*/); // remove
bool _saveLoginDetails = true; // I am extremely sorry for the placement of this variable
// it will be fixed soon I promise
FocusNode focusNode; // added so focus can move automatically
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");
focusNode = FocusNode();
2019-07-16 11:09:10 +00:00
_fillLoginDetails();
}
@override
void dispose() {
focusNode.dispose(); //disposes focus node when form disposed
super.dispose();
}
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;
}
_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-08-20 12:54:45 +00:00
_isLoggingIn = true;
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-08-20 12:54:45 +00:00
requestLoginAPI(context, username, password).then((value) {
_isLoggingIn = false;
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (Navigator.canPop(context)) {
Navigator.of(context).pushNamedAndRemoveUntil(
'/HomePage', (Route<dynamic> route) => false);
} else {
Navigator.of(context).pushReplacementNamed('/HomePage');
}
},
child: PlatformScaffold(
2019-08-20 15:01:56 +00:00
body: Stack(
children: [
AnimatedBackground([Colors.lightBlue[50], Colors.lightBlue[50]], Colors.white, Alignment.topRight, Alignment.bottomLeft, 3),
2019-08-20 15:01:56 +00:00
Container(
margin: EdgeInsets.fromLTRB(60,30,60,0),
child: Column(
children: <Widget>[
Expanded(
child: AnimatedContainer(
duration: Duration(seconds: 2),
margin: EdgeInsets.fromLTRB(15,0,15,0),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/launch_image.png')
),
),
),
),
2019-08-20 15:01:56 +00:00
Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child: TextField(
autocorrect: false,
textAlign: TextAlign.center,
controller: _emailController,
decoration: InputDecoration(
hintText: "EMAIL",
hintStyle: TextStyle(fontSize: 15),
),
style: TextStyle(
fontSize: 18.0,
color: Colors.grey[800],
fontWeight: FontWeight.bold,
),
onSubmitted: (_) {
FocusScope.of(context).requestFocus(focusNode);
},
),
),
2019-08-20 15:01:56 +00:00
Padding(
padding: EdgeInsets.fromLTRB(0.0, 15.0, 0.0, 0.0),
child: TextField(
autocorrect: false,
textAlign: TextAlign.center,
controller: _passwordController,
focusNode: focusNode,
decoration: InputDecoration(
hintText: 'PASSWORD',
hintStyle: TextStyle(fontSize: 15),
),
obscureText: true,
style: TextStyle(
fontSize: 18.0,
color: Colors.grey[800],
fontWeight: FontWeight.bold,
2019-08-20 12:54:45 +00:00
),
2019-08-20 15:01:56 +00:00
onSubmitted: (_) {
login( _emailController.text,
_passwordController.text);
},
),
2019-08-20 15:01:56 +00:00
),
2019-08-12 14:18:01 +00:00
2019-08-20 15:01:56 +00:00
Container(
margin: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 30.0),
width: 100,
height: 50,
child: Opacity(
opacity: _isLoggingIn ? 0.5 : 1,
child: ClipRRect(
borderRadius: BorderRadius.circular(2),
child : Stack(
children : [
AnimatedBackground([Colors.blue, Colors.lightBlue[300]], Colors.lightBlue, Alignment.bottomRight, Alignment.topLeft, 3),
2019-08-20 15:01:56 +00:00
Material(
type: MaterialType.transparency,
child : InkWell(
onTap: _isLoggingIn ? null : () => login( _emailController.text, _passwordController.text),
child: new Center(
child: new Text(
'GO', style: new TextStyle(fontSize: 18, color: Colors.white),),
),
),
2019-08-20 12:54:45 +00:00
),
2019-08-20 15:01:56 +00:00
],
2019-08-12 14:18:01 +00:00
),
),
),
),
2019-08-20 15:01:56 +00:00
Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 50),
2019-08-20 15:01:56 +00:00
child: LabeledCheckbox(
label : "SAVE LOGIN",
textStyle: TextStyle(fontSize: 18, color: Colors.black54, fontWeight: FontWeight.bold),
padding: const EdgeInsets.fromLTRB(0,0,0,0),
value : _saveLoginDetails,
2019-08-20 15:01:56 +00:00
onChanged: (bool newValue) {
setState(() {
_saveLoginDetails = newValue;
});
},
),
),
2019-08-20 15:01:56 +00:00
],
),
),
2019-08-20 15:01:56 +00:00
],
),
),
);
}
}