flutter-demo/lib/provider/item_category_provider.dart
LingandRX b53fe04e54 feat(category): 添加分类功能
- 新增分类列表页面和分类详情页面
- 实现分类数据的加载、刷新和展示
- 添加分类相关的数据模型、提供者和仓库
- 优化数据库操作,统一表名和查询条件
2025-05-08 21:06:52 +08:00

40 lines
1.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:item_tracker/models/item_category_model.dart';
import 'package:item_tracker/repository/item_category_repository.dart';
class ItemCategoryProvider extends ChangeNotifier {
List<ItemCategory> _categories = [];
List<ItemCategory> get categories => _categories;
final ItemCategoryRepository repository;
ItemCategoryProvider({required this.repository});
Future<void> loadCategories() async {
_categories = await repository.getAll();
notifyListeners();
}
Future<void> addCategory(ItemCategory) async {
await repository.insert(ItemCategory);
await loadCategories();
}
Future<void> updateCategory(ItemCategory) async {
await repository.update(ItemCategory);
await loadCategories();
}
Future<void> deleteCategory(ItemCategory) async {
await repository.delete(ItemCategory.id!);
await loadCategories();
}
Future<ItemCategory?> getCategoryById(int id) async {
final data = await repository.getById(id);
return data;
}
}