feat(): riverpod & state management
#1
Merged
Alyve
merged 1 commits from feat/state-management
into main
2 years ago
@ -0,0 +1,20 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:meals/models/Meal.dart';
|
||||||
|
|
||||||
|
class FavoriteMealsNotifier extends StateNotifier<List<Meal>> {
|
||||||
|
FavoriteMealsNotifier(List<Meal>? initialList) : super(initialList ?? []);
|
||||||
|
|
||||||
|
bool toggleMealFavoriteStatus(Meal meal) {
|
||||||
|
if (state.contains(meal)) {
|
||||||
|
state = state.where((Meal m) => m.id != meal.id).toList();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
state = [...state, meal];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final favoriteMealsProvider =
|
||||||
|
StateNotifierProvider<FavoriteMealsNotifier, List<Meal>>(
|
||||||
|
(ref) => FavoriteMealsNotifier([]));
|
@ -0,0 +1,55 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:meals/providers/meals_provider.dart';
|
||||||
|
import 'package:meals/screens/filters.dart';
|
||||||
|
|
||||||
|
enum Filter {
|
||||||
|
glutenFree,
|
||||||
|
lactoseFree,
|
||||||
|
veggie,
|
||||||
|
vegan,
|
||||||
|
}
|
||||||
|
|
||||||
|
class FiltersNotifier extends StateNotifier<Map<Filter, bool>> {
|
||||||
|
FiltersNotifier()
|
||||||
|
: super({
|
||||||
|
Filter.glutenFree: false,
|
||||||
|
Filter.lactoseFree: false,
|
||||||
|
Filter.veggie: false,
|
||||||
|
Filter.vegan: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
void setFilter(Filter filter, bool isActive) {
|
||||||
|
state = {
|
||||||
|
...state,
|
||||||
|
filter: isActive,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void setFilters(Map<Filter, bool> newFilters) {
|
||||||
|
state = newFilters;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final filtersProvider =
|
||||||
|
StateNotifierProvider<FiltersNotifier, Map<Filter, bool>>(
|
||||||
|
(ref) => FiltersNotifier());
|
||||||
|
|
||||||
|
final filteredMealsProvider = Provider((ref) {
|
||||||
|
final meals = ref.watch(mealsProvider);
|
||||||
|
final activeFilters = ref.watch(filtersProvider);
|
||||||
|
return meals.where((meal) {
|
||||||
|
if (activeFilters[Filter.glutenFree]! && !meal.isGlutenFree) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (activeFilters[Filter.lactoseFree]! && !meal.isLactoseFree) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (activeFilters[Filter.veggie]! && !meal.isVegetarian) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (activeFilters[Filter.vegan]! && !meal.isVegan) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}).toList();
|
||||||
|
});
|
@ -0,0 +1,4 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:meals/data/dummy_categories.dart';
|
||||||
|
|
||||||
|
final mealsProvider = Provider((ref) => dummyMeals);
|
@ -1,134 +1,94 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'dart:developer';
|
||||||
import 'package:meals/screens/tabs.dart';
|
|
||||||
import 'package:meals/widgets/main_drawer.dart';
|
|
||||||
|
|
||||||
enum Filter {
|
|
||||||
glutenFree,
|
|
||||||
lactoseFree,
|
|
||||||
veggie,
|
|
||||||
vegan,
|
|
||||||
}
|
|
||||||
|
|
||||||
class FiltersScreen extends StatefulWidget {
|
|
||||||
final Map<Filter, bool> currentFilters;
|
|
||||||
|
|
||||||
const FiltersScreen({Key? key, required this.currentFilters})
|
import 'package:flutter/material.dart';
|
||||||
: super(key: key);
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
@override
|
|
||||||
State<FiltersScreen> createState() => _FiltersScreenState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _FiltersScreenState extends State<FiltersScreen> {
|
import '../providers/filters_provider.dart';
|
||||||
bool _glutenFreeFilterSet = false;
|
|
||||||
bool _lactoseFreeFilterSet = false;
|
|
||||||
bool _veggieFilterSet = false;
|
|
||||||
bool _veganFilterSet = false;
|
|
||||||
|
|
||||||
@override
|
class FiltersScreen extends ConsumerWidget {
|
||||||
void initState() {
|
const FiltersScreen({Key? key}) : super(key: key);
|
||||||
super.initState();
|
|
||||||
setState(() {
|
|
||||||
_glutenFreeFilterSet = widget.currentFilters[Filter.glutenFree]!;
|
|
||||||
_lactoseFreeFilterSet = widget.currentFilters[Filter.lactoseFree]!;
|
|
||||||
_veggieFilterSet = widget.currentFilters[Filter.veggie]!;
|
|
||||||
_veganFilterSet = widget.currentFilters[Filter.vegan]!;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Your filters'),
|
title: const Text('Your filters'),
|
||||||
),
|
),
|
||||||
body: WillPopScope(
|
body: Column(
|
||||||
onWillPop: () async {
|
children: [
|
||||||
Navigator.of(context).pop({
|
SwitchListTile(
|
||||||
Filter.glutenFree: _glutenFreeFilterSet,
|
value: ref.watch(filtersProvider)[Filter.glutenFree]!,
|
||||||
Filter.lactoseFree: _lactoseFreeFilterSet,
|
onChanged: (checked) => ref
|
||||||
Filter.veggie: _veggieFilterSet,
|
.read(filtersProvider.notifier)
|
||||||
Filter.vegan: _veganFilterSet,
|
.setFilter(Filter.glutenFree, checked),
|
||||||
});
|
title: Text(
|
||||||
|
'Gluten free',
|
||||||
return false;
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
},
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
child: Column(
|
),
|
||||||
children: [
|
subtitle: Text(
|
||||||
SwitchListTile(
|
'Only includes gluten-free meals',
|
||||||
value: _glutenFreeFilterSet,
|
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||||
onChanged: (checked) => setState(() {
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
_glutenFreeFilterSet = checked;
|
),
|
||||||
}),
|
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||||
title: Text(
|
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||||
'Gluten free',
|
),
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
SwitchListTile(
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
value: ref.watch(filtersProvider)[Filter.lactoseFree]!,
|
||||||
),
|
onChanged: (checked) => ref
|
||||||
subtitle: Text(
|
.read(filtersProvider.notifier)
|
||||||
'Only includes gluten-free meals',
|
.setFilter(Filter.lactoseFree, checked),
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
title: Text(
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
'Lactose free',
|
||||||
),
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
),
|
||||||
|
subtitle: Text(
|
||||||
|
'Only includes lactose-free meals',
|
||||||
|
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||||
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
|
),
|
||||||
|
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||||
|
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||||
|
),
|
||||||
|
SwitchListTile(
|
||||||
|
value: ref.watch(filtersProvider)[Filter.veggie]!,
|
||||||
|
onChanged: (checked) => ref
|
||||||
|
.read(filtersProvider.notifier)
|
||||||
|
.setFilter(Filter.veggie, checked),
|
||||||
|
title: Text(
|
||||||
|
'Veggie',
|
||||||
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
|
),
|
||||||
|
subtitle: Text(
|
||||||
|
'Only includes veggie meals',
|
||||||
|
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||||
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
),
|
),
|
||||||
SwitchListTile(
|
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||||
value: _lactoseFreeFilterSet,
|
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||||
onChanged: (checked) => setState(() {
|
),
|
||||||
_lactoseFreeFilterSet = checked;
|
SwitchListTile(
|
||||||
}),
|
value: ref.watch(filtersProvider)[Filter.vegan]!,
|
||||||
title: Text(
|
onChanged: (checked) => ref
|
||||||
'Lactose free',
|
.read(filtersProvider.notifier)
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
.setFilter(Filter.vegan, checked),
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
title: Text(
|
||||||
),
|
'Vegan',
|
||||||
subtitle: Text(
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
'Only includes lactose-free meals',
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
|
||||||
),
|
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
|
||||||
),
|
),
|
||||||
SwitchListTile(
|
subtitle: Text(
|
||||||
value: _veggieFilterSet,
|
'Only includes vegan meals',
|
||||||
onChanged: (checked) => setState(() {
|
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||||
_veggieFilterSet = checked;
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
}),
|
|
||||||
title: Text(
|
|
||||||
'Veggie',
|
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
|
||||||
),
|
|
||||||
subtitle: Text(
|
|
||||||
'Only includes veggie meals',
|
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
|
||||||
),
|
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
|
||||||
),
|
),
|
||||||
SwitchListTile(
|
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||||
value: _veganFilterSet,
|
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||||
onChanged: (checked) => setState(() {
|
)
|
||||||
_veganFilterSet = checked;
|
],
|
||||||
}),
|
|
||||||
title: Text(
|
|
||||||
'Vegan',
|
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
|
||||||
),
|
|
||||||
subtitle: Text(
|
|
||||||
'Only includes vegan meals',
|
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
|
||||||
),
|
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue