食谱簿:还没有收藏内容
本课属于 Coddy Flutter 学习旅程的“列表和屏幕”单元。第 33 课,共 33 课。
在收藏任何内容之前,先打开“收藏”选项卡:屏幕是空白的,看起来像是出了问题。列表为空时,应该说明为空的原因。
当列表中没有任何项目时,列表的 isEmpty 为 true。在 build 中、return 之前,用 if 选择页面:
Widget page;
if (index == 0) {
page = recipeList(recipes);
} else if (favorites.isEmpty) {
page = const Center(child: Text('No favorites yet'));
} else {
page = recipeList(favorites);
}Widget 类型的变量可以保存任何组件,因此 page 在一个分支中是列表,在另一个分支中则是一条消息。然后,Scaffold 的 body 就是 page。
挑战
简单“收藏”标签页没有内容可显示时,在屏幕中央显示 No favorites yet,字号为 18,替代空列表。收藏食谱后,它会再次显示在该标签页中。
亲手试试
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final List<Recipe> recipes = [
Recipe(name: 'Pancakes', minutes: 20, ingredients: ['Flour', 'Eggs', 'Milk', 'Butter']),
Recipe(name: 'Tomato Soup', minutes: 35, ingredients: ['Tomatoes', 'Onion', 'Garlic', 'Olive oil']),
Recipe(name: 'Guacamole', minutes: 10, ingredients: ['Avocados', 'Lime', 'Onion', 'Salt']),
Recipe(name: 'Fried Rice', minutes: 25, ingredients: ['Rice', 'Eggs', 'Peas', 'Soy sauce']),
];
int index = 0;
Widget recipeList(List<Recipe> shown) {
return ListView.builder(
itemCount: shown.length,
itemBuilder: (context, i) {
final Recipe recipe = shown[i];
return ListTile(
leading: const Icon(Icons.restaurant_menu),
title: Text(recipe.name),
subtitle: Text('${recipe.minutes} min'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RecipeScreen(recipe: recipe)),
);
},
trailing: IconButton(
icon: Icon(
recipe.favorite ? Icons.favorite : Icons.favorite_border,
color: recipe.favorite ? Colors.red : null,
),
onPressed: () {
setState(() {
recipe.favorite = !recipe.favorite;
});
},
),
);
},
);
}
@override
Widget build(BuildContext context) {
final List<Recipe> favorites = recipes.where((recipe) => recipe.favorite).toList();
return Scaffold(
appBar: AppBar(title: const Text('Recipe Book')),
body: index == 0 ? recipeList(recipes) : recipeList(favorites),
bottomNavigationBar: NavigationBar(
selectedIndex: index,
onDestinationSelected: (int i) {
setState(() {
index = i;
});
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.menu_book_outlined),
selectedIcon: Icon(Icons.menu_book),
label: 'Recipes',
),
NavigationDestination(
icon: Icon(Icons.star_border),
selectedIcon: Icon(Icons.star),
label: 'Favorites',
),
],
),
);
}
}
class RecipeScreen extends StatelessWidget {
const RecipeScreen({super.key, required this.recipe});
final Recipe recipe;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(recipe.name)),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
Text('Ready in ${recipe.minutes} minutes', style: const TextStyle(fontSize: 18)),
const SizedBox(height: 24),
const Text('Ingredients', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
for (String ingredient in recipe.ingredients)
ListTile(
leading: const Icon(Icons.check_circle_outline),
title: Text(ingredient),
),
],
),
);
}
}
class Recipe {
final String name;
final int minutes;
final List<String> ingredients;
bool favorite = false;
Recipe({required this.name, required this.minutes, required this.ingredients});
}
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
列表和屏幕中的所有课
自己练习: Flutter 在线编译器