parent
dfbd608f8c
commit
1a53d7b583
@ -1,31 +1,28 @@ |
||||
import 'package:flutter/material.dart'; |
||||
import 'package:thesis_shop/models/product.dart'; |
||||
import 'package:flutter_riverpod/flutter_riverpod.dart'; |
||||
import 'package:thesis_shop/stores/product_list_provider.dart'; |
||||
import 'package:thesis_shop/widgets/user_switch.dart'; |
||||
|
||||
import 'cart_button_overlay.dart'; |
||||
import 'product_list.dart'; |
||||
|
||||
const _productPlaceholder = [ |
||||
Product(title: 'Bananen', price: 3), |
||||
Product(title: 'Äpfel', price: 2), |
||||
Product(title: 'Birnen', price: 2.5), |
||||
Product(title: 'Kirschen', price: 1.2), |
||||
]; |
||||
|
||||
class ProductListScreen extends StatelessWidget { |
||||
class ProductListScreen extends ConsumerWidget { |
||||
const ProductListScreen({Key? key}) : super(key: key); |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
const products = _productPlaceholder; |
||||
Widget build(BuildContext context, ref) { |
||||
return Scaffold( |
||||
appBar: AppBar( |
||||
title: const Text('Thesis Shop'), |
||||
actions: [UserSwitch(isOn: true, onChanged: (_) {})], |
||||
), |
||||
body: const CartButtonOverlay( |
||||
child: ProductList(products: products), |
||||
actions: const [UserSwitch()], |
||||
), |
||||
body: ref.watch(productListProvider).when( |
||||
data: (products) => CartButtonOverlay( |
||||
child: ProductList(products: products), |
||||
), |
||||
error: (error, _) => Center(child: Text(error.toString())), |
||||
loading: () => const Center(child: CircularProgressIndicator()), |
||||
), |
||||
); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,13 @@ |
||||
import 'package:flutter_riverpod/flutter_riverpod.dart'; |
||||
import 'package:thesis_shop/models/product.dart'; |
||||
import 'package:thesis_shop/stores/product_loading_provider.dart'; |
||||
import 'package:thesis_shop/stores/user_store.dart'; |
||||
|
||||
final productListProvider = Provider<AsyncValue<List<Product>>>((ref) { |
||||
final isSignedIn = ref.watch(userStoreProvider); |
||||
if (!isSignedIn) { |
||||
return ref.watch(productLoadingProvider); |
||||
} |
||||
return ref.watch(productLoadingProvider).whenData( |
||||
(products) => products.map((pro) => pro.copyWithDiscount()).toList()); |
||||
}); |
@ -0,0 +1,6 @@ |
||||
import 'package:flutter_riverpod/flutter_riverpod.dart'; |
||||
import 'package:thesis_shop/stores/product_service_provider.dart'; |
||||
|
||||
final productLoadingProvider = FutureProvider( |
||||
(ref) => ref.read(productServiceProvider).fetchProducts(), |
||||
dependencies: [productServiceProvider]); |
@ -0,0 +1,16 @@ |
||||
import 'package:flutter_riverpod/flutter_riverpod.dart'; |
||||
import 'package:thesis_shop/models/product.dart'; |
||||
import 'package:thesis_shop/service/product_service.dart'; |
||||
|
||||
class ProductServiceImplementationPlaceholder implements ProductService { |
||||
@override |
||||
Future<List<Product>> fetchProducts() { |
||||
throw UnimplementedError(); |
||||
} |
||||
|
||||
@override |
||||
String get url => throw UnimplementedError(); |
||||
} |
||||
|
||||
final productServiceProvider = Provider<ProductService>( |
||||
(ref) => ProductServiceImplementationPlaceholder()); |
@ -0,0 +1,12 @@ |
||||
import 'package:riverpod/riverpod.dart'; |
||||
|
||||
class UserStore extends StateNotifier<bool> { |
||||
UserStore({bool initialSignInStatus = false}) : super(initialSignInStatus); |
||||
|
||||
void changeSignInStatus(bool newStatus) { |
||||
state = newStatus; |
||||
} |
||||
} |
||||
|
||||
final userStoreProvider = |
||||
StateNotifierProvider<UserStore, bool>((ref) => UserStore()); |
Loading…
Reference in new issue