| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import 'package:flutter/foundation.dart';
- import '../models/document.dart';
- import '../services/mock_data_service.dart';
- /// 文档状态管理
- class DocumentProvider with ChangeNotifier {
- List<Document> _documents = [];
- Document? _selectedDocument;
- bool _loading = false;
- List<Document> get documents => _documents;
- Document? get selectedDocument => _selectedDocument;
- bool get loading => _loading;
- DocumentProvider() {
- loadDocuments();
- }
- /// 加载文档列表
- Future<void> loadDocuments() async {
- _loading = true;
- notifyListeners();
- // 模拟网络请求
- await Future.delayed(const Duration(milliseconds: 500));
- _documents = MockDataService.getMockDocuments();
- _loading = false;
- notifyListeners();
- }
- /// 选择文档
- void selectDocument(Document document) {
- _selectedDocument = document;
- notifyListeners();
- }
- /// 添加文档
- void addDocument(Document document) {
- _documents.insert(0, document);
- notifyListeners();
- }
- /// 更新文档
- void updateDocument(Document document) {
- final index = _documents.indexWhere((d) => d.id == document.id);
- if (index != -1) {
- _documents[index] = document;
- notifyListeners();
- }
- }
- /// 删除文档
- void deleteDocument(String id) {
- _documents.removeWhere((d) => d.id == id);
- if (_selectedDocument?.id == id) {
- _selectedDocument = null;
- }
- notifyListeners();
- }
- /// 根据ID获取文档
- Document? getDocumentById(String id) {
- try {
- return _documents.firstWhere((d) => d.id == id);
- } catch (e) {
- return null;
- }
- }
- }
|