From 919d8ed3e5ed8ed91382207699db488dadb3f82d Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Mon, 21 Feb 2022 20:59:32 +0100 Subject: [PATCH] Add user store --- lib/app.dart | 29 ++++++++++++------- lib/screens/cart/cart_screen.dart | 2 +- .../product_list/product_list_screen.dart | 2 +- lib/store/user_store.dart | 11 +++++++ lib/widgets/user_switch.dart | 14 ++++----- 5 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 lib/store/user_store.dart diff --git a/lib/app.dart b/lib/app.dart index c755039..a014271 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.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/service/product_service.dart'; +import 'package:thesis_shop/store/user_store.dart'; import 'package:thesis_shop/utils/map_keys_extension.dart'; class ThesisShopApp extends StatelessWidget { @@ -12,18 +14,23 @@ class ThesisShopApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( - title: 'Thesis Shop', - theme: ThemeData(primarySwatch: Colors.red), - darkTheme: ThemeData( - primarySwatch: Colors.red, - brightness: Brightness.dark, + return MultiProvider( + providers: [ + ChangeNotifierProvider(create: (context) => UserStore()), + ], + child: MaterialApp( + title: 'Thesis Shop', + theme: ThemeData(primarySwatch: Colors.red), + darkTheme: ThemeData( + primarySwatch: Colors.red, + brightness: Brightness.dark, + ), + routes: { + RouteKey.products: (context) => const ProductListScreen(), + RouteKey.cart: (context) => const CartScreen(), + }.mapKeys((key) => key.name), + initialRoute: 'products', ), - routes: { - RouteKey.products: (context) => const ProductListScreen(), - RouteKey.cart: (context) => const CartScreen(), - }.mapKeys((key) => key.name), - initialRoute: 'products', ); } } diff --git a/lib/screens/cart/cart_screen.dart b/lib/screens/cart/cart_screen.dart index 08e9e45..e369485 100644 --- a/lib/screens/cart/cart_screen.dart +++ b/lib/screens/cart/cart_screen.dart @@ -20,7 +20,7 @@ class CartScreen extends StatelessWidget { return Scaffold( appBar: AppBar( title: const Text('Warenkorb'), - actions: [UserSwitch(isOn: true, onChanged: (_) {})], + actions: const [UserSwitch()], ), body: Column( mainAxisSize: MainAxisSize.max, diff --git a/lib/screens/product_list/product_list_screen.dart b/lib/screens/product_list/product_list_screen.dart index 30ce3a8..e623621 100644 --- a/lib/screens/product_list/product_list_screen.dart +++ b/lib/screens/product_list/product_list_screen.dart @@ -21,7 +21,7 @@ class ProductListScreen extends StatelessWidget { return Scaffold( appBar: AppBar( title: const Text('Thesis Shop'), - actions: [UserSwitch(isOn: true, onChanged: (_) {})], + actions: const [UserSwitch()], ), body: const CartButtonOverlay( child: ProductList(products: products), diff --git a/lib/store/user_store.dart b/lib/store/user_store.dart new file mode 100644 index 0000000..85a7957 --- /dev/null +++ b/lib/store/user_store.dart @@ -0,0 +1,11 @@ +import 'package:flutter/foundation.dart'; + +class UserStore extends ChangeNotifier { + bool _isSignedIn = false; + bool get isSignedIn => _isSignedIn; + + void changeSignIn(bool newValue) { + _isSignedIn = newValue; + notifyListeners(); + } +} diff --git a/lib/widgets/user_switch.dart b/lib/widgets/user_switch.dart index 63107e7..a3a13ad 100644 --- a/lib/widgets/user_switch.dart +++ b/lib/widgets/user_switch.dart @@ -1,24 +1,22 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:thesis_shop/benchmark_counter.dart'; +import 'package:thesis_shop/store/user_store.dart'; class UserSwitch extends StatelessWidget { - final bool isOn; - final ValueChanged onChanged; - const UserSwitch({ - required this.isOn, - required this.onChanged, - }) : super(key: const Key('user_switch')); + const UserSwitch() : super(key: const Key('user_switch')); @override Widget build(BuildContext context) { + final userStore = context.watch(); BenchmarkCounters.userSwitch++; return Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.account_circle), Switch( - value: isOn, - onChanged: onChanged, + value: userStore.isSignedIn, + onChanged: userStore.changeSignIn, activeColor: Colors.green, ) ],