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 '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;
|
||||
import 'dart:developer';
|
||||
|
||||
const FiltersScreen({Key? key, required this.currentFilters})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
State<FiltersScreen> createState() => _FiltersScreenState();
|
||||
}
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class _FiltersScreenState extends State<FiltersScreen> {
|
||||
bool _glutenFreeFilterSet = false;
|
||||
bool _lactoseFreeFilterSet = false;
|
||||
bool _veggieFilterSet = false;
|
||||
bool _veganFilterSet = false;
|
||||
import '../providers/filters_provider.dart';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
setState(() {
|
||||
_glutenFreeFilterSet = widget.currentFilters[Filter.glutenFree]!;
|
||||
_lactoseFreeFilterSet = widget.currentFilters[Filter.lactoseFree]!;
|
||||
_veggieFilterSet = widget.currentFilters[Filter.veggie]!;
|
||||
_veganFilterSet = widget.currentFilters[Filter.vegan]!;
|
||||
});
|
||||
}
|
||||
class FiltersScreen extends ConsumerWidget {
|
||||
const FiltersScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Your filters'),
|
||||
),
|
||||
body: WillPopScope(
|
||||
onWillPop: () async {
|
||||
Navigator.of(context).pop({
|
||||
Filter.glutenFree: _glutenFreeFilterSet,
|
||||
Filter.lactoseFree: _lactoseFreeFilterSet,
|
||||
Filter.veggie: _veggieFilterSet,
|
||||
Filter.vegan: _veganFilterSet,
|
||||
});
|
||||
|
||||
return false;
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
value: _glutenFreeFilterSet,
|
||||
onChanged: (checked) => setState(() {
|
||||
_glutenFreeFilterSet = checked;
|
||||
}),
|
||||
title: Text(
|
||||
'Gluten free',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
subtitle: Text(
|
||||
'Only includes gluten-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),
|
||||
body: Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
value: ref.watch(filtersProvider)[Filter.glutenFree]!,
|
||||
onChanged: (checked) => ref
|
||||
.read(filtersProvider.notifier)
|
||||
.setFilter(Filter.glutenFree, checked),
|
||||
title: Text(
|
||||
'Gluten free',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
subtitle: Text(
|
||||
'Only includes gluten-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.lactoseFree]!,
|
||||
onChanged: (checked) => ref
|
||||
.read(filtersProvider.notifier)
|
||||
.setFilter(Filter.lactoseFree, checked),
|
||||
title: Text(
|
||||
'Lactose free',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
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(
|
||||
value: _lactoseFreeFilterSet,
|
||||
onChanged: (checked) => setState(() {
|
||||
_lactoseFreeFilterSet = checked;
|
||||
}),
|
||||
title: Text(
|
||||
'Lactose free',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
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),
|
||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||
),
|
||||
SwitchListTile(
|
||||
value: ref.watch(filtersProvider)[Filter.vegan]!,
|
||||
onChanged: (checked) => ref
|
||||
.read(filtersProvider.notifier)
|
||||
.setFilter(Filter.vegan, checked),
|
||||
title: Text(
|
||||
'Vegan',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
SwitchListTile(
|
||||
value: _veggieFilterSet,
|
||||
onChanged: (checked) => setState(() {
|
||||
_veggieFilterSet = 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),
|
||||
),
|
||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||
subtitle: Text(
|
||||
'Only includes vegan meals',
|
||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
SwitchListTile(
|
||||
value: _veganFilterSet,
|
||||
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),
|
||||
)
|
||||
],
|
||||
),
|
||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue