Initial framework in place, non-functional POST & env vars atm
This commit is contained in:
parent
3bc940063a
commit
5c718fc14b
29 changed files with 1263 additions and 170 deletions
50
lib/common/apifunctions/request_login_api.dart
Normal file
50
lib/common/apifunctions/request_login_api.dart
Normal file
|
@ -0,0 +1,50 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:local_spend/common/functions/save_current_login.dart';
|
||||
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
||||
import 'package:local_spend/model/json/login_model.dart';
|
||||
import 'package:local_spend/config.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
Future<LoginModel> requestLoginAPI(
|
||||
BuildContext context, String email, String password) async {
|
||||
//var apiUrl = ConfigWrapper.of(context).apiKey;
|
||||
final url = "https://dev.peartrade.org/api/login";
|
||||
|
||||
Map<String, String> body = {
|
||||
'email': email,
|
||||
'password': password,
|
||||
};
|
||||
|
||||
debugPrint('$body');
|
||||
|
||||
final response = await http.post(
|
||||
url,
|
||||
body: body,
|
||||
);
|
||||
|
||||
debugPrint(response.body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final responseJson = json.decode(response.body);
|
||||
var user = new LoginModel.fromJson(responseJson);
|
||||
|
||||
saveCurrentLogin(responseJson);
|
||||
Navigator.of(context).pushReplacementNamed('/HomePage');
|
||||
|
||||
return LoginModel.fromJson(responseJson);
|
||||
} else {
|
||||
final responseJson = json.decode(response.body);
|
||||
|
||||
saveCurrentLogin(responseJson);
|
||||
showDialogSingleButton(
|
||||
context,
|
||||
"Unable to Login",
|
||||
"You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
|
||||
"OK");
|
||||
return null;
|
||||
}
|
||||
}
|
31
lib/common/apifunctions/request_logout_api.dart
Normal file
31
lib/common/apifunctions/request_logout_api.dart
Normal file
|
@ -0,0 +1,31 @@
|
|||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:local_spend/common/functions/get_token.dart';
|
||||
import 'package:local_spend/common/functions/save_logout.dart';
|
||||
import 'package:local_spend/model/json/login_model.dart';
|
||||
|
||||
Future<LoginModel> requestLogoutAPI(BuildContext context) async {
|
||||
final url = "https://www.yoururl.com/logout";
|
||||
|
||||
var token;
|
||||
|
||||
await getToken().then((result) {
|
||||
token = result;
|
||||
});
|
||||
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {HttpHeaders.authorizationHeader: "Token $token"},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
saveLogout();
|
||||
return null;
|
||||
} else {
|
||||
saveLogout();
|
||||
return null;
|
||||
}
|
||||
}
|
8
lib/common/functions/get_token.dart
Normal file
8
lib/common/functions/get_token.dart
Normal file
|
@ -0,0 +1,8 @@
|
|||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
getToken() async {
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
|
||||
String getToken = await preferences.getString("LastToken");
|
||||
return getToken;
|
||||
}
|
30
lib/common/functions/save_current_login.dart
Normal file
30
lib/common/functions/save_current_login.dart
Normal file
|
@ -0,0 +1,30 @@
|
|||
import 'package:local_spend/model/json/login_model.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
saveCurrentLogin(Map responseJson) async {
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
|
||||
var user;
|
||||
if ((responseJson != null && responseJson.isNotEmpty)) {
|
||||
user = LoginModel.fromJson(responseJson).userName;
|
||||
} else {
|
||||
user = "";
|
||||
}
|
||||
var token = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).token
|
||||
: "";
|
||||
var email = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).email
|
||||
: "";
|
||||
var pk = (responseJson != null && responseJson.isNotEmpty)
|
||||
? LoginModel.fromJson(responseJson).userId
|
||||
: 0;
|
||||
|
||||
await preferences.setString(
|
||||
'LastUser', (user != null && user.length > 0) ? user : "");
|
||||
await preferences.setString(
|
||||
'LastToken', (token != null && token.length > 0) ? token : "");
|
||||
await preferences.setString(
|
||||
'LastEmail', (email != null && email.length > 0) ? email : "");
|
||||
await preferences.setInt('LastUserId', (pk != null && pk > 0) ? pk : 0);
|
||||
}
|
10
lib/common/functions/save_logout.dart
Normal file
10
lib/common/functions/save_logout.dart
Normal file
|
@ -0,0 +1,10 @@
|
|||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
saveLogout() async {
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
|
||||
await preferences.setString('LastUser', "");
|
||||
await preferences.setString('LastToken', "");
|
||||
await preferences.setString('LastEmail', "");
|
||||
await preferences.setInt('LastUserId', 0);
|
||||
}
|
25
lib/common/functions/show_dialog_single_button.dart
Normal file
25
lib/common/functions/show_dialog_single_button.dart
Normal file
|
@ -0,0 +1,25 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
void showDialogSingleButton(
|
||||
BuildContext context, String title, String message, String buttonLabel) {
|
||||
// flutter defined function
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
// return object of type Dialog
|
||||
return AlertDialog(
|
||||
title: new Text(title),
|
||||
content: new Text(message),
|
||||
actions: <Widget>[
|
||||
// usually buttons at the bottom of the dialog
|
||||
new FlatButton(
|
||||
child: new Text(buttonLabel),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
72
lib/common/platform/platform_scaffold.dart
Normal file
72
lib/common/platform/platform_scaffold.dart
Normal file
|
@ -0,0 +1,72 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PlatformScaffold extends StatelessWidget {
|
||||
final Key key;
|
||||
final PreferredSizeWidget appBar;
|
||||
final Widget body;
|
||||
final Widget floatingActionButton;
|
||||
final FloatingActionButtonLocation floatingActionButtonLocation;
|
||||
final FloatingActionButtonAnimator floatingActionButtonAnimator;
|
||||
final List<Widget> persistentFooterButtons;
|
||||
final Widget drawer;
|
||||
final Widget endDrawer;
|
||||
final Widget bottomNavigationBar;
|
||||
final Color backgroundColor;
|
||||
final bool resizeToAvoidBottomPadding;
|
||||
final bool primary;
|
||||
|
||||
PlatformScaffold(
|
||||
{this.key,
|
||||
this.appBar,
|
||||
this.body,
|
||||
this.floatingActionButton,
|
||||
this.floatingActionButtonLocation,
|
||||
this.floatingActionButtonAnimator,
|
||||
this.persistentFooterButtons,
|
||||
this.drawer,
|
||||
this.endDrawer,
|
||||
this.bottomNavigationBar,
|
||||
this.backgroundColor,
|
||||
this.resizeToAvoidBottomPadding: true,
|
||||
this.primary: true})
|
||||
: assert(primary != null),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Platform.isIOS
|
||||
? Scaffold(
|
||||
key: key,
|
||||
appBar: appBar,
|
||||
body: body,
|
||||
floatingActionButton: floatingActionButton,
|
||||
persistentFooterButtons: persistentFooterButtons,
|
||||
floatingActionButtonLocation: floatingActionButtonLocation,
|
||||
floatingActionButtonAnimator: floatingActionButtonAnimator,
|
||||
drawer: endDrawer,
|
||||
endDrawer: drawer,
|
||||
bottomNavigationBar: bottomNavigationBar,
|
||||
backgroundColor: backgroundColor,
|
||||
resizeToAvoidBottomPadding: resizeToAvoidBottomPadding,
|
||||
primary: primary,
|
||||
)
|
||||
: Scaffold(
|
||||
key: key,
|
||||
appBar: appBar,
|
||||
body: body,
|
||||
floatingActionButton: floatingActionButton,
|
||||
persistentFooterButtons: persistentFooterButtons,
|
||||
floatingActionButtonLocation: floatingActionButtonLocation,
|
||||
floatingActionButtonAnimator: floatingActionButtonAnimator,
|
||||
drawer: drawer,
|
||||
endDrawer: endDrawer,
|
||||
bottomNavigationBar: bottomNavigationBar,
|
||||
backgroundColor: backgroundColor,
|
||||
resizeToAvoidBottomPadding: resizeToAvoidBottomPadding,
|
||||
primary: primary,
|
||||
);
|
||||
}
|
||||
}
|
54
lib/common/widgets/basic_drawer.dart
Normal file
54
lib/common/widgets/basic_drawer.dart
Normal file
|
@ -0,0 +1,54 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_spend/common/apifunctions/request_logout_api.dart';
|
||||
|
||||
class BasicDrawer extends StatefulWidget {
|
||||
@override
|
||||
_BasicDrawerState createState() => _BasicDrawerState();
|
||||
}
|
||||
|
||||
class _BasicDrawerState extends State<BasicDrawer> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
child: Container(
|
||||
padding: new EdgeInsets.all(32.0),
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
title: Text(
|
||||
"Submit Receipt",
|
||||
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
||||
),
|
||||
onTap: () {
|
||||
requestLogoutAPI(context);
|
||||
Navigator.of(context).pushNamed('/ReceiptPage');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text(
|
||||
"About",
|
||||
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
||||
),
|
||||
onTap: () {
|
||||
SystemChannels.textInput.invokeMethod('TextInput.hide');
|
||||
// Here I have not implemented an actual about screen, but if you did you would navigate to it's route
|
||||
// Navigator.of(context).pushReplacementNamed('/AboutScreen');
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text(
|
||||
"Logout",
|
||||
style: TextStyle(color: Colors.black, fontSize: 20.0),
|
||||
),
|
||||
onTap: () {
|
||||
requestLogoutAPI(context);
|
||||
Navigator.of(context).pushReplacementNamed('/LoginPage');
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in a new issue