Skip to content

Commit

Permalink
Merge pull request #22 from gohealthnow/18-pagamento-e-exibio-de-paga…
Browse files Browse the repository at this point in the history
…mento

Pagamento e exibição de pagamento
  • Loading branch information
Polabiel authored Sep 12, 2024
2 parents 5e24861 + 0b5b893 commit ffd4701
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 20 deletions.
62 changes: 62 additions & 0 deletions lib/api/services/shared_local_storage_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:gohealth/api/interfaces/local_storage_interface.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/models/user_models.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand Down Expand Up @@ -77,4 +78,65 @@ class SharedLocalStorageService implements ILocalStorage {
value.clear();
});
}

void saveProduct(ProductModels productModels) {
var shared = SharedPreferences.getInstance();
shared.then((value) {
var products = value.getString('products');
List<ProductModels> list = [];
if (products != null) {
var decoded = jsonDecode(products) as List;
list = decoded.map((e) => ProductModels.fromJson(e)).toList();
}
list.add(productModels);
value.setString('products', jsonEncode(list));
});
}

Future<List<ProductModels>> getAllProducts() {
var shared = SharedPreferences.getInstance();
return shared.then((value) {
var products = value.getString('products');
if (products != null) {
var decoded = jsonDecode(products) as List;
return decoded.map((e) => ProductModels.fromJson(e)).toList();
} else {
throw Exception('No products found');
}
});
}

getSelectedItems() {
var shared = SharedPreferences.getInstance();
return shared.then((value) {
var products = value.getString('products');
if (products != null) {
var decoded = jsonDecode(products) as List;
return decoded.map((e) => e.toString()).toList();
} else {
throw Exception('No products found');
}
});
}

void removeProduct(int? id) {
var shared = SharedPreferences.getInstance();
shared.then((value) {
var products = value.getString('products');
List<ProductModels> list = [];
if (products != null) {
var decoded = jsonDecode(products) as List;
list = decoded.map((e) => ProductModels.fromJson(e)).toList();
}
list.removeWhere((element) => element.id == id);
value.setString('products', jsonEncode(list));
});
}

void clearCart() {
var shared = SharedPreferences.getInstance();
shared.then((value) {
value.remove('products');
});
}
}
127 changes: 127 additions & 0 deletions lib/src/app/home/cart/cart_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/services/shared_local_storage_service.dart';
import 'package:gohealth/src/app/home/home_page.dart';
import 'package:gohealth/src/app/sessions/products/product_page.dart';
import 'package:gohealth/src/components/header_bar.dart';
import 'package:gohealth/src/components/side_menu.dart';

class CartPage extends StatefulWidget {
const CartPage({ Key? key }) : super(key: key);

@override
_CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const HeaderBarState(),
drawer: const SideMenu(),
bottomNavigationBar: Padding(
padding: const EdgeInsets.all(8.0),
child: FutureBuilder<List<ProductModels>>(
future: SharedLocalStorageService().getAllProducts(),
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const SizedBox.shrink();
} else {
final total = snapshot.data!.fold<double>(
0.0,
(sum, item) => sum + (item.price ?? 0.0),
);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {
SharedLocalStorageService().clearCart();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Pedido finalizado com sucesso'),
backgroundColor: Colors.green,
duration: Duration(seconds: 2),
),
);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Homepage()));
setState(() {});
},
child: const Text(
'Finalizar Pedido',
style: TextStyle(color: Colors.white),
),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
backgroundColor: Colors.green,
),
),
Text(
'Total: R\$${total.toStringAsFixed(2)}',
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
),
],
);
}
},
),
),
body: FutureBuilder<List<ProductModels>>(
future: SharedLocalStorageService().getAllProducts(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('Nenhum produto no carrinho'));
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
final product = snapshot.data![index];
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
ProductPage(productModels: snapshot.data![index]),
),
);
},
child: Card(
margin: const EdgeInsets.all(10.0),
child: ListTile(
leading: Image.network(product.image ?? "https://via.placeholder.com/150", width: 50, height: 50, fit: BoxFit.cover),
title: Text(product.name!),
subtitle: Text(
'Preço: R\$${product.price!.toStringAsFixed(2)}'),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
setState(() {
snapshot.data!.removeAt(index);
SharedLocalStorageService()
.removeProduct(product.id);
});
},
),
),
),
);
},
);
}
},
),
);
}
}
4 changes: 2 additions & 2 deletions lib/src/app/login/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class LoginPageState extends State<LoginPage> {
print(user.toJson());
}

await Navigator.pushReplacement(
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SplashPage(),
Expand All @@ -205,7 +205,7 @@ class LoginPageState extends State<LoginPage> {
}
},
child: const Text(
'Login',
'Logar',
style: TextStyle(
fontSize: 18.0, // Tamanho do texto
fontWeight: FontWeight.w900, // Deixa o texto em negrito
Expand Down
6 changes: 3 additions & 3 deletions lib/src/app/register/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class RegisterPageState extends State<RegisterPage> {
print(user.toJson());
}

await Navigator.pushReplacement(
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SplashPage(),
Expand All @@ -260,9 +260,9 @@ class RegisterPageState extends State<RegisterPage> {
}
},
child: const Text(
'Register',
'Cadastrar',
style: TextStyle(
fontSize: 18.0, // Tamanho do texto
fontSize: 12.0, // Tamanho do texto
fontWeight: FontWeight.w900, // Deixa o texto em negrito
),
),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/app/sessions/final_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FinalSessionState extends State<FinalSessionPage> {
children: [
TextButton(
onPressed: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SecondSessionPage()));
Expand Down Expand Up @@ -92,7 +92,7 @@ class FinalSessionState extends State<FinalSessionPage> {
),
TextButton(
onPressed: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FinalSessionPage()));
Expand Down Expand Up @@ -135,7 +135,7 @@ class FinalSessionState extends State<FinalSessionPage> {
onPressed: () => {
for (var item in itemSelected)
{_viewModel.addProductInUser(item, _id!)},
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Homepage()))
Expand Down
2 changes: 1 addition & 1 deletion lib/src/app/sessions/first_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FirstSessionState extends State<FirstSessionPage> {
),
TextButton(
onPressed: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SecondSessionPage()));
Expand Down
14 changes: 13 additions & 1 deletion lib/src/app/sessions/products/product_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/models/product_models.dart';
import 'package:gohealth/api/services/shared_local_storage_service.dart';
import 'image_carousel.dart';
import 'product_details.dart';
import 'description_section.dart';
Expand Down Expand Up @@ -44,7 +45,18 @@ class ProductState extends State<ProductPage> {
bottomNavigationBar: Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
onPressed: () {},
onPressed: () {
// Salvar os dados do produto no carrinho atráves do storage
SharedLocalStorageService().saveProduct(widget.productModels);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Produto adicionado ao carrinho'),
backgroundColor: Colors.green,
duration: Duration(seconds: 2),
),
);
Navigator.pop(context);
},
child: Text('Adicionar ao carrinho'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/app/sessions/second_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SecondSessionState extends State<SecondSessionPage> {
children: [
TextButton(
onPressed: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FirstSessionPage()));
Expand Down Expand Up @@ -63,7 +63,7 @@ class SecondSessionState extends State<SecondSessionPage> {
),
TextButton(
onPressed: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FinalSessionPage()));
Expand Down
6 changes: 3 additions & 3 deletions lib/src/app/splash_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ class SplashPageState extends State<SplashPage> {
Future.delayed(const Duration(seconds: 2), () {
if (isLogged) {
if (isFirstSession) {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const FirstSessionPage(),
),
);
} else {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const Homepage(),
),
);
}
} else {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const LoginPage(),
Expand Down
5 changes: 5 additions & 0 deletions lib/src/components/header_bar.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:gohealth/api/services/shared_local_storage_service.dart';
import 'package:gohealth/src/app/home/cart/cart_page.dart';

class HeaderBarState extends StatefulWidget implements PreferredSizeWidget {
const HeaderBarState({super.key});
Expand Down Expand Up @@ -84,6 +85,10 @@ class _HeaderBarState extends State<HeaderBarState> {
],
),
actions: [
IconButton(onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (BuildContext context) => const CartPage()));
}, icon: const Icon(Icons.shopping_cart)),
IconButton(
icon: const Icon(Icons.notifications),
onPressed: () {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/components/side_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SideMenuState extends State<SideMenu> {
ListTile(
title: const Text('Tela Inicial', style: textStyle),
onTap: () {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => const Homepage()));
Expand All @@ -88,7 +88,7 @@ class SideMenuState extends State<SideMenu> {
ListTile(
title: const Text('Maps', style: textStyle),
onTap: () async {
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => const MapsPage()));
Expand All @@ -101,7 +101,7 @@ class SideMenuState extends State<SideMenu> {

await prefs.logout();
_repository.clearProfile();
Navigator.pushReplacement(
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => const SplashPage()));
Expand Down
Loading

0 comments on commit ffd4701

Please sign in to comment.