Skip to content

Commit

Permalink
Add service to hold data while navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Nov 13, 2021
1 parent 9d08169 commit 0888b14
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 47 deletions.
2 changes: 2 additions & 0 deletions lib/core/locator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:get_it/get_it.dart';
import 'package:logger/logger.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:zerosandones/core/services/temp.dart';
import 'logger.dart';

final GetIt locator = GetIt.instance;
Expand All @@ -14,5 +15,6 @@ class LocatorInjector {
locator.registerLazySingleton(() => DialogService());
log.d('Registering Snackbar Service');
locator.registerLazySingleton(() => SnackbarService());
locator.registerLazySingleton(() => FoodDetailHolder());
}
}
18 changes: 18 additions & 0 deletions lib/core/services/temp.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:stacked/stacked.dart';

class FoodDetailHolder extends BaseViewModel {
late String _foodTag;
late String _foodImagePath;
late String _foodName;

String get foodTag => _foodTag;
String get foodImagePath => _foodImagePath;
String get foodName => _foodName;

setAllProperties(String foodTag, String foodImagePath, String foodName) {
_foodImagePath = foodImagePath;
_foodTag = foodTag;
_foodName = foodName;
notifyListeners();
}
}
87 changes: 49 additions & 38 deletions lib/views/home_page/home_page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,57 @@ class HomePageView extends StatelessWidget {
physics: const BouncingScrollPhysics(),
itemCount: mockFood.length,
itemBuilder: (context, index) {
return Container(
width: size.width * 0.35,
height: size.height * 0.1,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: AppTheme.primaryColor,
borderRadius:
BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: AppTheme.primaryColor
.withOpacity(0.75),
spreadRadius: 1,
blurRadius: 5,
offset: const Offset(2, 4.5),
)
],
),
child: Column(
children: [
SizedBox(
height: size.height * 0.025,
),
Hero(
tag:
'${mockFood[index].imageLink}-$index,',
child: Image.asset(
mockFood[index].imageLink,
final data = mockFood[index];
return GestureDetector(
onTap: () {
viewModel.setFoodHolderProps(
foodImagePath: data.imageLink,
foodName: data.name,
foodTag:
'${mockFood[index].imageLink}-$index,');
viewModel.navigateItemDetailPage();
},
child: Container(
width: size.width * 0.35,
height: size.height * 0.1,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: AppTheme.primaryColor,
borderRadius:
BorderRadius.circular(20.0),
boxShadow: [
BoxShadow(
color: AppTheme.primaryColor
.withOpacity(0.75),
spreadRadius: 1,
blurRadius: 5,
offset: const Offset(2, 4.5),
)
],
),
child: Column(
children: [
SizedBox(
height: size.height * 0.025,
),
Hero(
tag:
'${mockFood[index].imageLink}-$index,',
child: Image.asset(
data.imageLink,
),
),
),
const Spacer(),
Text(
mockFood[index].name,
style: const TextStyle(
color: Colors.white,
fontSize: 16.0,
const Spacer(),
Text(
data.name,
style: const TextStyle(
color: Colors.white,
fontSize: 16.0,
),
),
),
],
],
),
),
);
},
Expand Down
16 changes: 12 additions & 4 deletions lib/views/home_page/home_page_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:stacked_services/stacked_services.dart';
import 'package:zerosandones/core/locator.dart';
import 'package:zerosandones/core/logger.dart';
import 'package:zerosandones/core/models/user_location.dart';
import 'package:zerosandones/core/services/temp.dart';
import 'package:zerosandones/views/item_detail_page/item_detail_page_view.dart';

class HomePageViewModel extends BaseViewModel {
Expand All @@ -19,6 +20,7 @@ class HomePageViewModel extends BaseViewModel {
}

final _navigationService = locator<NavigationService>();
final foodDetailHolder = locator<FoodDetailHolder>();

late UserLocation _currentLocation;
var location = Location();
Expand Down Expand Up @@ -78,11 +80,17 @@ class HomePageViewModel extends BaseViewModel {
address += "${place.locality}";
}

navigateItemDetailPage(String imageTag) async {
_navigationService.replaceWithTransition(
setFoodHolderProps(
{required String foodTag,
required String foodImagePath,
required String foodName}) {
foodDetailHolder.setAllProperties(foodTag, foodImagePath, foodName);
}

navigateItemDetailPage() async {
_navigationService.navigateWithTransition(
ItemDetailPageView(),
duration: const Duration(milliseconds: 600),
transitionClass: Transition.rightToLeftWithFade,
duration: const Duration(milliseconds: 500),
popGesture: false,
);
}
Expand Down
35 changes: 30 additions & 5 deletions lib/views/item_detail_page/item_detail_page_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,41 @@ import 'package:stacked/stacked.dart';
import 'item_detail_page_view_model.dart';

class ItemDetailPageView extends StatelessWidget {
// final String imageTag;
// final String imagePath;
// const ItemDetailPageView(
// {Key? key,
// this.imageTag = "",
// this.imagePath = ""}) // ! TODO Might cause a crash here. Fix it later
// : super(key: key);

@override
Widget build(BuildContext context) {
Size _size = MediaQuery.of(context).size;
return ViewModelBuilder<ItemDetailPageViewModel>.reactive(
builder: (BuildContext context, ItemDetailPageViewModel viewModel, _) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Hero(
tag: "tag",
child: Image.asset("name"),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: _size.height * 0.08,
),
Align(
alignment: Alignment.center,
child: Hero(
tag: viewModel.foodTag,
child: Image.asset(viewModel.foodImagePath),
),
),
Text(
viewModel.foodName,
style: const TextStyle(
fontWeight: FontWeight.w700, fontSize: 24.0),
)
],
),
),
);
Expand Down
6 changes: 6 additions & 0 deletions lib/views/item_detail_page/item_detail_page_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import 'package:logger/logger.dart';
import 'package:stacked/stacked.dart';
import 'package:zerosandones/core/locator.dart';
import 'package:zerosandones/core/logger.dart';
import 'package:zerosandones/core/services/temp.dart';

class ItemDetailPageViewModel extends BaseViewModel {
late Logger log;

ItemDetailPageViewModel() {
log = getLogger(runtimeType.toString());
}
final _foodDetailHolder = locator<FoodDetailHolder>();
String get foodName => _foodDetailHolder.foodName;
String get foodImagePath => _foodDetailHolder.foodImagePath;
String get foodTag => _foodDetailHolder.foodTag;
}

0 comments on commit 0888b14

Please sign in to comment.