2019-05-08 18:54:14 +00:00
|
|
|
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';
|
|
|
|
|
2019-08-12 14:18:01 +00:00
|
|
|
Future<void> _incorrectDialog(BuildContext context) async {
|
|
|
|
return showDialog<void>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: true,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
title: Text("Unable to Login"),
|
|
|
|
content: Text("Incorrect login details. Please try again."),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
|
|
|
child: Text('OK'),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2019-07-16 11:09:10 +00:00
|
|
|
// debugPrint('$body');
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
final response = await http.post(
|
|
|
|
url,
|
2019-05-09 11:52:25 +00:00
|
|
|
body: json.encode(body),
|
2019-05-08 18:54:14 +00:00
|
|
|
);
|
|
|
|
|
2019-07-16 11:09:10 +00:00
|
|
|
// debugPrint(response.body);
|
2019-05-08 18:54:14 +00:00
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
final responseJson = json.decode(response.body);
|
|
|
|
var user = new LoginModel.fromJson(responseJson);
|
|
|
|
|
2019-05-10 11:43:45 +00:00
|
|
|
saveCurrentLogin(responseJson, body["email"]);
|
2019-05-08 18:54:14 +00:00
|
|
|
Navigator.of(context).pushReplacementNamed('/HomePage');
|
|
|
|
|
|
|
|
return LoginModel.fromJson(responseJson);
|
|
|
|
} else {
|
2019-07-16 11:09:10 +00:00
|
|
|
// debugPrint("Invalid, either credentials are wrong or server is down");
|
2019-07-15 11:09:10 +00:00
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
final responseJson = json.decode(response.body);
|
|
|
|
|
2019-05-10 11:43:45 +00:00
|
|
|
saveCurrentLogin(responseJson, body["email"]);
|
2019-07-16 11:09:10 +00:00
|
|
|
|
2019-08-12 14:18:01 +00:00
|
|
|
_incorrectDialog(context);
|
2019-07-16 11:09:10 +00:00
|
|
|
|
2019-05-08 18:54:14 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|