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 '../models/category.dart';
|
||||
import '../models/meal.dart';
|
||||
import '../widgets/category_grid_item.dart';
|
||||
|
||||
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) {
|
||||
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(
|
||||
MaterialPageRoute(
|
||||
builder: (ctx) => MealsScreen(
|
||||
title: category.title,
|
||||
meals: filteredMeals,
|
||||
onToggleFavorites: onToggleFavorites,
|
||||
),
|
||||
),
|
||||
); //Navigator.push(context, route);
|
||||
@@ -24,22 +33,22 @@ class CategoriesScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GridView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
childAspectRatio: 3 / 2,
|
||||
crossAxisSpacing: 20,
|
||||
mainAxisSpacing: 20,
|
||||
),
|
||||
children: [
|
||||
for (final category in availableCategories)
|
||||
CategoryGridItem(
|
||||
category: category,
|
||||
onSelectCategory: () {
|
||||
_selectCategory(context, category);
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
padding: const EdgeInsets.all(24),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
childAspectRatio: 3 / 2,
|
||||
crossAxisSpacing: 20,
|
||||
mainAxisSpacing: 20,
|
||||
),
|
||||
children: [
|
||||
for (final category in availableCategories)
|
||||
CategoryGridItem(
|
||||
category: category,
|
||||
onSelectCategory: () {
|
||||
_selectCategory(context, category);
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,23 @@ class MealDetailsScreen extends StatelessWidget {
|
||||
const MealDetailsScreen({
|
||||
super.key,
|
||||
required this.meal,
|
||||
required this.onToggleFavorites,
|
||||
});
|
||||
|
||||
final Meal meal;
|
||||
final void Function(Meal meal) onToggleFavorites;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(meal.title),
|
||||
),
|
||||
appBar: AppBar(title: Text(meal.title), actions: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
onToggleFavorites(meal);
|
||||
},
|
||||
icon: const Icon(Icons.star),
|
||||
)
|
||||
]),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
|
||||
@@ -9,15 +9,18 @@ class MealsScreen extends StatelessWidget {
|
||||
super.key,
|
||||
this.title,
|
||||
required this.meals,
|
||||
required this.onToggleFavorites,
|
||||
});
|
||||
|
||||
final String? title;
|
||||
final List<Meal> meals;
|
||||
final void Function(Meal meal) onToggleFavorites;
|
||||
|
||||
void selectMeal(BuildContext context, Meal meal) {
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (ctx) => MealDetailsScreen(
|
||||
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/meals.dart';
|
||||
|
||||
import '../models/meal.dart';
|
||||
|
||||
class TabsScreen extends StatefulWidget {
|
||||
const TabsScreen({super.key});
|
||||
|
||||
@@ -13,6 +15,28 @@ class TabsScreen extends StatefulWidget {
|
||||
|
||||
class _TabsScreenState extends State<TabsScreen> {
|
||||
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) {
|
||||
setState(() {
|
||||
@@ -22,11 +46,16 @@ class _TabsScreenState extends State<TabsScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget activePage = const CategoriesScreen();
|
||||
Widget activePage = CategoriesScreen(
|
||||
onToggleFavorites: _toggleMealFavoriteStatus,
|
||||
);
|
||||
var activePageTitle = 'Categories';
|
||||
|
||||
if (_selectedPageIndex == 1) {
|
||||
activePage = const MealsScreen(meals: []);
|
||||
activePage = MealsScreen(
|
||||
meals: _favoriteMeals,
|
||||
onToggleFavorites: _toggleMealFavoriteStatus,
|
||||
);
|
||||
activePageTitle = 'Your Favorites';
|
||||
}
|
||||
|
||||
|
||||
@@ -34,8 +34,8 @@ class CategoryGridItem extends StatelessWidget {
|
||||
child: Text(
|
||||
category.title,
|
||||
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