-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
357 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export 'screen/basket_screen.dart'; | ||
export 'basket_screen.dart'; | ||
export 'state/basket_scope.dart'; | ||
export 'state/basket_state.dart'; | ||
export 'widgets/widgets.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import '../../repositories/fruit.dart'; | ||
import 'basket_scope.dart'; | ||
|
||
class ProductOrder { | ||
ProductOrder({ | ||
required this.fruit, | ||
required this.quantity, | ||
}); | ||
|
||
final Fruit fruit; | ||
final int quantity; | ||
|
||
double get total => fruit.price * quantity; | ||
} | ||
|
||
class BasketState extends ChangeNotifier { | ||
BasketState({ | ||
Map<Fruit, ProductOrder>? data, | ||
}) : store = data ?? {}; | ||
|
||
Map<Fruit, ProductOrder> store; | ||
|
||
double get subTotal { | ||
return store.values.fold( | ||
0, | ||
(previousValue, element) => previousValue + element.total, | ||
); | ||
} | ||
|
||
double get delivery => 0.56; | ||
|
||
static BasketState of(BuildContext context) { | ||
return context.dependOnInheritedWidgetOfExactType<BasketScope>()!.notifier!; | ||
} | ||
|
||
void addFruit(Fruit fruit) { | ||
store.update( | ||
fruit, | ||
(value) => ProductOrder( | ||
fruit: fruit, | ||
quantity: value.quantity + 1, | ||
), | ||
ifAbsent: () => ProductOrder( | ||
fruit: fruit, | ||
quantity: 1, | ||
), | ||
); | ||
|
||
notifyListeners(); | ||
} | ||
|
||
void removeFruit(Fruit fruit) { | ||
if (!store.containsKey(fruit)) return; | ||
|
||
store.update( | ||
fruit, | ||
(value) => ProductOrder( | ||
fruit: fruit, | ||
quantity: value.quantity - 1, | ||
), | ||
); | ||
|
||
if (store[fruit]!.quantity <= 0) { | ||
store.remove(fruit); | ||
} | ||
|
||
notifyListeners(); | ||
} | ||
} |
Oops, something went wrong.