168 Start
This commit is contained in:
+28
-19
@@ -3,19 +3,28 @@ import 'package:flutter_navigation/data/dummy_data.dart';
|
|||||||
import 'package:flutter_navigation/screens/meals.dart';
|
import 'package:flutter_navigation/screens/meals.dart';
|
||||||
|
|
||||||
import '../models/category.dart';
|
import '../models/category.dart';
|
||||||
|
import '../models/meal.dart';
|
||||||
import '../widgets/category_grid_item.dart';
|
import '../widgets/category_grid_item.dart';
|
||||||
|
|
||||||
class CategoriesScreen extends StatelessWidget {
|
class CategoriesScreen extends StatelessWidget {
|
||||||
const CategoriesScreen({super.key});
|
const CategoriesScreen({
|
||||||
|
super.key,
|
||||||
|
required this.onToggleFavorites,
|
||||||
|
});
|
||||||
|
|
||||||
|
final void Function(Meal meal) onToggleFavorites;
|
||||||
|
|
||||||
void _selectCategory(BuildContext context, Category category) {
|
void _selectCategory(BuildContext context, Category category) {
|
||||||
final filteredMeals = dummyMeals.where((meal) => meal.categories.contains(category.id)).toList();
|
final filteredMeals = dummyMeals
|
||||||
|
.where((meal) => meal.categories.contains(category.id))
|
||||||
|
.toList();
|
||||||
|
|
||||||
Navigator.of(context).push(
|
Navigator.of(context).push(
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (ctx) => MealsScreen(
|
builder: (ctx) => MealsScreen(
|
||||||
title: category.title,
|
title: category.title,
|
||||||
meals: filteredMeals,
|
meals: filteredMeals,
|
||||||
|
onToggleFavorites: onToggleFavorites,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
); //Navigator.push(context, route);
|
); //Navigator.push(context, route);
|
||||||
@@ -24,22 +33,22 @@ class CategoriesScreen extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return GridView(
|
return GridView(
|
||||||
padding: const EdgeInsets.all(24),
|
padding: const EdgeInsets.all(24),
|
||||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
crossAxisCount: 2,
|
crossAxisCount: 2,
|
||||||
childAspectRatio: 3 / 2,
|
childAspectRatio: 3 / 2,
|
||||||
crossAxisSpacing: 20,
|
crossAxisSpacing: 20,
|
||||||
mainAxisSpacing: 20,
|
mainAxisSpacing: 20,
|
||||||
),
|
),
|
||||||
children: [
|
children: [
|
||||||
for (final category in availableCategories)
|
for (final category in availableCategories)
|
||||||
CategoryGridItem(
|
CategoryGridItem(
|
||||||
category: category,
|
category: category,
|
||||||
onSelectCategory: () {
|
onSelectCategory: () {
|
||||||
_selectCategory(context, category);
|
_selectCategory(context, category);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,16 +6,23 @@ class MealDetailsScreen extends StatelessWidget {
|
|||||||
const MealDetailsScreen({
|
const MealDetailsScreen({
|
||||||
super.key,
|
super.key,
|
||||||
required this.meal,
|
required this.meal,
|
||||||
|
required this.onToggleFavorites,
|
||||||
});
|
});
|
||||||
|
|
||||||
final Meal meal;
|
final Meal meal;
|
||||||
|
final void Function(Meal meal) onToggleFavorites;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(title: Text(meal.title), actions: [
|
||||||
title: Text(meal.title),
|
IconButton(
|
||||||
),
|
onPressed: () {
|
||||||
|
onToggleFavorites(meal);
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.star),
|
||||||
|
)
|
||||||
|
]),
|
||||||
body: SingleChildScrollView(
|
body: SingleChildScrollView(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
@@ -9,15 +9,18 @@ class MealsScreen extends StatelessWidget {
|
|||||||
super.key,
|
super.key,
|
||||||
this.title,
|
this.title,
|
||||||
required this.meals,
|
required this.meals,
|
||||||
|
required this.onToggleFavorites,
|
||||||
});
|
});
|
||||||
|
|
||||||
final String? title;
|
final String? title;
|
||||||
final List<Meal> meals;
|
final List<Meal> meals;
|
||||||
|
final void Function(Meal meal) onToggleFavorites;
|
||||||
|
|
||||||
void selectMeal(BuildContext context, Meal meal) {
|
void selectMeal(BuildContext context, Meal meal) {
|
||||||
Navigator.of(context).push(MaterialPageRoute(
|
Navigator.of(context).push(MaterialPageRoute(
|
||||||
builder: (ctx) => MealDetailsScreen(
|
builder: (ctx) => MealDetailsScreen(
|
||||||
meal: meal,
|
meal: meal,
|
||||||
|
onToggleFavorites: onToggleFavorites,
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-2
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_navigation/screens/categories.dart';
|
import 'package:flutter_navigation/screens/categories.dart';
|
||||||
import 'package:flutter_navigation/screens/meals.dart';
|
import 'package:flutter_navigation/screens/meals.dart';
|
||||||
|
|
||||||
|
import '../models/meal.dart';
|
||||||
|
|
||||||
class TabsScreen extends StatefulWidget {
|
class TabsScreen extends StatefulWidget {
|
||||||
const TabsScreen({super.key});
|
const TabsScreen({super.key});
|
||||||
|
|
||||||
@@ -13,6 +15,28 @@ class TabsScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class _TabsScreenState extends State<TabsScreen> {
|
class _TabsScreenState extends State<TabsScreen> {
|
||||||
int _selectedPageIndex = 0;
|
int _selectedPageIndex = 0;
|
||||||
|
final List<Meal> _favoriteMeals = [];
|
||||||
|
|
||||||
|
void _showInfoMessage(String message) {
|
||||||
|
ScaffoldMessenger.of(context).clearSnackBars();
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text(message)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _toggleMealFavoriteStatus(Meal meal) {
|
||||||
|
final isExisting = _favoriteMeals.contains(meal);
|
||||||
|
|
||||||
|
if (isExisting) {
|
||||||
|
setState(() {
|
||||||
|
_favoriteMeals.remove(meal);
|
||||||
|
_showInfoMessage('Meal is no longer a favorite.');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
_favoriteMeals.add(meal);
|
||||||
|
_showInfoMessage('Marked as a favorite.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _selectPage(int index) {
|
void _selectPage(int index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -22,11 +46,16 @@ class _TabsScreenState extends State<TabsScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
Widget activePage = const CategoriesScreen();
|
Widget activePage = CategoriesScreen(
|
||||||
|
onToggleFavorites: _toggleMealFavoriteStatus,
|
||||||
|
);
|
||||||
var activePageTitle = 'Categories';
|
var activePageTitle = 'Categories';
|
||||||
|
|
||||||
if (_selectedPageIndex == 1) {
|
if (_selectedPageIndex == 1) {
|
||||||
activePage = const MealsScreen(meals: []);
|
activePage = MealsScreen(
|
||||||
|
meals: _favoriteMeals,
|
||||||
|
onToggleFavorites: _toggleMealFavoriteStatus,
|
||||||
|
);
|
||||||
activePageTitle = 'Your Favorites';
|
activePageTitle = 'Your Favorites';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ class CategoryGridItem extends StatelessWidget {
|
|||||||
child: Text(
|
child: Text(
|
||||||
category.title,
|
category.title,
|
||||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||||
color: Theme.of(context).colorScheme.onBackground,
|
color: Theme.of(context).colorScheme.onBackground,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user