Skip to content

Commit

Permalink
refactor: cleanup (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
yousinix authored Nov 8, 2023
1 parent 828cde1 commit 5d30ba9
Show file tree
Hide file tree
Showing 46 changed files with 357 additions and 422 deletions.
40 changes: 22 additions & 18 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:flutter_lints/flutter.yaml
include: package:very_good_analysis/analysis_options.yaml

analyzer:
language:
Expand All @@ -13,28 +13,32 @@ analyzer:
linter:
rules:
# Constructors
- sort_unnamed_constructors_first
- sort_constructors_first
- prefer_const_constructors
- use_key_in_widget_constructors
sort_unnamed_constructors_first: true
sort_constructors_first: true
prefer_const_constructors: true
use_key_in_widget_constructors: true
always_put_required_named_parameters_first: false

# Imports
- prefer_relative_imports
- directives_ordering
- combinators_ordering
- depend_on_referenced_packages
- avoid_web_libraries_in_flutter
prefer_relative_imports: true
directives_ordering: true
combinators_ordering: true
depend_on_referenced_packages: true
avoid_web_libraries_in_flutter: true
always_use_package_imports: false

# Dependencies
- sort_pub_dependencies
sort_pub_dependencies: true

# Style
- prefer_single_quotes
- require_trailing_commas
- noop_primitive_operations
- use_super_parameters
- avoid_redundant_argument_values
prefer_single_quotes: true
require_trailing_commas: true
noop_primitive_operations: true
use_super_parameters: true
avoid_redundant_argument_values: true
public_member_api_docs: false
flutter_style_todos: false

# Types
- unrelated_type_equality_checks
- avoid_types_on_closure_parameters
unrelated_type_equality_checks: true
avoid_types_on_closure_parameters: true
Binary file removed assets/fruits/avocado.jpeg
Binary file not shown.
Binary file removed assets/fruits/avocado2.jpeg
Binary file not shown.
Binary file removed assets/fruits/banana.jpeg
Binary file not shown.
Binary file removed assets/fruits/banana2.jpeg
Binary file not shown.
Binary file removed assets/fruits/mango.jpeg
Binary file not shown.
Binary file removed assets/fruits/mango2.jpeg
Binary file not shown.
67 changes: 36 additions & 31 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';

import 'basket/basket_scope.dart';
import 'basket/basket_state.dart';
import 'fixtures/fruits.dart';
import 'basket/basket.dart';
import 'home/home.dart';
import 'l10n/app_localizations.dart';
import 'repositories/fruits_repository.dart';
import 'theme/theme.dart';

final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => BasketScope(
state: BasketState(
data: getFruitsMap(context),
),
child: Builder(
builder: (context) {
final basketState = BasketState.of(context);
return HomeScreen(
fruits: basketState.basketSummary.keys.toList(),
);
},
),
builder: (_, __) => FutureBuilder(
future: FruitRepository().getFruits(),
builder: (_, snapshot) {
if (!snapshot.hasData) return const Placeholder();

return HomeScreen(
fruits: snapshot.data!,
);
},
),
),
// GoRoute(
// path: '/basket',
// builder: (context, state) => BasketScreen(
// fruits: Map.fromIterable(getFruits(context)),
// ),
// ),
GoRoute(
path: '/basket',
builder: (context, state) {
final basketState = BasketState.of(context);

return BasketScreen(
basket: basketState.store,
delivery: basketState.delivery,
subTotal: basketState.subTotal,
);
},
),
],
);

Expand All @@ -47,16 +49,19 @@ class App extends StatelessWidget {
: darkTheme,
child: Builder(
builder: (context) {
return ColoredBox(
color: AppTheme.of(context).surface.primary,
child: SafeArea(
child: WidgetsApp.router(
debugShowCheckedModeBanner: false,
color: lightTheme.surface.primary,
routerConfig: _router,
title: 'Grocery App',
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
return BasketScope(
child: ColoredBox(
color: AppTheme.of(context).surface.primary,
child: SafeArea(
child: WidgetsApp.router(
title: 'Grocery App',
debugShowCheckedModeBanner: false,
color: lightTheme.surface.primary,
routerConfig: _router,
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates:
AppLocalizations.localizationsDelegates,
),
),
),
);
Expand Down
4 changes: 3 additions & 1 deletion lib/basket/basket.dart
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';
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@ import 'dart:math';

import 'package:flutter/widgets.dart';

import '../../core/app_bar.dart';
import '../../core/primary_button.dart';
import '../../l10n/app_localizations.dart';
import '../../models/fruit.dart';
import '../../theme/app_theme.dart';
import '../basket_state.dart';
import '../widgets/widgets.dart';
import '../core/app_bar.dart';
import '../core/primary_button.dart';
import '../l10n/app_localizations.dart';
import '../repositories/fruit.dart';
import '../theme/app_theme.dart';
import 'state/basket_state.dart';
import 'widgets/widgets.dart';

class BasketScreen extends StatelessWidget {
const BasketScreen({
super.key,
required this.fruits,
required this.basket,
required this.delivery,
required this.total,
required this.subTotal,
});

final Map<Fruit, ProductOrder> fruits;
final Map<Fruit, ProductOrder> basket;
final double delivery;
final double total;
final double subTotal;

Widget _buildEmptyPage(BuildContext context) {
Expand Down Expand Up @@ -57,7 +55,7 @@ class BasketScreen extends StatelessWidget {
constraint.maxWidth ~/ 350,
);
return GridView.count(
key: ValueKey(fruits),
key: ValueKey(basket),
crossAxisCount: columns,
padding: EdgeInsets.zero,
mainAxisSpacing: AppTheme.of(context).spacing.medium,
Expand All @@ -66,13 +64,13 @@ class BasketScreen extends StatelessWidget {
// 116 is the height of the BasketCard
childAspectRatio: constraint.maxWidth / columns / 116,
children: [
for (final fruit in fruits.keys)
for (final fruit in basket.keys)
BasketCard(
numberOfFruits: fruits[fruit]!.quantity,
fruit: fruit,
onFruitAdded: (fruit) =>
count: basket[fruit]!.quantity,
onFruitAdded: () =>
BasketState.of(context).addFruit(fruit),
onFruitRemoved: (fruit) =>
onFruitRemoved: () =>
BasketState.of(context).removeFruit(fruit),
),
],
Expand All @@ -81,7 +79,6 @@ class BasketScreen extends StatelessWidget {
),
),
Summary(
total: total,
delivery: delivery,
subTotal: subTotal,
),
Expand All @@ -102,7 +99,7 @@ class BasketScreen extends StatelessWidget {
children: [
AppBar(
title: 'Groceries App',
numberOfItemsInBasket: fruits.length,
basketSize: basket.length,
),
SizedBox(
height: AppTheme.of(context).spacing.large,
Expand All @@ -112,7 +109,7 @@ class BasketScreen extends StatelessWidget {
padding: EdgeInsets.symmetric(
horizontal: AppTheme.of(context).spacing.medium,
),
child: fruits.isEmpty
child: basket.isEmpty
? _buildEmptyPage(context)
: _buildFilledPage(context),
),
Expand Down
68 changes: 0 additions & 68 deletions lib/basket/basket_state.dart

This file was deleted.

File renamed without changes.
71 changes: 71 additions & 0 deletions lib/basket/state/basket_state.dart
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();
}
}
Loading

0 comments on commit 5d30ba9

Please sign in to comment.