1
0
Fork 0
Browse Source

Add cart button

main
Jonas Franz 6 months ago
parent
commit
476dff1499
  1. 9
      lib/app.dart
  2. 7
      lib/route_key.dart
  3. 12
      lib/screens/cart/cart_screen.dart
  4. 15
      lib/screens/product_list/cart_button.dart
  5. 23
      lib/screens/product_list/cart_button_overlay.dart
  6. 18
      lib/screens/product_list/product_list.dart
  7. 10
      lib/screens/product_list/product_list_screen.dart
  8. 6
      lib/utils/map_keys_extension.dart

9
lib/app.dart

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
import 'package:flutter/material.dart';
import 'package:thesis_shop/route_key.dart';
import 'package:thesis_shop/screens/cart/cart_screen.dart';
import 'package:thesis_shop/screens/product_list/product_list_screen.dart';
import 'package:thesis_shop/utils/map_keys_extension.dart';
class ThesisShopApp extends StatelessWidget {
const ThesisShopApp({Key? key}) : super(key: key);
@ -13,7 +16,11 @@ class ThesisShopApp extends StatelessWidget { @@ -13,7 +16,11 @@ class ThesisShopApp extends StatelessWidget {
primarySwatch: Colors.red,
brightness: Brightness.dark,
),
home: const ProductListScreen(),
routes: {
RouteKey.products: (context) => const ProductListScreen(),
RouteKey.cart: (context) => const CartScreen(),
}.mapKeys((key) => key.name),
initialRoute: 'products',
);
}
}

7
lib/route_key.dart

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
import 'package:flutter/widgets.dart';
enum RouteKey { products, cart }
extension NavigatorHelper on NavigatorState {
pushRouteKey(RouteKey key) => pushNamed(key.name);
}

12
lib/screens/cart/cart_screen.dart

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
import 'package:flutter/material.dart';
class CartScreen extends StatelessWidget {
const CartScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Warenkorb')),
);
}
}

15
lib/screens/product_list/cart_button.dart

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
import 'package:thesis_shop/route_key.dart';
class CartButton extends StatelessWidget {
const CartButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ElevatedButton.icon(
onPressed: () => Navigator.of(context).pushRouteKey(RouteKey.cart),
icon: const Icon(Icons.shopping_basket),
label: const Text('Warenkorb (2 Produkte)'),
);
}
}

23
lib/screens/product_list/cart_button_overlay.dart

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'cart_button.dart';
class CartButtonOverlay extends StatelessWidget {
final Widget child;
const CartButtonOverlay({Key? key, required this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned.fill(child: child),
const Positioned(
left: 8,
bottom: 8,
right: 8,
child: SafeArea(child: CartButton()),
)
],
);
}
}

18
lib/screens/product_list/product_list.dart

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'package:thesis_shop/models/product.dart';
import 'product_item.dart';
class ProductList extends StatelessWidget {
final List<Product> products;
const ProductList({Key? key, required this.products}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: products.length,
itemBuilder: (context, index) => ProductItem(product: products[index]),
separatorBuilder: (context, _) => const Divider(),
);
}
}

10
lib/screens/product_list/product_list_screen.dart

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:thesis_shop/models/product.dart';
import 'package:thesis_shop/screens/product_list/product_item.dart';
import 'cart_button_overlay.dart';
import 'product_list.dart';
const _productPlaceholder = [
Product(title: 'Bananen', price: 3),
@ -17,10 +19,8 @@ class ProductListScreen extends StatelessWidget { @@ -17,10 +19,8 @@ class ProductListScreen extends StatelessWidget {
const products = _productPlaceholder;
return Scaffold(
appBar: AppBar(title: const Text('Thesis Shop')),
body: ListView.separated(
itemCount: products.length,
itemBuilder: (context, index) => ProductItem(product: products[index]),
separatorBuilder: (context, _) => const Divider(),
body: const CartButtonOverlay(
child: ProductList(products: _productPlaceholder),
),
);
}

6
lib/utils/map_keys_extension.dart

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
typedef MapFunction<T, K> = T Function(K key);
extension MapKeysExtension<K, V> on Map<K, V> {
Map<T, V> mapKeys<T>(MapFunction<T, K> keyMapper) =>
map((key, value) => MapEntry(keyMapper(key), value));
}
Loading…
Cancel
Save