Files
jiu/client/lib/providers/stock_in_provider.dart
T
wangjia a678c3806e
Deploy Client / build-client-web (push) Successful in 44s
Deploy Client / build-macos (push) Successful in 2m29s
Deploy Client / build-android (push) Successful in 1m23s
Deploy Client / build-ios (push) Successful in 2m59s
Deploy Client / build-windows (push) Successful in 1m52s
Deploy Client / release-deploy-client (push) Successful in 2m40s
chore: release client-v1.0.79
入库/出库单搜索框升级:搜索后关键词转为框内 chip 标签 + 一键清除(✕),仅保留单个搜索词;新增 SearchChip 组件 + provider currentKeyword getter 进屏同步。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZ4DskSRKsSiheQonFtQvx
2026-06-24 12:08:55 +08:00

151 lines
4.0 KiB
Dart

import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../core/api/api_client.dart';
import '../core/auth/auth_state.dart';
import '../core/config/app_constants.dart';
import '../core/models/page_result.dart';
import '../models/stock_in.dart';
import '../repositories/stock_in_repository.dart';
import 'connectivity_provider.dart';
import 'inventory_provider.dart';
final stockInRepositoryProvider = Provider<StockInRepository>((ref) {
return StockInRepository(ref.watch(apiClientProvider));
});
final stockInListProvider =
AsyncNotifierProvider<StockInListNotifier, PageResult<StockInOrder>>(
StockInListNotifier.new,
);
class StockInListNotifier extends AsyncNotifier<PageResult<StockInOrder>> {
int _page = 1;
int _pageSize = AppConstants.defaultPageSize;
String _status = '';
String? _startDate;
String? _endDate;
String _keyword = '';
PageResult<StockInOrder>? _cache;
Timer? _searchDebounce;
@override
Future<PageResult<StockInOrder>> build() async {
ref.onDispose(() => _searchDebounce?.cancel());
ref.watch(authStateProvider.select((s) => s.user?.shopId));
ref.watch(networkRecoveryCountProvider);
try {
final result = await _fetch();
_cache = result;
return result;
} catch (_) {
if (_cache != null) return _cache!;
rethrow;
}
}
Future<PageResult<StockInOrder>> _fetch() {
return ref.read(stockInRepositoryProvider).list(
status: _status.isEmpty ? null : _status,
startDate: _startDate,
endDate: _endDate,
keyword: _keyword.isEmpty ? null : _keyword,
page: _page,
pageSize: _pageSize,
);
}
void setPage(int page) {
_page = page;
reload();
}
void setPageSize(int pageSize) {
_pageSize = pageSize;
_page = 1;
reload();
}
/// 当前服务端状态过滤值(供页面进入时对齐标签/下拉,避免重复拉取)
String get currentStatus => _status;
String get currentKeyword => _keyword;
void setStatus(String status) {
_status = status;
_page = 1;
reload();
}
void setDateRange(String? startDate, String? endDate) {
_startDate = startDate;
_endDate = endDate;
_page = 1;
reload();
}
/// 关键词搜索(含酒名模糊反查,单次约 55ms 的明细扫描)。
/// 去重 + 防抖限流:同词不重查;连发回车/狂点合并取最后一次,避免昂贵查询被刷。
void setKeyword(String keyword) {
final kw = keyword.trim();
if (kw == _keyword) return;
_searchDebounce?.cancel();
_searchDebounce = Timer(const Duration(milliseconds: 350), () {
_keyword = kw;
_page = 1;
reload();
});
}
void reload() {
state = const AsyncValue.loading();
_fetch().then((result) {
_cache = result;
state = AsyncValue.data(result);
}, onError: (e, st) {
if (_cache != null) {
state = AsyncValue.data(_cache!);
} else {
state = AsyncValue.error(e, st);
}
});
}
Future<void> createOrder(Map<String, dynamic> data) async {
await ref.read(stockInRepositoryProvider).create(data);
reload();
}
Future<void> deleteOrder(int id) async {
await ref.read(stockInRepositoryProvider).delete(id);
reload();
}
Future<void> submitOrder(int id) async {
await ref.read(stockInRepositoryProvider).submit(id);
reload();
}
Future<void> approveOrder(int id) async {
await ref.read(stockInRepositoryProvider).approve(id);
reload();
ref.invalidate(inventoryListProvider);
}
Future<void> rejectOrder(int id) async {
await ref.read(stockInRepositoryProvider).reject(id);
reload();
}
Future<void> withdrawOrder(int id) async {
await ref.read(stockInRepositoryProvider).withdraw(id);
reload();
}
Future<void> returnItems(int id, List<int> itemIds) async {
await ref.read(stockInRepositoryProvider).returnItems(id, itemIds);
reload();
ref.invalidate(inventoryListProvider);
}
}