Skip to content

Instantly share code, notes, and snippets.

@mkodekar
Created March 31, 2018 06:57
Show Gist options
  • Save mkodekar/29e2de53b47058fb3a96467b0290d6bc to your computer and use it in GitHub Desktop.
Save mkodekar/29e2de53b47058fb3a96467b0290d6bc to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:redux/redux.dart';
import 'package:chatmetricsproject/models/models.dart' show Login, AppState;
import 'package:chatmetricsproject/widgetutils/widgetutils.dart' show WidgetUtils;
class LoginRequest {
}
class LoginSuccess{
final int success;
final String message;
final Login login;
LoginSuccess(this.success, this.message, this.login);
}
class LoginError {
final int success;
final String message;
LoginError(this.success, this.message);
}
final login = (BuildContext context, String email, String password) {
return (Store<AppState> store) {
WidgetUtils.showLoadingDialog(context, 'Signing');
store.dispatch(new LoginRequest());
var url = 'https://testmetrics.webautodev.com/api/web/v1/site/login';
var client = new http.Client();
client.post(url, body: {
'email': email,
'password': password
}).then((response) => JSON.decode(response.body)).then((value) {
if (value['success'] == 1) {
var login = new Login.fromJson(value);
Navigator.pop(context);
store.dispatch(new LoginSuccess(login.success, login.message, login));
client.close();
WidgetUtils.showSuccess(context, 'Login', login.message);
} else {
Navigator.pop(context);
store.dispatch(new LoginError(value['success'], value['message']));
client.close();
WidgetUtils.showError(context, 'Login Error', value['message']);
}
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment