Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Furqankhanzada/Flutter_E-Commerce…
Browse files Browse the repository at this point in the history
…_UI_KIT
  • Loading branch information
Furqankhanzada committed Oct 28, 2019
2 parents 0255061 + 777578f commit 0667547
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
10 changes: 5 additions & 5 deletions lib/auth/signin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class _SignInState extends State<SignIn> {
final UserCredential userCredential = UserCredential();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
return Center(
child: Form(
key: _formKey,
child: SingleChildScrollView(
Expand Down Expand Up @@ -73,10 +72,12 @@ class _SignInState extends State<SignIn> {
return RaisedButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: Text('Sign In'),
child: auth.loading && auth.loadingType == 'login' ? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
) : Text('Sign In'),
onPressed: () {
// Validate form
if (_formKey.currentState.validate()) {
if (_formKey.currentState.validate() && !auth.loading) {
// Update values
_formKey.currentState.save();
// Hit Api
Expand All @@ -93,7 +94,6 @@ class _SignInState extends State<SignIn> {
),
),
),
),
);
}
}
10 changes: 5 additions & 5 deletions lib/auth/signup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class _SignUpState extends State<SignUp> {
String confirmPassword;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
return Center(
child: Form(
key: _formKey,
child: SingleChildScrollView(
Expand Down Expand Up @@ -115,9 +114,11 @@ class _SignUpState extends State<SignUp> {
return RaisedButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: Text('Sign Up'),
child: auth.loading && auth.loadingType == 'register' ? CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
) : Text('Sign Up'),
onPressed: () {
if (_formKey.currentState.validate()) {
if (_formKey.currentState.validate() && !auth.loading) {
_formKey.currentState.save();
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
Expand All @@ -133,7 +134,6 @@ class _SignUpState extends State<SignUp> {
),
),
),
),
);
}
}
17 changes: 15 additions & 2 deletions lib/blocks/auth_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ class AuthBlock extends ChangeNotifier {

// Loading
bool _loading = false;
String _loadingType;
bool get loading => _loading;
String get loadingType => _loadingType;
set loading(bool loadingState) {
_loading = loadingState;
notifyListeners();
}

set loadingType(String loadingType) {
_loadingType = loadingType;
notifyListeners();
}
// Loading
bool _isLoggedIn = false;
bool get isLoggedIn => _isLoggedIn;
Expand All @@ -41,13 +46,21 @@ class AuthBlock extends ChangeNotifier {
}

login(UserCredential userCredential) async {
loading = true;
loadingType = 'login';
await _authService.login(userCredential);
setUser();
loading = false;
}

register(User user) async {
loading = true;
loadingType = 'register';
await _authService.register(user);
loading = false;
}
logout() async{

logout() async {
await _authService.logout();
isLoggedIn = false;
notifyListeners();
Expand Down
34 changes: 27 additions & 7 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_scaffold/config.dart';
import 'package:flutter_scaffold/models/user.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart' as http;
import 'package:fluttertoast/fluttertoast.dart';

class AuthService {
final storage = FlutterSecureStorage();
Expand All @@ -20,24 +21,43 @@ class AuthService {
setUser(response.body);
return jsonDecode(response.body);
} else {
if (response.statusCode == 403) {
Fluttertoast.showToast(
msg: "Invalid Credentials",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
fontSize: 16.0);
}
// If that call was not successful, throw an error.
throw Exception(response.body);
// throw Exception(response.body);
return jsonDecode(response.body);
}
}

Future<Map> register(User user) async {
final response = await http.post('$BASE_URL/tradebakerz/wc/v1/register', body: {
'username': user.username,
'password': user.password,
'email': user.email
});
final response = await http.post('$BASE_URL/tradebakerz/wc/v1/register',
body: {
'username': user.username,
'password': user.password,
'email': user.email
});
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON.
// return User.fromJson(json.decode(response.body));
return jsonDecode(response.body);
} else {
if (response.statusCode == 400) {
Fluttertoast.showToast(
msg: 'Email already exist',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
fontSize: 16.0);
}
// If that call was not successful, throw an error.
throw Exception(response.body);
// throw Exception(response.body);
return jsonDecode(response.body);
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
http: ^0.12.0+2
provider: ^3.1.0+1
flutter_secure_storage: ^3.3.1+1

fluttertoast: ^3.1.3

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit 0667547

Please sign in to comment.