Skip to content

Commit

Permalink
Update decorator and strategy example code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobuolys committed Jan 28, 2024
1 parent 124aaa3 commit 73110e2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 84 deletions.
51 changes: 17 additions & 34 deletions assets/markdown/strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,42 +182,25 @@ class _StrategyExampleState extends State<StrategyExample> {
onClear: _clearOrder,
),
const SizedBox(height: LayoutConstants.spaceM),
Stack(
Column(
children: <Widget>[
AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _order.items.isEmpty ? 1.0 : 0.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Your order is empty',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
if (_order.items.isEmpty)
Text(
'Your order is empty',
style: Theme.of(context).textTheme.titleLarge,
)
else
OrderItemsTable(orderItems: _order.items),
const SizedBox(height: LayoutConstants.spaceM),
ShippingOptions(
selectedIndex: _selectedStrategyIndex,
shippingOptions: _shippingCostsStrategyList,
onChanged: _setSelectedStrategyIndex,
),
AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _order.items.isEmpty ? 0.0 : 1.0,
child: Column(
children: <Widget>[
OrderItemsTable(
orderItems: _order.items,
),
const SizedBox(height: LayoutConstants.spaceM),
ShippingOptions(
selectedIndex: _selectedStrategyIndex,
shippingOptions: _shippingCostsStrategyList,
onChanged: _setSelectedStrategyIndex,
),
OrderSummary(
shippingCostsStrategy:
_shippingCostsStrategyList[_selectedStrategyIndex],
order: _order,
),
],
),
OrderSummary(
shippingCostsStrategy:
_shippingCostsStrategyList[_selectedStrategyIndex],
order: _order,
),
],
),
Expand Down
52 changes: 36 additions & 16 deletions lib/widgets/design_patterns/decorator/custom_pizza_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,49 @@ class CustomPizzaSelection extends StatelessWidget {
children: <Widget>[
for (var i = 0; i < pizzaToppingsDataMap.length; i++)
i == 0
? ChoiceChip(
label: const Text(
'Pizza Base',
style: TextStyle(color: Colors.white),
),
? _ChoiceChip(
label: 'Pizza Base',
selected: true,
selectedColor: Colors.black,
onSelected: (_) {},
)
: ChoiceChip(
label: Text(
pizzaToppingsDataMap[i]!.label,
style: TextStyle(
color: pizzaToppingsDataMap[i]!.selected
? Colors.white
: Colors.black,
),
),
: _ChoiceChip(
label: pizzaToppingsDataMap[i]!.label,
selected: pizzaToppingsDataMap[i]!.selected,
selectedColor: Colors.black,
onSelected: (bool? selected) => onSelected(i, selected),
),
],
);
}
}

class _ChoiceChip extends StatelessWidget {
const _ChoiceChip({
required this.label,
required this.selected,
required this.onSelected,
});

final String label;
final bool selected;
final ValueSetter<bool> onSelected;

@override
Widget build(BuildContext context) {
final color = selected ? Colors.white : Colors.black;

return ChoiceChip(
color: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return Colors.black;
}

return Colors.white;
}),
label: Text(label),
labelStyle: TextStyle(color: color),
checkmarkColor: color,
selected: selected,
onSelected: onSelected,
);
}
}
51 changes: 17 additions & 34 deletions lib/widgets/design_patterns/strategy/strategy_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,25 @@ class _StrategyExampleState extends State<StrategyExample> {
onClear: _clearOrder,
),
const SizedBox(height: LayoutConstants.spaceM),
Stack(
Column(
children: <Widget>[
AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _order.items.isEmpty ? 1.0 : 0.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Your order is empty',
style: Theme.of(context).textTheme.titleLarge,
),
],
),
if (_order.items.isEmpty)
Text(
'Your order is empty',
style: Theme.of(context).textTheme.titleLarge,
)
else
OrderItemsTable(orderItems: _order.items),
const SizedBox(height: LayoutConstants.spaceM),
ShippingOptions(
selectedIndex: _selectedStrategyIndex,
shippingOptions: _shippingCostsStrategyList,
onChanged: _setSelectedStrategyIndex,
),
AnimatedOpacity(
duration: const Duration(milliseconds: 500),
opacity: _order.items.isEmpty ? 0.0 : 1.0,
child: Column(
children: <Widget>[
OrderItemsTable(
orderItems: _order.items,
),
const SizedBox(height: LayoutConstants.spaceM),
ShippingOptions(
selectedIndex: _selectedStrategyIndex,
shippingOptions: _shippingCostsStrategyList,
onChanged: _setSelectedStrategyIndex,
),
OrderSummary(
shippingCostsStrategy:
_shippingCostsStrategyList[_selectedStrategyIndex],
order: _order,
),
],
),
OrderSummary(
shippingCostsStrategy:
_shippingCostsStrategyList[_selectedStrategyIndex],
order: _order,
),
],
),
Expand Down

0 comments on commit 73110e2

Please sign in to comment.