Compare commits
No commits in common. '490e0c10cb6ec2ba446e00dfc998b1c9d0cfbb56' and 'bbac96686e93ae8d097fa7f529878bed53f23d3d' have entirely different histories.
490e0c10cb
...
bbac96686e
@ -1,20 +0,0 @@
|
|||||||
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([]));
|
|
@ -1,55 +0,0 @@
|
|||||||
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();
|
|
||||||
});
|
|
@ -1,4 +0,0 @@
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
||||||
import 'package:meals/data/dummy_categories.dart';
|
|
||||||
|
|
||||||
final mealsProvider = Provider((ref) => dummyMeals);
|
|
@ -1,94 +1,134 @@
|
|||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.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;
|
||||||
|
|
||||||
|
const FiltersScreen({Key? key, required this.currentFilters})
|
||||||
|
: super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<FiltersScreen> createState() => _FiltersScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
import '../providers/filters_provider.dart';
|
class _FiltersScreenState extends State<FiltersScreen> {
|
||||||
|
bool _glutenFreeFilterSet = false;
|
||||||
|
bool _lactoseFreeFilterSet = false;
|
||||||
|
bool _veggieFilterSet = false;
|
||||||
|
bool _veganFilterSet = false;
|
||||||
|
|
||||||
class FiltersScreen extends ConsumerWidget {
|
@override
|
||||||
const FiltersScreen({Key? key}) : super(key: key);
|
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]!;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Your filters'),
|
title: const Text('Your filters'),
|
||||||
),
|
),
|
||||||
body: Column(
|
body: WillPopScope(
|
||||||
children: [
|
onWillPop: () async {
|
||||||
SwitchListTile(
|
Navigator.of(context).pop({
|
||||||
value: ref.watch(filtersProvider)[Filter.glutenFree]!,
|
Filter.glutenFree: _glutenFreeFilterSet,
|
||||||
onChanged: (checked) => ref
|
Filter.lactoseFree: _lactoseFreeFilterSet,
|
||||||
.read(filtersProvider.notifier)
|
Filter.veggie: _veggieFilterSet,
|
||||||
.setFilter(Filter.glutenFree, checked),
|
Filter.vegan: _veganFilterSet,
|
||||||
title: Text(
|
});
|
||||||
'Gluten free',
|
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
return false;
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
},
|
||||||
),
|
child: Column(
|
||||||
subtitle: Text(
|
children: [
|
||||||
'Only includes gluten-free meals',
|
SwitchListTile(
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
value: _glutenFreeFilterSet,
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
onChanged: (checked) => setState(() {
|
||||||
),
|
_glutenFreeFilterSet = checked;
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
}),
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
title: Text(
|
||||||
),
|
'Gluten free',
|
||||||
SwitchListTile(
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
value: ref.watch(filtersProvider)[Filter.lactoseFree]!,
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
onChanged: (checked) => ref
|
),
|
||||||
.read(filtersProvider.notifier)
|
subtitle: Text(
|
||||||
.setFilter(Filter.lactoseFree, checked),
|
'Only includes gluten-free meals',
|
||||||
title: Text(
|
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
||||||
'Lactose free',
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
style: Theme.of(context).textTheme.titleLarge!.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 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),
|
|
||||||
),
|
),
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
SwitchListTile(
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
value: _lactoseFreeFilterSet,
|
||||||
),
|
onChanged: (checked) => setState(() {
|
||||||
SwitchListTile(
|
_lactoseFreeFilterSet = checked;
|
||||||
value: ref.watch(filtersProvider)[Filter.vegan]!,
|
}),
|
||||||
onChanged: (checked) => ref
|
title: Text(
|
||||||
.read(filtersProvider.notifier)
|
'Lactose free',
|
||||||
.setFilter(Filter.vegan, checked),
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
title: Text(
|
color: Theme.of(context).colorScheme.onBackground),
|
||||||
'Vegan',
|
),
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
subtitle: Text(
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
'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),
|
||||||
),
|
),
|
||||||
subtitle: Text(
|
SwitchListTile(
|
||||||
'Only includes vegan meals',
|
value: _veggieFilterSet,
|
||||||
style: Theme.of(context).textTheme.labelMedium!.copyWith(
|
onChanged: (checked) => setState(() {
|
||||||
color: Theme.of(context).colorScheme.onBackground),
|
_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),
|
||||||
),
|
),
|
||||||
activeColor: Theme.of(context).colorScheme.tertiary,
|
SwitchListTile(
|
||||||
contentPadding: const EdgeInsets.only(left: 34, right: 22),
|
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),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue