73 lines
1.8 KiB
Dart
73 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_common/widget/common_widget.dart';
|
|
import 'package:food_hub_app/provider/food_provider.dart';
|
|
import 'package:food_hub_app/widgets/recipe/card.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class RecipeList extends StatefulWidget {
|
|
final int userId;
|
|
|
|
const RecipeList({super.key, required this.userId});
|
|
|
|
@override
|
|
State<RecipeList> createState() => _RecipeListState();
|
|
}
|
|
|
|
class _RecipeListState extends State<RecipeList> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// 初始化加载数据
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (widget.userId == 0) {
|
|
context.read<FoodProvider>().refreshRecipeList();
|
|
} else {
|
|
context.read<FoodProvider>().queryRecipeByUserId(widget.userId);
|
|
}
|
|
});
|
|
}
|
|
|
|
Widget _buildRecipeList(FoodProvider provider) {
|
|
return GridView.builder(
|
|
padding: const EdgeInsets.only(bottom: 80),
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 2,
|
|
crossAxisSpacing: 8,
|
|
mainAxisSpacing: 8,
|
|
childAspectRatio: 0.85,
|
|
),
|
|
itemCount: provider.recipeSummaryList.length,
|
|
itemBuilder: (context, index) {
|
|
return RecipeCard(recipe: provider.recipeSummaryList[index]);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildContent(FoodProvider provider) {
|
|
if (provider.recipeSummaryList.isEmpty) {
|
|
return buildEmptyData();
|
|
} else {
|
|
return Column(
|
|
children: [
|
|
Expanded(child: _buildRecipeList(provider)),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<FoodProvider>();
|
|
|
|
return Stack(
|
|
children: [
|
|
if (provider.isLoading)
|
|
buildLoadingIndicator()
|
|
else
|
|
_buildContent(provider),
|
|
],
|
|
);
|
|
}
|
|
}
|