From d2a6b3c2359facdd5a40e42593c8f1e7fa803e19 Mon Sep 17 00:00:00 2001 From: Muhammad Usama Date: Wed, 28 Jun 2023 17:42:06 +0500 Subject: [PATCH] tabs.dart is modified. --- lib/screens/tabs.dart | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/screens/tabs.dart b/lib/screens/tabs.dart index cc5cb77..a8772d5 100644 --- a/lib/screens/tabs.dart +++ b/lib/screens/tabs.dart @@ -1,10 +1,18 @@ import 'package:flutter/material.dart'; +import 'package:meals_app/data/dummy_data.dart'; import 'package:meals_app/models/meal.dart'; import 'package:meals_app/widgets/main_drawer.dart'; import 'package:meals_app/screens/categories.dart'; import 'package:meals_app/screens/meals.dart'; import 'package:meals_app/screens/filters.dart'; +const kInitialFilters = { + Filter.glutenFree: false, + Filter.lactoseFree: false, + Filter.vegetarian: false, + Filter.vegan: false, +}; + class TabsScreen extends StatefulWidget { const TabsScreen({super.key}); @override @@ -16,6 +24,7 @@ class TabsScreen extends StatefulWidget { class _TabsScreenState extends State { int _selectedPageIndex = 0; final List _favoriteMeals = []; + Map _selectedFilters = kInitialFilters; void _showInfoMessage(String message) { ScaffoldMessenger.of(context).clearSnackBars(); @@ -48,15 +57,21 @@ class _TabsScreenState extends State { }); } - void _setScreen(String identifier) { + void _setScreen(String identifier) async { Navigator.of(context).pop(); if (identifier == 'filters') { - Navigator.of(context).push( + final result = await Navigator.of(context).push>( // before it was pushReplacement. MaterialPageRoute( - builder: (ctx) => const FiltersScreen(), + builder: (ctx) => FiltersScreen( + currentFilters: _selectedFilters, + ), ), ); + setState(() { + _selectedFilters = result ?? kInitialFilters; + }); + // print(result); } // else { // Navigator.of(context).pop(); @@ -65,8 +80,25 @@ class _TabsScreenState extends State { @override Widget build(BuildContext context) { - Widget activePage = - CategoriesScreen(onToggleFavorite: _toggleMealFavoriteStatus); + final availableMeals = dummyMeals.where((meal) { + if (_selectedFilters[Filter.glutenFree]! && !meal.isGlutenFree) { + return false; + } + if (_selectedFilters[Filter.lactoseFree]! && !meal.isLactoseFree) { + return false; + } + if (_selectedFilters[Filter.vegetarian]! && !meal.isVegetarian) { + return false; + } + if (_selectedFilters[Filter.vegan]! && !meal.isVegan) { + return false; + } + return true; + }).toList(); + Widget activePage = CategoriesScreen( + onToggleFavorite: _toggleMealFavoriteStatus, + availableMeals: availableMeals, + ); var activePageTitle = 'Categories'; if (_selectedPageIndex == 1) {