Skip to content

Commit

Permalink
City stored in shared pref, shows in appbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinmay-KB committed Apr 26, 2020
1 parent ef49afe commit 8f6cce6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
44 changes: 44 additions & 0 deletions gogrocy/lib/core/models/Address.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Address {
String recipent;
String address_id;
String city;
String contact;
String country;
String is_primary;
String locality;
String state;
String user_id;
String zip;

Address({this.recipent, this.address_id, this.city, this.contact, this.country, this.is_primary, this.locality, this.state, this.user_id, this.zip});

factory Address.fromJson(Map<String, dynamic> json) {
return Address(
recipent: json['recipent'],
address_id: json['address_id'],
city: json['city'],
contact: json['contact'],
country: json['country'],
is_primary: json['is_primary'],
locality: json['locality'],
state: json['state'],
user_id: json['user_id'],
zip: json['zip'],
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['recipent'] = this.recipent;
data['address_id'] = this.address_id;
data['city'] = this.city;
data['contact'] = this.contact;
data['country'] = this.country;
data['is_primary'] = this.is_primary;
data['locality'] = this.locality;
data['state'] = this.state;
data['user_id'] = this.user_id;
data['zip'] = this.zip;
return data;
}
}
27 changes: 26 additions & 1 deletion gogrocy/lib/core/services/api.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:gogrocy/core/models/Address.dart';
import 'package:gogrocy/core/models/cart_edit.dart';
import 'package:gogrocy/core/models/cart_list.dart';
import 'package:gogrocy/core/models/product.dart';
Expand All @@ -24,6 +25,7 @@ const String verifyUser = baseUrl + "verifyUser";
const String addAddress = baseUrl + "add_address";
const String cartList = baseUrl + 'getCartItems';
const String editCart=baseUrl+ "add_to_cart";
const String getAddress=baseUrl+"getAddress";

class Apis {
final SharedPrefsService _sharedPrefsService = locator<SharedPrefsService>();
Expand Down Expand Up @@ -131,12 +133,13 @@ class Apis {
"product_id":product_id,
"quantity":quantity
};
String jwt=await _sharedPrefsService.getJWT();
var client = new http.Client();
bool connectionState = await checkStatus();
if (connectionState) //TODO: Add a proper else return
{
var response = await client.post(editCart, headers: {
'Authorization': 'Bearer $TOKEN',
'Authorization': 'Bearer $jwt',
},body: body);
return CartEdit.fromJson(json.decode(response.body));
} else
Expand All @@ -161,6 +164,28 @@ class Apis {
}
}

Future<List<Address>> getAddresses() async{
var client = new http.Client();
bool connectionState = await checkStatus();
String jwt=await _sharedPrefsService.getJWT();
if (connectionState) //TODO: Add a proper else return
{
var address = List<Address>();
var response = await client.post(getAddress, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $jwt',
});
var parsed = json.decode(response.body) as List<dynamic>;
for (var product in parsed) {
address.add(Address.fromJson(product));
}
return address;
} else
(print("Network failure"));
}


Future<cart_list> getCartList() async {
var client = new http.Client();
bool connectionState = await checkStatus();
Expand Down
2 changes: 1 addition & 1 deletion gogrocy/lib/core/services/bottom_appbar_provider.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

class BottomNavBarProvider with ChangeNotifier{
int _currentIndex=0;
int _currentIndex=1;

get currentIndex=> _currentIndex;

Expand Down
22 changes: 22 additions & 0 deletions gogrocy/lib/core/services/shared_prefs.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:gogrocy/core/models/Address.dart';
import 'package:gogrocy/service_locator.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'api.dart';

class SharedPrefsService {

Future<bool> setJWT(String s) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString("jwt", s);
Expand Down Expand Up @@ -30,4 +35,21 @@ class SharedPrefsService {
return prefs.getString('cart');
}

Future<bool> setCity(String s)async{
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString("city", s);
}

Future<String> getCity() async{
final Apis apis = locator<Apis>();
final SharedPreferences prefs = await SharedPreferences.getInstance();
String city=prefs.getString("city");
if(city==null){
List<Address> address=await apis.getAddresses();
setCity(address[0].city);
return address[0].city;
}
else return city;
}

}
2 changes: 1 addition & 1 deletion gogrocy/lib/ui/views/view_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ViewCarousel extends StatelessWidget {
Widget build(BuildContext context) {
var provider = Provider.of<BottomNavBarProvider>(context);
PageController controller =
PageController(initialPage: provider.currentIndex);
PageController(initialPage: provider.currentIndex,keepPage: true);
Provider.of<BottomNavBarProvider>(context, listen: true);
provider.addListener(() {
controller.animateToPage(provider.currentIndex,
Expand Down
24 changes: 18 additions & 6 deletions gogrocy/lib/ui/widgets/appbar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gogrocy/core/services/shared_prefs.dart';
import 'package:gogrocy/service_locator.dart';
import 'package:gogrocy/ui/shared/constants.dart' as constants;

class TopAppBar extends StatelessWidget implements PreferredSizeWidget {
final SharedPrefsService sharedPrefsService = locator<SharedPrefsService>();
@override
Widget build(BuildContext context) {
constants.mediaQueryData = MediaQuery.of(context);
Expand Down Expand Up @@ -33,12 +37,20 @@ class TopAppBar extends StatelessWidget implements PreferredSizeWidget {
color: Colors.green,
fontWeight: FontWeight.w500),
),
Text(
"Sector 9",
style: TextStyle(
fontSize: constants.AppBarConfig.addressFontSize,
color: Colors.black,
fontWeight: FontWeight.w500),
FutureBuilder(
future: sharedPrefsService.getCity(),
builder: (context, snapshot){
if(snapshot.connectionState!=ConnectionState.done)
return SizedBox(width:8,height: 8,child: Center(child: CircularProgressIndicator(strokeWidth: 1.5,),));
else
return Text(
snapshot.data,
style: TextStyle(
fontSize: constants.AppBarConfig.addressFontSize,
color: Colors.black,
fontWeight: FontWeight.w500),
);
},
)
],
),
Expand Down
1 change: 1 addition & 0 deletions gogrocy/lib/ui/widgets/bottom_navbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BottomNavBar extends StatelessWidget {
var provider = Provider.of<BottomNavBarProvider>(context);
return FancyBottomNavigation(
barBackgroundColor: Colors.black,
initialSelection: 1,
inactiveIconColor: Colors.grey,
textColor: Colors.white,
tabs: [
Expand Down

0 comments on commit 8f6cce6

Please sign in to comment.