feat: remove listenablebuilders from cart

This commit is contained in:
mike doornenbal 2024-07-09 10:00:46 +02:00
parent db1299f22b
commit 1b307e3084
2 changed files with 154 additions and 152 deletions

View file

@ -9,6 +9,7 @@ class DefaultShoppingCartItem extends StatelessWidget {
const DefaultShoppingCartItem({ const DefaultShoppingCartItem({
required this.product, required this.product,
required this.configuration, required this.configuration,
required this.onItemAddedRemoved,
super.key, super.key,
}); });
@ -17,12 +18,13 @@ class DefaultShoppingCartItem extends StatelessWidget {
/// Shopping cart configuration. /// Shopping cart configuration.
final ShoppingCartConfig configuration; final ShoppingCartConfig configuration;
/// Function that is called when an item is added or removed.
final Function() onItemAddedRemoved;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var theme = Theme.of(context); var theme = Theme.of(context);
return ListenableBuilder( return Padding(
listenable: configuration.service,
builder: (context, _) => Padding(
padding: const EdgeInsets.only(bottom: 20), padding: const EdgeInsets.only(bottom: 20),
child: ListTile( child: ListTile(
contentPadding: const EdgeInsets.only(top: 3, left: 4, bottom: 3), contentPadding: const EdgeInsets.only(top: 3, left: 4, bottom: 3),
@ -88,8 +90,10 @@ class DefaultShoppingCartItem extends StatelessWidget {
Icons.remove, Icons.remove,
color: Colors.black, color: Colors.black,
), ),
onPressed: () => onPressed: () {
configuration.service.removeOneProduct(product), configuration.service.removeOneProduct(product);
onItemAddedRemoved();
},
), ),
Padding( Padding(
padding: const EdgeInsets.all(2), padding: const EdgeInsets.all(2),
@ -117,6 +121,7 @@ class DefaultShoppingCartItem extends StatelessWidget {
), ),
onPressed: () { onPressed: () {
configuration.service.addProduct(product); configuration.service.addProduct(product);
onItemAddedRemoved();
}, },
), ),
], ],
@ -124,7 +129,6 @@ class DefaultShoppingCartItem extends StatelessWidget {
], ],
), ),
), ),
),
); );
} }
} }

View file

@ -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"; import "package:flutter_shopping_cart/src/widgets/default_sum_bottom_sheet_builder.dart";
/// Shopping cart screen widget. /// Shopping cart screen widget.
class ShoppingCartScreen extends StatelessWidget { class ShoppingCartScreen extends StatefulWidget {
/// Creates a shopping cart screen. /// Creates a shopping cart screen.
const ShoppingCartScreen({ const ShoppingCartScreen({
required this.configuration, required this.configuration,
@ -16,26 +16,31 @@ class ShoppingCartScreen extends StatelessWidget {
/// Configuration for the shopping cart screen. /// Configuration for the shopping cart screen.
final ShoppingCartConfig configuration; final ShoppingCartConfig configuration;
@override
State<ShoppingCartScreen> createState() => _ShoppingCartScreenState();
}
class _ShoppingCartScreenState extends State<ShoppingCartScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var theme = Theme.of(context); var theme = Theme.of(context);
return Scaffold( return Scaffold(
appBar: appBar: widget.configuration.appBarBuilder?.call(context) ??
configuration.appBarBuilder?.call(context) ?? const DefaultAppbar(), const DefaultAppbar(),
body: SafeArea( body: SafeArea(
child: Stack( child: Stack(
fit: StackFit.expand, fit: StackFit.expand,
children: [ children: [
Padding( Padding(
padding: configuration.pagePadding, padding: widget.configuration.pagePadding,
child: SingleChildScrollView( child: SingleChildScrollView(
child: Column( child: Column(
children: [ children: [
if (configuration.titleBuilder != null) ...{ if (widget.configuration.titleBuilder != null) ...{
configuration.titleBuilder!( widget.configuration.titleBuilder!(
context, context,
configuration.translations.cartTitle, widget.configuration.translations.cartTitle,
), ),
} else ...{ } else ...{
Padding( Padding(
@ -45,7 +50,7 @@ class ShoppingCartScreen extends StatelessWidget {
child: Row( child: Row(
children: [ children: [
Text( Text(
configuration.translations.cartTitle, widget.configuration.translations.cartTitle,
style: theme.textTheme.titleLarge, style: theme.textTheme.titleLarge,
textAlign: TextAlign.start, textAlign: TextAlign.start,
), ),
@ -53,31 +58,33 @@ class ShoppingCartScreen extends StatelessWidget {
), ),
), ),
}, },
ListenableBuilder( Column(
listenable: configuration.service,
builder: (context, _) => Column(
children: [ children: [
for (var product in configuration.service.products) for (var product
configuration.productItemBuilder?.call( in widget.configuration.service.products)
widget.configuration.productItemBuilder?.call(
context, context,
product, product,
configuration, widget.configuration,
) ?? ) ??
DefaultShoppingCartItem( DefaultShoppingCartItem(
product: product, product: product,
configuration: configuration, configuration: widget.configuration,
onItemAddedRemoved: () {
setState(() {});
},
), ),
// Additional whitespace at // Additional whitespace at
// the bottom to make sure the last // the bottom to make sure the last
// product(s) are not hidden by the bottom sheet. // product(s) are not hidden by the bottom sheet.
SizedBox( SizedBox(
height: configuration.confirmOrderButtonHeight + height:
configuration.sumBottomSheetHeight, widget.configuration.confirmOrderButtonHeight +
widget.configuration.sumBottomSheetHeight,
), ),
], ],
), ),
),
], ],
), ),
), ),
@ -85,7 +92,7 @@ class ShoppingCartScreen extends StatelessWidget {
Align( Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
child: _BottomSheet( child: _BottomSheet(
configuration: configuration, configuration: widget.configuration,
), ),
), ),
], ],
@ -106,18 +113,10 @@ class _BottomSheet extends StatelessWidget {
Widget build(BuildContext context) => Column( Widget build(BuildContext context) => Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
ListenableBuilder( configuration.sumBottomSheetBuilder?.call(context, configuration) ??
listenable: configuration.service,
builder: (BuildContext context, Widget? child) =>
configuration.sumBottomSheetBuilder
?.call(context, configuration) ??
DefaultSumBottomSheetBuilder( DefaultSumBottomSheetBuilder(
configuration: configuration, configuration: configuration,
), ),
),
ListenableBuilder(
listenable: configuration.service,
builder: (context, _) =>
configuration.confirmOrderButtonBuilder?.call( configuration.confirmOrderButtonBuilder?.call(
context, context,
configuration, configuration,
@ -127,7 +126,6 @@ class _BottomSheet extends StatelessWidget {
configuration: configuration, configuration: configuration,
onConfirmOrder: configuration.onConfirmOrder, onConfirmOrder: configuration.onConfirmOrder,
), ),
),
], ],
); );
} }