Add: CartProvider
This commit is contained in:
@@ -3,9 +3,11 @@ import 'package:gymlink_module_web/components/app_bar.dart';
|
|||||||
import 'package:gymlink_module_web/components/basket_item_card.dart';
|
import 'package:gymlink_module_web/components/basket_item_card.dart';
|
||||||
import 'package:gymlink_module_web/components/heading.dart';
|
import 'package:gymlink_module_web/components/heading.dart';
|
||||||
import 'package:gymlink_module_web/pages/order_confirmation.dart';
|
import 'package:gymlink_module_web/pages/order_confirmation.dart';
|
||||||
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/tools/prefs.dart';
|
import 'package:gymlink_module_web/tools/prefs.dart';
|
||||||
import 'package:gymlink_module_web/tools/routes.dart';
|
import 'package:gymlink_module_web/tools/routes.dart';
|
||||||
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
List<Map<String, dynamic>> cart = [
|
List<Map<String, dynamic>> cart = [
|
||||||
{
|
{
|
||||||
@@ -73,6 +75,10 @@ class _BasketPageState extends State<BasketPage> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _updateCart() {
|
||||||
|
Provider.of<CartProvider>(context, listen: false).updateCartLength();
|
||||||
|
}
|
||||||
|
|
||||||
void removeItem(String id) async {
|
void removeItem(String id) async {
|
||||||
final item = cartItems.firstWhere((element) => element['id'] == id);
|
final item = cartItems.firstWhere((element) => element['id'] == id);
|
||||||
bool toDelete = false;
|
bool toDelete = false;
|
||||||
@@ -136,6 +142,9 @@ class _BasketPageState extends State<BasketPage> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
cartItems.removeWhere((element) => element['id'] == id);
|
cartItems.removeWhere((element) => element['id'] == id);
|
||||||
});
|
});
|
||||||
|
if (mounted) {
|
||||||
|
_updateCart();
|
||||||
|
}
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -173,6 +182,9 @@ class _BasketPageState extends State<BasketPage> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
cartItems = [];
|
cartItems = [];
|
||||||
});
|
});
|
||||||
|
if (mounted) {
|
||||||
|
_updateCart();
|
||||||
|
}
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gymlink_module_web/components/app_bar.dart';
|
import 'package:gymlink_module_web/components/app_bar.dart';
|
||||||
import 'package:gymlink_module_web/components/heading.dart';
|
import 'package:gymlink_module_web/components/heading.dart';
|
||||||
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/tools/prefs.dart';
|
import 'package:gymlink_module_web/tools/prefs.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
//TODO: Сделать получение инфы через объект
|
//TODO: Сделать получение инфы через объект
|
||||||
class DetailPage extends StatefulWidget {
|
class DetailPage extends StatefulWidget {
|
||||||
@@ -67,6 +69,9 @@ class _DetailPageState extends State<DetailPage> {
|
|||||||
isInCart = true;
|
isInCart = true;
|
||||||
quantity = 1;
|
quantity = 1;
|
||||||
});
|
});
|
||||||
|
if (mounted) {
|
||||||
|
context.read<CartProvider>().updateCartLength();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
style: ElevatedButton.styleFrom(
|
style: ElevatedButton.styleFrom(
|
||||||
backgroundColor: Theme.of(context).primaryColor,
|
backgroundColor: Theme.of(context).primaryColor,
|
||||||
@@ -93,6 +98,9 @@ class _DetailPageState extends State<DetailPage> {
|
|||||||
quantity = 0;
|
quantity = 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (mounted) {
|
||||||
|
context.read<CartProvider>().updateCartLength();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
const SizedBox(width: 10),
|
const SizedBox(width: 10),
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import 'package:gymlink_module_web/components/item_card.dart';
|
|||||||
import 'package:gymlink_module_web/pages/basket.dart';
|
import 'package:gymlink_module_web/pages/basket.dart';
|
||||||
import 'package:gymlink_module_web/pages/detail.dart';
|
import 'package:gymlink_module_web/pages/detail.dart';
|
||||||
import 'package:gymlink_module_web/pages/order_history.dart';
|
import 'package:gymlink_module_web/pages/order_history.dart';
|
||||||
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/tools/prefs.dart';
|
import 'package:gymlink_module_web/tools/prefs.dart';
|
||||||
import 'package:gymlink_module_web/tools/relative.dart';
|
import 'package:gymlink_module_web/tools/relative.dart';
|
||||||
import 'package:gymlink_module_web/tools/routes.dart';
|
import 'package:gymlink_module_web/tools/routes.dart';
|
||||||
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
const List<Map<String, String>> testData = [
|
const List<Map<String, String>> testData = [
|
||||||
@@ -105,6 +107,7 @@ class _MainPageState extends State<MainPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final cartL = context.watch<CartProvider>().cartLength;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: widget.isLoading ? null : const GymLinkAppBar(),
|
appBar: widget.isLoading ? null : const GymLinkAppBar(),
|
||||||
body: widget.isLoading
|
body: widget.isLoading
|
||||||
@@ -233,50 +236,49 @@ class _MainPageState extends State<MainPage> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
floatingActionButton: SizedBox(
|
floatingActionButton: !widget.isLoading
|
||||||
height: 80,
|
? SizedBox(
|
||||||
width: 80,
|
height: 80,
|
||||||
child: FittedBox(
|
width: 80,
|
||||||
child: Stack(
|
child: FittedBox(
|
||||||
children: [
|
child: Stack(
|
||||||
FloatingActionButton(
|
children: [
|
||||||
onPressed: () => Navigator.of(context).push(CustomPageRoute(
|
FloatingActionButton(
|
||||||
builder: (context) => const BasketPage(),
|
onPressed: () =>
|
||||||
)),
|
Navigator.of(context).push(CustomPageRoute(
|
||||||
backgroundColor: Colors.transparent,
|
builder: (context) => const BasketPage(),
|
||||||
elevation: 0,
|
)),
|
||||||
child: CircleAvatar(
|
backgroundColor: Colors.transparent,
|
||||||
radius: 25,
|
elevation: 0,
|
||||||
backgroundColor: Theme.of(context).primaryColor,
|
child: CircleAvatar(
|
||||||
foregroundColor: Colors.white,
|
radius: 25,
|
||||||
child: const Icon(Icons.shopping_cart_outlined)),
|
backgroundColor: Theme.of(context).primaryColor,
|
||||||
),
|
foregroundColor: Colors.white,
|
||||||
/**
|
child: const Icon(Icons.shopping_cart_outlined)),
|
||||||
* Загадочная штука, надо переделать
|
),
|
||||||
* TODO: По идее надо через провайдер сделать, но не пойму как это сделать
|
cartL > 0
|
||||||
*/
|
? Positioned(
|
||||||
cartLength > 0
|
right: -3,
|
||||||
? Positioned(
|
bottom: 0,
|
||||||
right: -3,
|
child: Card(
|
||||||
bottom: 0,
|
color: Colors.red,
|
||||||
child: Card(
|
child: SizedBox(
|
||||||
color: Colors.red,
|
width: 20,
|
||||||
child: SizedBox(
|
child: Center(
|
||||||
width: 20,
|
child: Text(
|
||||||
child: Center(
|
cartL.toString(),
|
||||||
child: Text(
|
style: const TextStyle(color: Colors.white),
|
||||||
cartLength.toString(),
|
),
|
||||||
style: const TextStyle(color: Colors.white),
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
)
|
||||||
),
|
: const SizedBox.shrink(),
|
||||||
),
|
],
|
||||||
)
|
),
|
||||||
: const SizedBox.shrink(),
|
),
|
||||||
],
|
)
|
||||||
),
|
: null,
|
||||||
),
|
|
||||||
),
|
|
||||||
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
|
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gymlink_module_web/main_mobile.dart';
|
import 'package:gymlink_module_web/main_mobile.dart';
|
||||||
import 'package:gymlink_module_web/pages/main.dart';
|
import 'package:gymlink_module_web/pages/main.dart';
|
||||||
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/providers/main.dart';
|
import 'package:gymlink_module_web/providers/main.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@@ -8,12 +9,19 @@ class MyAppStateMobile extends State<MyApp> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Consumer<GymLinkProvider>(
|
return Consumer<GymLinkProvider>(
|
||||||
builder: (context, provider, __) => MaterialApp(
|
builder: (context, provider, __) {
|
||||||
title: 'GymLink Module',
|
final theme = provider.theme;
|
||||||
theme: provider.theme,
|
final isLoading = provider.isLoading;
|
||||||
debugShowCheckedModeBanner: false,
|
return ChangeNotifierProvider(
|
||||||
home: MainPage(isLoading: provider.isLoading),
|
create: (_) => CartProvider(),
|
||||||
),
|
builder: (context, __) => MaterialApp(
|
||||||
|
title: 'GymLink Module',
|
||||||
|
theme: theme,
|
||||||
|
debugShowCheckedModeBanner: false,
|
||||||
|
home: MainPage(isLoading: isLoading),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import 'dart:js_interop_unsafe' as js_util;
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gymlink_module_web/main.dart';
|
import 'package:gymlink_module_web/main.dart';
|
||||||
import 'package:gymlink_module_web/pages/main.dart';
|
import 'package:gymlink_module_web/pages/main.dart';
|
||||||
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/theme.dart';
|
import 'package:gymlink_module_web/theme.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@js.JSExport()
|
@js.JSExport()
|
||||||
class MyAppStateWeb extends State<MyApp> {
|
class MyAppStateWeb extends State<MyApp> {
|
||||||
@@ -30,11 +32,14 @@ class MyAppStateWeb extends State<MyApp> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return ChangeNotifierProvider(
|
||||||
title: 'GymLink Module',
|
create: (_) => CartProvider(),
|
||||||
theme: theme,
|
child: MaterialApp(
|
||||||
debugShowCheckedModeBanner: false,
|
title: 'GymLink Module',
|
||||||
home: MainPage(isLoading: _isLoading),
|
theme: theme,
|
||||||
|
debugShowCheckedModeBanner: false,
|
||||||
|
home: MainPage(isLoading: _isLoading),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user