1
0
Fork 0

Add user store

provider
Jonas Franz 2 years ago
parent 06a457f7c4
commit 919d8ed3e5
  1. 29
      lib/app.dart
  2. 2
      lib/screens/cart/cart_screen.dart
  3. 2
      lib/screens/product_list/product_list_screen.dart
  4. 11
      lib/store/user_store.dart
  5. 14
      lib/widgets/user_switch.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',
);
}
}

@ -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,

@ -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),

@ -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();
}
}

@ -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<bool> 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<UserStore>();
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,
)
],

Loading…
Cancel
Save