gcx/frontend/genex-mobile/lib/features/transfer/presentation/providers/transfer_provider.dart

50 lines
1.5 KiB
Dart

// ============================================================
// TransferProvider — 转赠 Riverpod Providers
// ============================================================
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../../core/network/api_client.dart';
class TransferState {
final bool isLoading;
final bool isSuccess;
final String? error;
const TransferState({this.isLoading = false, this.isSuccess = false, this.error});
TransferState copyWith({bool? isLoading, bool? isSuccess, String? error, bool clearError = false}) {
return TransferState(
isLoading: isLoading ?? this.isLoading,
isSuccess: isSuccess ?? this.isSuccess,
error: clearError ? null : (error ?? this.error),
);
}
}
class TransferNotifier extends Notifier<TransferState> {
@override
TransferState build() => const TransferState();
Future<void> transfer({
required String holdingId,
required String recipientPhone,
int quantity = 1,
}) async {
state = state.copyWith(isLoading: true, clearError: true, isSuccess: false);
try {
final api = ApiClient.instance;
await api.post('/api/v1/transfers', data: {
'holdingId': holdingId,
'recipientPhone': recipientPhone,
'quantity': quantity,
});
state = state.copyWith(isLoading: false, isSuccess: true);
} catch (e) {
state = state.copyWith(isLoading: false, error: e.toString());
rethrow;
}
}
}
final transferProvider = NotifierProvider<TransferNotifier, TransferState>(TransferNotifier.new);