feat(): toggle favorite meals
parent
083a617c95
commit
7e09c1f81f
@ -0,0 +1,80 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../models/Meal.dart';
|
||||
|
||||
class MealDetailScreen extends StatelessWidget {
|
||||
final Meal meal;
|
||||
final void Function(Meal meal) onToggleFavorite;
|
||||
|
||||
const MealDetailScreen(
|
||||
{Key? key, required this.meal, required this.onToggleFavorite})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(meal.title),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () => onToggleFavorite(meal),
|
||||
icon: const Icon(Icons.star))
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
Image.network(
|
||||
meal.imageUrl,
|
||||
width: double.infinity,
|
||||
height: 300,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
const SizedBox(
|
||||
height: 14,
|
||||
),
|
||||
Text(
|
||||
'Ingredients',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 14,
|
||||
),
|
||||
for (final ingredient in meal.ingredients)
|
||||
Text(
|
||||
ingredient,
|
||||
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 14,
|
||||
),
|
||||
Text(
|
||||
'Steps',
|
||||
style: Theme.of(context).textTheme.titleLarge!.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
height: 14,
|
||||
),
|
||||
for (final step in meal.steps)
|
||||
Padding(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
child: Text(
|
||||
step,
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:meals/screens/CategoriesScreen.dart';
|
||||
import 'package:meals/screens/MealsScreen.dart';
|
||||
|
||||
import '../models/Meal.dart';
|
||||
|
||||
class TabsScreen extends StatefulWidget {
|
||||
const TabsScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<TabsScreen> createState() => _TabsScreenState();
|
||||
}
|
||||
|
||||
class _TabsScreenState extends State<TabsScreen> {
|
||||
int _selectedPageIndex = 0;
|
||||
final List<Meal> _favoriteMeals = [];
|
||||
|
||||
void _selectPage(int index) {
|
||||
setState(() {
|
||||
_selectedPageIndex = index;
|
||||
});
|
||||
}
|
||||
|
||||
void _toggleFavoriteMeal(Meal meal) {
|
||||
if (_favoriteMeals.contains(meal)) {
|
||||
setState(() {
|
||||
_favoriteMeals.remove(meal);
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_favoriteMeals.add(meal);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget activePage;
|
||||
String? activePageTitle;
|
||||
|
||||
switch (_selectedPageIndex) {
|
||||
case 1:
|
||||
activePageTitle = 'Favorites';
|
||||
activePage = MealsScreen(
|
||||
meals: _favoriteMeals,
|
||||
onToggleFavorite: _toggleFavoriteMeal,
|
||||
);
|
||||
break;
|
||||
default:
|
||||
activePageTitle = 'Pick your category';
|
||||
activePage = CategoriesScreen(
|
||||
onToggleFavorite: _toggleFavoriteMeal,
|
||||
);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(activePageTitle)),
|
||||
body: activePage,
|
||||
bottomNavigationBar: BottomNavigationBar(
|
||||
onTap: _selectPage,
|
||||
currentIndex: _selectedPageIndex,
|
||||
items: const [
|
||||
BottomNavigationBarItem(
|
||||
icon: Icon(Icons.set_meal), label: 'Categories'),
|
||||
BottomNavigationBarItem(icon: Icon(Icons.star), label: 'Favorites'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue