116 lines
3.2 KiB
Dart
116 lines
3.2 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 {
|
|
const RecipeList({super.key});
|
|
|
|
@override
|
|
State<RecipeList> createState() => _RecipeListState();
|
|
}
|
|
|
|
class _RecipeListState extends State<RecipeList> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
// 初始化加载数据
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<FoodProvider>().refreshRecipeList();
|
|
context.read<FoodProvider>().queryCategoryList();
|
|
});
|
|
}
|
|
|
|
Widget _buildSelectCategory(FoodProvider provider) {
|
|
final colors = Theme.of(context).colorScheme;
|
|
|
|
return Container(
|
|
width: 140,
|
|
decoration: BoxDecoration(
|
|
color: colors.surfaceContainer,
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
padding: EdgeInsets.symmetric(horizontal: 16),
|
|
child: DropdownButton<String>(
|
|
value: provider.queryCategory,
|
|
isExpanded: true,
|
|
borderRadius: BorderRadius.circular(12),
|
|
dropdownColor: colors.surfaceContainer,
|
|
elevation: 6,
|
|
underline: Container(),
|
|
items:
|
|
provider.categoryList.map((String value) {
|
|
return DropdownMenuItem<String>(
|
|
value: value,
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 8,
|
|
height: 8,
|
|
decoration: BoxDecoration(
|
|
color: colors.primary,
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(value, style: TextStyle(fontSize: 14)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList(),
|
|
onChanged: (value) {
|
|
provider.queryCategory = value!;
|
|
provider.refreshRecipeList();
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildRecipeList(FoodProvider provider) {
|
|
return GridView.builder(
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
|
crossAxisCount: 2,
|
|
crossAxisSpacing: 8,
|
|
mainAxisSpacing: 8,
|
|
childAspectRatio: 0.9,
|
|
),
|
|
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: [
|
|
_buildSelectCategory(provider),
|
|
SizedBox(height: 5),
|
|
Expanded(child: _buildRecipeList(provider)),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<FoodProvider>();
|
|
|
|
return Stack(
|
|
children: [
|
|
if (provider.isLoading)
|
|
buildLoadingIndicator()
|
|
else
|
|
_buildContent(provider),
|
|
],
|
|
);
|
|
}
|
|
}
|