342 lines
11 KiB
Dart
342 lines
11 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
import 'package:game/network/NetworkConfig.dart';
|
|
import 'package:game/tools/shop/shop_model.dart';
|
|
|
|
import '../../beans/create_order_bean.dart';
|
|
import '../../beans/mall_bean.dart';
|
|
import '../../common/EventBusUtil.dart';
|
|
import '../../common/Global.dart';
|
|
import '../../common/func.dart';
|
|
|
|
class ShopPage extends StatefulWidget {
|
|
const ShopPage({super.key});
|
|
|
|
@override
|
|
State<ShopPage> createState() => _ShopPageState();
|
|
}
|
|
|
|
class _ShopPageState extends State<ShopPage> with AutomaticKeepAliveClientMixin {
|
|
late StreamSubscription subscription;
|
|
final ShopModel _viewModel = ShopModel();
|
|
StreamSubscription<PaySuccess>? _paySuccess;
|
|
late StreamSubscription<RefreshUserdata> _refreshUserEvent;
|
|
|
|
List<MallBean> goodList = [];
|
|
|
|
int goodIndex = 0;
|
|
|
|
String paymentMethod = "wx";
|
|
|
|
int payType = 0; //0 微信 1 支付宝
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
|
|
//支付完成 验单
|
|
_paySuccess = EventBusUtil.listen((event) {
|
|
_viewModel.getOrderRewardsInfo();
|
|
});
|
|
|
|
//刷新信息
|
|
_refreshUserEvent = EventBusUtil.listen((event) {
|
|
_viewModel.getUserInfo();
|
|
});
|
|
|
|
subscription = _viewModel.streamController.stream.listen((event) {
|
|
String code = event['code'];
|
|
if (code.isNotEmpty) {
|
|
switch (code) {
|
|
case "getDiamondMall":
|
|
EasyLoading.dismiss();
|
|
goodList = event['data'];
|
|
break;
|
|
|
|
case "createOrderWx": //微信支付
|
|
CreateOrderBean createOrderBean = event['data'];
|
|
|
|
Map<String, dynamic> map = {
|
|
"orderInfoWx": createOrderBean.payment,
|
|
};
|
|
invokeNativeMethod("WxPay", map);
|
|
|
|
break;
|
|
|
|
case "createOrderZfb": //支付宝支付
|
|
CreateOrderBean createOrderBean = event['data'];
|
|
|
|
Map<String, dynamic> map = {
|
|
"orderInfoZfb": createOrderBean.payment,
|
|
};
|
|
invokeNativeMethod("Alipay", map);
|
|
break;
|
|
|
|
case "getOrderRewardsInfo": //获取订单状态
|
|
loadData();
|
|
EasyLoading.showToast(event['data']);
|
|
EventBusUtil.fire(RefreshUserdata());
|
|
break;
|
|
}
|
|
setState(() {});
|
|
}
|
|
});
|
|
loadData();
|
|
}
|
|
|
|
loadData() {
|
|
FunctionUtil.loading();
|
|
_viewModel.getDiamondMall();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
_paySuccess?.cancel();
|
|
_refreshUserEvent.cancel();
|
|
subscription.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final size = MediaQuery.of(context).size;
|
|
final h25 = size.width / 14.4;
|
|
final s16 = size.width / 22.5;
|
|
final l14 = size.width / 25.71428571428571;
|
|
final t36 = size.width / 10;
|
|
final l20 = size.width / 18;
|
|
final t17 = size.width / 21.17647058823529;
|
|
final l18 = size.width / 20;
|
|
final t35 = size.width / 10.28571428571429;
|
|
final s29 = size.width / 12.41379310344828;
|
|
final h140 = size.width / 2.571428571428571;
|
|
final w157 = size.width / 2.292993630573248;
|
|
final h44 = size.width / 8.181818181818182;
|
|
final c22 = size.width / 16.36363636363636;
|
|
final l11 = size.width / 32.727272727272;
|
|
final s14 = size.width / 25.714285714285;
|
|
final s13 = size.width / 27.692307692307;
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFF17181A),
|
|
body: Column(
|
|
children: [
|
|
Container(
|
|
width: size.width,
|
|
height: h25,
|
|
alignment: Alignment.center,
|
|
margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
|
child: Text(
|
|
"钻石商城",
|
|
style: TextStyle(color: const Color(0xFFD6D6D7), fontSize: s16),
|
|
),
|
|
),
|
|
Container(
|
|
width: size.width,
|
|
alignment: Alignment.center,
|
|
margin: EdgeInsets.only(left: l14, right: l14, top: t36),
|
|
child: Stack(
|
|
children: [
|
|
const Image(image: AssetImage('assets/images/diamond_bg.png')),
|
|
Positioned(
|
|
left: l20,
|
|
top: t17,
|
|
child: Text(
|
|
"当前钻石数量",
|
|
style: TextStyle(fontSize: l11, color: const Color(0xFFE2F3FF)),
|
|
)),
|
|
Positioned(
|
|
left: l18,
|
|
top: t35,
|
|
child: Text(
|
|
"${NetworkConfig.userInfoBean!.diamond}",
|
|
style: TextStyle(fontSize: s29, color: Colors.white),
|
|
)),
|
|
],
|
|
),
|
|
),
|
|
|
|
///商品列表
|
|
Expanded(
|
|
child: Container(
|
|
margin: EdgeInsets.only(left: l14, right: l14),
|
|
child: GridView.count(
|
|
crossAxisCount: 2,
|
|
crossAxisSpacing: l18,
|
|
mainAxisSpacing: t36,
|
|
childAspectRatio: 0.844,
|
|
children: _goodItem(context),
|
|
),
|
|
),
|
|
),
|
|
|
|
///支付
|
|
Container(
|
|
width: size.width,
|
|
height: h140,
|
|
color: const Color(0xFF171A22),
|
|
padding: EdgeInsets.symmetric(horizontal: l14),
|
|
child: Column(
|
|
children: [
|
|
///支付方式
|
|
Container(
|
|
margin: EdgeInsets.only(top: l20),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
///微信
|
|
GestureDetector(
|
|
onTap: () {
|
|
setState(() {
|
|
payType = 0;
|
|
paymentMethod = "wx";
|
|
});
|
|
},
|
|
child: Container(
|
|
width: w157,
|
|
height: t36,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: Color(payType == 0 ? 0xFF2E3443 : 0xFF20242F),
|
|
borderRadius: BorderRadius.all(Radius.circular(11)),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Image(width: l20, height: t17, image: AssetImage('assets/images/ic_wx.png')),
|
|
Container(
|
|
margin: EdgeInsets.only(left: l11),
|
|
child: Text(
|
|
"微信支付",
|
|
style: TextStyle(fontSize: s13, color: Color(0xFFD6D6D7)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
///支付宝
|
|
GestureDetector(
|
|
onTap: () {
|
|
setState(() {
|
|
payType = 1;
|
|
paymentMethod = "zfb";
|
|
});
|
|
},
|
|
child: Container(
|
|
width: w157,
|
|
height: t36,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: Color(payType == 1 ? 0xFF2E3443 : 0xFF20242F),
|
|
borderRadius: BorderRadius.all(Radius.circular(11)),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Image(width: t17, height: t17, image: AssetImage('assets/images/ic_zfb.png')),
|
|
Container(
|
|
margin: EdgeInsets.only(left: l11),
|
|
child: Text(
|
|
"支付宝支付",
|
|
style: TextStyle(fontSize: s13, color: Color(0xFFD6D6D7)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
///支付按钮
|
|
GestureDetector(
|
|
onTap: () {
|
|
_viewModel.createOrder(goodList[goodIndex].productId, paymentMethod);
|
|
},
|
|
child: Container(
|
|
width: size.width,
|
|
height: h44,
|
|
margin: EdgeInsets.only(top: l20),
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: const Color(0xFF074CE7),
|
|
borderRadius: BorderRadius.all(Radius.circular(c22)),
|
|
),
|
|
child: Text(
|
|
"确认支付",
|
|
style: TextStyle(fontSize: s14, color: Colors.white),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_goodItem(context) {
|
|
final size = MediaQuery.of(context).size;
|
|
final w157 = size.width / 2.292993630573248;
|
|
final t78 = size.width / 4.615384615384615;
|
|
final s18 = size.width / 20;
|
|
final b25 = size.width / 14.4;
|
|
final h20 = size.width / 18;
|
|
final s16 = size.width / 22.5;
|
|
|
|
return goodList.map((value) {
|
|
int index = goodList.indexOf(value);
|
|
return GestureDetector(
|
|
onTap: () {
|
|
setState(() {
|
|
goodIndex = index;
|
|
});
|
|
},
|
|
child: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Visibility(
|
|
visible: goodIndex == index ? true : false,
|
|
child: CachedNetworkImage(
|
|
width: w157,
|
|
imageUrl: '${goodList[index].productSelectImage}',
|
|
errorWidget: (context, url, error) => const Icon(Icons.error),
|
|
),
|
|
),
|
|
Visibility(
|
|
visible: goodIndex != index ? true : false,
|
|
child: CachedNetworkImage(
|
|
width: w157,
|
|
imageUrl: '${goodList[index].productImage}',
|
|
errorWidget: (context, url, error) => const Icon(Icons.error),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList();
|
|
}
|
|
|
|
// 获取原生的值
|
|
invokeNativeMethod(String method, Map<String, dynamic> map) async {
|
|
dynamic args;
|
|
try {
|
|
args = await Global.method.invokeMethod(method, map);
|
|
} on PlatformException catch (e) {}
|
|
}
|
|
|
|
@override
|
|
// TODO: implement wantKeepAlive
|
|
bool get wantKeepAlive => true;
|
|
}
|