Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Feature/restauran tour rafael kawagoe #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/Modules/Details/view/detail_injection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:restaurantour/Modules/Details/view/detail_view.dart';
import 'package:restaurantour/Modules/Details/viewmodel/detail_viewmodel.dart';
import 'package:restaurantour/adapter/shared_preferences/shared_preferences_adapter.dart';
import 'package:restaurantour/adapter/shared_preferences/shared_preferences_service.dart';
import 'package:restaurantour/models/restaurant.dart';

class DetailInjection extends StatelessWidget {
const DetailInjection({
Key? key,
required this.restaurant,
required this.favoriteRestaurants,
}) : super(key: key);

final Restaurant restaurant;
final RestaurantQueryResult favoriteRestaurants;

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => DetailViewmodel(
restaurant: restaurant,
favoriteRestaurants: favoriteRestaurants,
sharedPreferencesService: SharedPreferencesService(
SharedpreferencesAdapter(),
),
),
child: const DetailView(),
);
}
}
171 changes: 171 additions & 0 deletions lib/Modules/Details/view/detail_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:restaurantour/Modules/Details/view/widgets/review_card.dart';
import 'package:restaurantour/Modules/Details/viewmodel/detail_state.dart';
import 'package:restaurantour/Modules/Details/viewmodel/detail_viewmodel.dart';
import 'package:restaurantour/Modules/Home/tabs/widgets/categorie_text.dart';
import 'package:restaurantour/Modules/Home/tabs/widgets/is_open_label.dart';
import 'package:restaurantour/Modules/Home/tabs/widgets/price_text.dart';

class DetailView extends StatelessWidget {
const DetailView({
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final DetailViewmodel detailViewmodel =
BlocProvider.of<DetailViewmodel>(context);

return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
detailViewmodel.restaurant.name!,
overflow: TextOverflow.ellipsis,
),
),
actions: [
BlocBuilder<DetailViewmodel, DetailState>(
builder: (context, state) {
return IconButton(
onPressed: () async {
await detailViewmodel.invertFavoriteValue();
await detailViewmodel.addtoFavorites();
},
icon: (detailViewmodel.restaurant.isFavorite)
? const Icon(Icons.favorite)
: const Icon(Icons.favorite_outline),
);
},
),
],
),
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
width: double.infinity,
height: 300,
child: detailViewmodel.restaurant.photos != null
? Image.network(
detailViewmodel.restaurant.photos!.first,
fit: BoxFit.cover,
)
: const Text('no photo'),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
PriceText(
text: detailViewmodel.restaurant.price,
// text: 'homeViewmodel.restaurants.restaurants![index].price',
),
CategorieText(
text: detailViewmodel.restaurant.categories?[0].title,
),
const Expanded(
child: IsOpenLabel(
isOpen: true,
),
),
// 'text: homeViewmodel.restaurants.restaurants![index].categories?[0].title')
],
),
const SizedBox(height: 32),
Container(
width: double.infinity,
height: 1,
color: Colors.grey.withOpacity(0.2),
),
const SizedBox(height: 32),
const Text('Adress'),
const SizedBox(height: 32),
Text(
detailViewmodel.restaurant.location?.formattedAddress ?? '',
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 32),
Container(
width: double.infinity,
height: 1,
color: Colors.grey.withOpacity(0.2),
),
const SizedBox(height: 32),
const Text('Overall Rating'),
const SizedBox(height: 18),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
detailViewmodel.restaurant.rating.toString(),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 32,
),
),
const Padding(
padding: EdgeInsets.only(bottom: 6),
child: Icon(
Icons.star,
size: 14,
color: Color.fromARGB(255, 255, 205, 40),
),
),
],
),
const SizedBox(height: 32),
Container(
width: double.infinity,
height: 1,
color: Colors.grey.withOpacity(0.2),
),
const SizedBox(height: 32),
Text(
detailViewmodel.restaurant.reviews!.length.toString() +
(detailViewmodel.restaurant.reviews!.length > 1
? ' Reviews'
: 'Review'),
),
const SizedBox(height: 32),
SizedBox(
height: (200.toInt() *
detailViewmodel.restaurant.reviews!.length)
.toDouble(),
child: ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: detailViewmodel.restaurant.reviews?.length,
padding: const EdgeInsets.only(top: 10),
itemBuilder: (BuildContext context, int index) {
return Column(
children: [
ReviewCard(
review:
detailViewmodel.restaurant.reviews![index],
),
const SizedBox(height: 18),
Container(
width: double.infinity,
height: 1,
color: Colors.grey.withOpacity(0.2),
),
const SizedBox(height: 32),
],
);
},
),
),
],
),
),
],
),
),
);
}
}
46 changes: 46 additions & 0 deletions lib/Modules/Details/view/widgets/review_card.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:restaurantour/Modules/Home/tabs/widgets/rating_stars_icon.dart';
import 'package:restaurantour/models/restaurant.dart';

class ReviewCard extends StatelessWidget {
const ReviewCard({Key? key, required this.review}) : super(key: key);
final Review review;

@override
Widget build(BuildContext context) {
return Column(
children: [
RatingStarsIcon(
amount: review.rating!.toDouble(),
),
const SizedBox(height: 10),
const Text(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ornare sit amet nisl vitae pellentesque. Suspendisse libero est, pulvinar ut molestie dignissim, aliquet vel neque.',
style: TextStyle(
fontWeight: FontWeight.w300,
fontSize: 14,
),
overflow: TextOverflow.ellipsis,
maxLines: 4,
),
const SizedBox(height: 8),
Row(
children: [
const CircleAvatar(
radius: 24, // Image radius
),
const SizedBox(width: 8),
Text(
review.user!.name!,
style: const TextStyle(
fontWeight: FontWeight.w400,
fontSize: 12,
),
),
],
),
],
);
}
}
12 changes: 12 additions & 0 deletions lib/Modules/Details/viewmodel/detail_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
abstract class DetailState {}

class InitialState extends DetailState {}

class LoadingState extends DetailState {}

class LoadedState extends DetailState {}

class ErrorState extends DetailState {
ErrorState(this.message);
final String message;
}
47 changes: 47 additions & 0 deletions lib/Modules/Details/viewmodel/detail_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:restaurantour/Modules/Details/viewmodel/detail_state.dart';
import 'package:restaurantour/adapter/shared_preferences/shared_preferences_keys.dart';
import 'package:restaurantour/adapter/shared_preferences/shared_preferences_service.dart';
import 'package:restaurantour/models/favorites.dart';
import 'package:restaurantour/models/restaurant.dart';

class DetailViewmodel extends Cubit<DetailState> {
DetailViewmodel({
required this.restaurant,
required this.favoriteRestaurants,
required this.sharedPreferencesService,
}) : super(InitialState());

final Restaurant restaurant;
RestaurantQueryResult favoriteRestaurants;
final SharedPreferencesService sharedPreferencesService;
Favorites fav = Favorites(ids: ['']);

invertFavoriteValue() {
emit(LoadingState());
restaurant.isFavorite = !restaurant.isFavorite;
emit(LoadedState());
}

Future<void> addtoFavorites() async {
try {
emit(LoadingState());
fav = Favorites.fromJson(
await sharedPreferencesService
.get(SharedPreferencesKeys.favoriteRestaurants),
);
if (fav.ids.contains(restaurant.id)) {
fav.ids.remove(restaurant.id);
} else {
fav.ids.add(restaurant.id!);
}
await sharedPreferencesService.set(
SharedPreferencesKeys.favoriteRestaurants,
Favorites.toJson(fav),
);
emit(LoadedState());
} catch (e) {
emit(ErrorState('something went wrong'));
}
}
}
Loading