bloc.dart 1.4 KB
import 'package:bloc/bloc.dart';
import 'package:wow_english/models/product_entity.dart';

import '../../utils/log_util.dart';
import 'event.dart';
import 'state.dart';

class ShoppingBloc extends Bloc<ShoppingEvent, ShoppingState> {

  ProductEntity? _productData;

  ProductEntity? get productData => _productData;

  PaymentChannel _curPaymentChannel = PaymentChannel.wechatPay;

  PaymentChannel get curPaymentChannel => _curPaymentChannel;


  ShoppingBloc() : super(ShoppingState().init()) {
    //页面初始化时刻
    on<InitEvent>(_init);
    on<ChangePaymentChannelEvent>(_changePaymentChannel);
  }

  void _init(InitEvent event, Emitter<ShoppingState> emit) async {
    //处理一些初始化操作,然后刷新界面
    emit(state.clone());
  }

  /// 更换支付渠道
  void _changePaymentChannel(ChangePaymentChannelEvent event,
      Emitter<ShoppingState> emitter) async {
    Log.d("_curPaymentChannel=$_curPaymentChannel, event.paymentChannel=${event.paymentChannel}");
    if (_curPaymentChannel != event.paymentChannel) {
      _curPaymentChannel = event.paymentChannel;
      emitter(PaymentChannelChangeState());
    }
  }

  void _gotoPay() {
    // 跳转到支付页面
  }
}

enum PaymentChannel {

  wechatPay(1, "微信支付"),

  aliPay(2, "支付宝支付");

  const PaymentChannel(this.payChannelType, this.payChannelName);

  final int payChannelType;

  final String payChannelName;
}