mirror of
https://github.com/Iconica-Development/flutter_shopping.git
synced 2025-05-19 08:53:46 +02:00
feat: remove listenablebuilders from cart
This commit is contained in:
parent
db1299f22b
commit
1b307e3084
2 changed files with 154 additions and 152 deletions
|
@ -9,6 +9,7 @@ class DefaultShoppingCartItem extends StatelessWidget {
|
|||
const DefaultShoppingCartItem({
|
||||
required this.product,
|
||||
required this.configuration,
|
||||
required this.onItemAddedRemoved,
|
||||
super.key,
|
||||
});
|
||||
|
||||
|
@ -17,12 +18,13 @@ class DefaultShoppingCartItem extends StatelessWidget {
|
|||
|
||||
/// Shopping cart configuration.
|
||||
final ShoppingCartConfig configuration;
|
||||
|
||||
/// Function that is called when an item is added or removed.
|
||||
final Function() onItemAddedRemoved;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var theme = Theme.of(context);
|
||||
return ListenableBuilder(
|
||||
listenable: configuration.service,
|
||||
builder: (context, _) => Padding(
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 20),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.only(top: 3, left: 4, bottom: 3),
|
||||
|
@ -88,8 +90,10 @@ class DefaultShoppingCartItem extends StatelessWidget {
|
|||
Icons.remove,
|
||||
color: Colors.black,
|
||||
),
|
||||
onPressed: () =>
|
||||
configuration.service.removeOneProduct(product),
|
||||
onPressed: () {
|
||||
configuration.service.removeOneProduct(product);
|
||||
onItemAddedRemoved();
|
||||
},
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(2),
|
||||
|
@ -117,6 +121,7 @@ class DefaultShoppingCartItem extends StatelessWidget {
|
|||
),
|
||||
onPressed: () {
|
||||
configuration.service.addProduct(product);
|
||||
onItemAddedRemoved();
|
||||
},
|
||||
),
|
||||
],
|
||||
|
@ -124,7 +129,6 @@ class DefaultShoppingCartItem extends StatelessWidget {
|
|||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import "package:flutter_shopping_cart/src/widgets/default_shopping_cart_item.dar
|
|||
import "package:flutter_shopping_cart/src/widgets/default_sum_bottom_sheet_builder.dart";
|
||||
|
||||
/// Shopping cart screen widget.
|
||||
class ShoppingCartScreen extends StatelessWidget {
|
||||
class ShoppingCartScreen extends StatefulWidget {
|
||||
/// Creates a shopping cart screen.
|
||||
const ShoppingCartScreen({
|
||||
required this.configuration,
|
||||
|
@ -16,26 +16,31 @@ class ShoppingCartScreen extends StatelessWidget {
|
|||
/// Configuration for the shopping cart screen.
|
||||
final ShoppingCartConfig configuration;
|
||||
|
||||
@override
|
||||
State<ShoppingCartScreen> createState() => _ShoppingCartScreenState();
|
||||
}
|
||||
|
||||
class _ShoppingCartScreenState extends State<ShoppingCartScreen> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar:
|
||||
configuration.appBarBuilder?.call(context) ?? const DefaultAppbar(),
|
||||
appBar: widget.configuration.appBarBuilder?.call(context) ??
|
||||
const DefaultAppbar(),
|
||||
body: SafeArea(
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
Padding(
|
||||
padding: configuration.pagePadding,
|
||||
padding: widget.configuration.pagePadding,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
if (configuration.titleBuilder != null) ...{
|
||||
configuration.titleBuilder!(
|
||||
if (widget.configuration.titleBuilder != null) ...{
|
||||
widget.configuration.titleBuilder!(
|
||||
context,
|
||||
configuration.translations.cartTitle,
|
||||
widget.configuration.translations.cartTitle,
|
||||
),
|
||||
} else ...{
|
||||
Padding(
|
||||
|
@ -45,7 +50,7 @@ class ShoppingCartScreen extends StatelessWidget {
|
|||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
configuration.translations.cartTitle,
|
||||
widget.configuration.translations.cartTitle,
|
||||
style: theme.textTheme.titleLarge,
|
||||
textAlign: TextAlign.start,
|
||||
),
|
||||
|
@ -53,31 +58,33 @@ class ShoppingCartScreen extends StatelessWidget {
|
|||
),
|
||||
),
|
||||
},
|
||||
ListenableBuilder(
|
||||
listenable: configuration.service,
|
||||
builder: (context, _) => Column(
|
||||
Column(
|
||||
children: [
|
||||
for (var product in configuration.service.products)
|
||||
configuration.productItemBuilder?.call(
|
||||
for (var product
|
||||
in widget.configuration.service.products)
|
||||
widget.configuration.productItemBuilder?.call(
|
||||
context,
|
||||
product,
|
||||
configuration,
|
||||
widget.configuration,
|
||||
) ??
|
||||
DefaultShoppingCartItem(
|
||||
product: product,
|
||||
configuration: configuration,
|
||||
configuration: widget.configuration,
|
||||
onItemAddedRemoved: () {
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
|
||||
// Additional whitespace at
|
||||
// the bottom to make sure the last
|
||||
// product(s) are not hidden by the bottom sheet.
|
||||
SizedBox(
|
||||
height: configuration.confirmOrderButtonHeight +
|
||||
configuration.sumBottomSheetHeight,
|
||||
height:
|
||||
widget.configuration.confirmOrderButtonHeight +
|
||||
widget.configuration.sumBottomSheetHeight,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -85,7 +92,7 @@ class ShoppingCartScreen extends StatelessWidget {
|
|||
Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: _BottomSheet(
|
||||
configuration: configuration,
|
||||
configuration: widget.configuration,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
@ -106,18 +113,10 @@ class _BottomSheet extends StatelessWidget {
|
|||
Widget build(BuildContext context) => Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListenableBuilder(
|
||||
listenable: configuration.service,
|
||||
builder: (BuildContext context, Widget? child) =>
|
||||
configuration.sumBottomSheetBuilder
|
||||
?.call(context, configuration) ??
|
||||
configuration.sumBottomSheetBuilder?.call(context, configuration) ??
|
||||
DefaultSumBottomSheetBuilder(
|
||||
configuration: configuration,
|
||||
),
|
||||
),
|
||||
ListenableBuilder(
|
||||
listenable: configuration.service,
|
||||
builder: (context, _) =>
|
||||
configuration.confirmOrderButtonBuilder?.call(
|
||||
context,
|
||||
configuration,
|
||||
|
@ -127,7 +126,6 @@ class _BottomSheet extends StatelessWidget {
|
|||
configuration: configuration,
|
||||
onConfirmOrder: configuration.onConfirmOrder,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue