基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

counpon_widget.dart 5.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/widgets/goods_details/coupon/bloc/bloc.dart';
  4. import 'package:zhiying_base_widget/widgets/goods_details/coupon/bloc/counpon_repository.dart';
  5. import 'package:zhiying_base_widget/widgets/goods_details/coupon/counpon_sk.dart';
  6. import 'package:zhiying_base_widget/widgets/goods_details/coupon/model/counpon_model.dart';
  7. import 'package:zhiying_comm/zhiying_comm.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:cached_network_image/cached_network_image.dart';
  10. import 'package:provider/provider.dart';
  11. import 'package:zhiying_comm/util/turn_chain/turn_chain_util.dart';
  12. ///
  13. /// 优惠券widget
  14. ///
  15. class CounponWidget extends StatelessWidget {
  16. final Map<String, dynamic> model;
  17. const CounponWidget(this.model);
  18. @override
  19. Widget build(BuildContext context) {
  20. // return Container();
  21. return BlocProvider<CounponBloc>(
  22. create: (_) => CounponBloc(repository: CounponRepository()), //..add(CounponInitEvent(model: model)),
  23. child: CounponWidgetContainer(
  24. model,
  25. key: UniqueKey(),
  26. ),
  27. );
  28. }
  29. }
  30. class CounponWidgetContainer extends StatefulWidget {
  31. final Map<String, dynamic> model;
  32. const CounponWidgetContainer(this.model, {Key key}) : super(key: key);
  33. @override
  34. _CounponWidgetContainerState createState() => _CounponWidgetContainerState();
  35. }
  36. class _CounponWidgetContainerState extends State<CounponWidgetContainer> {
  37. UserInfoModel _user;
  38. @override
  39. void initState() {
  40. BlocProvider.of<CounponBloc>(context).add(CounponInitEvent(model: widget?.model));
  41. super.initState();
  42. }
  43. @override
  44. void didChangeDependencies() {
  45. _user = Provider.of<UserInfoNotifier>(context).userInfo;
  46. super.didChangeDependencies();
  47. }
  48. /// 点击领取
  49. void _onJump(CounponModel model) async{
  50. TurnChainUtil.openReceiveCoupon(context, _user, model?.good_id, model.provider, model?.convertArgs?.toJson());
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return BlocConsumer<CounponBloc, CounponState>(
  55. listener: (context, state) {},
  56. buildWhen: (prev, current) {
  57. if (current is CounponErrorState) {
  58. return false;
  59. }
  60. return true;
  61. },
  62. builder: (context, state) {
  63. if (state is CounponLoadedState) {
  64. return _getMainWidget(state.model);
  65. }
  66. return CounponSkeleton();
  67. },
  68. );
  69. }
  70. /// 主视图
  71. Widget _getMainWidget(CounponModel model) {
  72. return Visibility(
  73. visible: !EmptyUtil.isEmpty(model?.coupon_price),
  74. child: GestureDetector(
  75. onTap: () => _onJump(model),
  76. behavior: HitTestBehavior.opaque,
  77. child: Container(
  78. width: double.infinity,
  79. decoration: BoxDecoration(
  80. color: HexColor.fromHex(model?.bgColor),
  81. borderRadius: BorderRadius.only(
  82. topLeft: Radius.circular(ParseUtil.stringParseDouble(model?.topLeftRadius)),
  83. topRight: Radius.circular(ParseUtil.stringParseDouble(model?.topRightRadius)),
  84. bottomLeft: Radius.circular(ParseUtil.stringParseDouble(model?.bottomLeftRadius)),
  85. bottomRight: Radius.circular(ParseUtil.stringParseDouble(model?.bottomRightRadius))
  86. )
  87. ),
  88. margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(model?.topMargin), left: ParseUtil.stringParseDouble(model?.leftRightMargin), right: ParseUtil.stringParseDouble(model?.leftRightMargin)),
  89. padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 6, bottom: 6),
  90. child: Container(
  91. // color: Colors.red,
  92. width: double.infinity,
  93. padding: const EdgeInsets.only(left: 18.5, top: 12, bottom: 14),
  94. decoration: BoxDecoration(
  95. image: DecorationImage(
  96. image: CachedNetworkImageProvider(
  97. model?.bgImage ?? '',
  98. ),
  99. fit: BoxFit.fill)),
  100. alignment: Alignment.centerLeft,
  101. child: Row(
  102. children: <Widget>[
  103. /// 价格
  104. _getPriceWidget(model),
  105. const SizedBox(width: 7.5),
  106. /// 有效期
  107. _getTimeWidget(model)
  108. ],
  109. ),
  110. ),
  111. ),
  112. ),
  113. );
  114. }
  115. /// 价格
  116. Widget _getPriceWidget(CounponModel model) {
  117. return Row(
  118. crossAxisAlignment: CrossAxisAlignment.center,
  119. mainAxisAlignment: MainAxisAlignment.center,
  120. children: <Widget>[
  121. /// 价格类型
  122. Text('¥ ', style: TextStyle(fontSize: 15, color: HexColor.fromHex(model?.couponPriceColor ?? '#FFFFFF'))),
  123. /// 价格
  124. Text(model?.coupon_price ?? '100',
  125. style: TextStyle(fontSize: 30, color: HexColor.fromHex(model?.couponPriceColor ?? '#FFFFFF'), fontFamily: 'Din', package: 'zhiying_comm')),
  126. ],
  127. );
  128. }
  129. /// 名称与有效期
  130. Widget _getTimeWidget(CounponModel model) {
  131. return Column(
  132. crossAxisAlignment: CrossAxisAlignment.start,
  133. children: <Widget>[
  134. /// 标题
  135. Text(model?.couponText ?? '优惠券', style: TextStyle(fontSize: 17, color: HexColor.fromHex(model?.couponTextColor ?? '#FFFFFF'))),
  136. /// 到期时间
  137. Visibility(
  138. visible: !EmptyUtil.isEmpty(model?.coupon_endtime),
  139. child: Text(model?.coupon_endtime ?? '有效期至2020-10-01', style: TextStyle(fontSize: 10, color: HexColor.fromHex(model?.couponTimeColor ?? '#FFFFFF'))))
  140. ],
  141. );
  142. }
  143. }