156 lines
4.6 KiB
Dart
156 lines
4.6 KiB
Dart
|
|
import 'dart:async';
|
|
|
|
import 'package:aiplot/tools/shop/shop_model.dart';
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
|
|
import '../../bean/create_order_bean.dart';
|
|
import '../../bean/product_bean.dart';
|
|
import '../../generated/l10n.dart';
|
|
|
|
class ShopPage extends StatefulWidget {
|
|
const ShopPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ShopPage> createState() => _ShopPageState();
|
|
}
|
|
|
|
class _ShopPageState extends State<ShopPage> {
|
|
StreamSubscription? subscription;
|
|
final ShopModel _viewModel = ShopModel();
|
|
List<ProductBean> productList = [];
|
|
CreateOrderBean? createOrderBean;
|
|
int currentIndex = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
|
|
//网络请求回调
|
|
subscription = _viewModel.streamController.stream.listen((newData) {
|
|
String code = newData['code'];
|
|
if (code.isNotEmpty) {
|
|
EasyLoading.dismiss();
|
|
switch (code) {
|
|
case "getProductList":
|
|
productList = newData['data'];
|
|
break;
|
|
case "createOrder":
|
|
createOrderBean = newData['data'];
|
|
completeOrder(createOrderBean!.OrderCode!, productList[currentIndex].ProductId!, "a");
|
|
break;
|
|
}
|
|
}
|
|
setState(() {});
|
|
});
|
|
getProductList();
|
|
}
|
|
|
|
//获取商品列表
|
|
getProductList() {
|
|
EasyLoading.show(status: 'loading...');
|
|
_viewModel.getProductList();
|
|
}
|
|
|
|
//生成订单
|
|
createOrder(String productId) {
|
|
EasyLoading.show(status: 'loading...', maskType: EasyLoadingMaskType.black);
|
|
_viewModel.createOrder(productId);
|
|
}
|
|
|
|
//完成订单
|
|
completeOrder(String orderCode, String productId, String token) {
|
|
EasyLoading.show(status: 'loading...');
|
|
_viewModel.completeOrder(orderCode, productId, token);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
subscription?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final size = MediaQuery.of(context).size;
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.white,
|
|
elevation: 0,
|
|
title: Text(
|
|
S.of(context).Mall,
|
|
style: const TextStyle(fontSize: 20, color: Colors.black),
|
|
),
|
|
centerTitle: true,
|
|
leading: IconButton(
|
|
icon: const ImageIcon(AssetImage('assets/images/ic_return.png')),
|
|
color: Colors.black,
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
}),
|
|
),
|
|
body: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Column(
|
|
children: [
|
|
Image(width: size.width, fit: BoxFit.fitWidth, image: const AssetImage('assets/images/shop_bg.png')),
|
|
Container(
|
|
margin: const EdgeInsets.only(top: 28, left: 15),
|
|
child: Row(
|
|
children: [
|
|
const Image(width: 14, height: 14, image: AssetImage('assets/images/shop_star.png')),
|
|
Container(
|
|
margin: const EdgeInsets.only(left: 9),
|
|
child: Text(S.of(context).Paint_points),
|
|
)
|
|
],
|
|
)),
|
|
Container(
|
|
margin: const EdgeInsets.only(top: 10, left: 15),
|
|
child: Row(
|
|
children: [
|
|
const Image(width: 14, height: 14, image: AssetImage('assets/images/shop_star.png')),
|
|
Container(
|
|
margin: const EdgeInsets.only(left: 9),
|
|
child: Text(S.of(context).Painting_points_never),
|
|
)
|
|
],
|
|
)),
|
|
//商品列表
|
|
Expanded(
|
|
child: SizedBox(
|
|
width: size.width,
|
|
child: ListView.builder(
|
|
itemCount: productList.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return _item(productList[index], index);
|
|
},
|
|
scrollDirection: Axis.vertical),
|
|
))
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_item(ProductBean bean, int index) {
|
|
return GestureDetector(
|
|
onTap: () {
|
|
currentIndex = index;
|
|
createOrder(bean.ProductId!);
|
|
},
|
|
child: Container(
|
|
margin: const EdgeInsets.only(left: 10, top: 10, right: 10),
|
|
child: CachedNetworkImage(imageUrl: bean.ImageUrl!),
|
|
),
|
|
);
|
|
}
|
|
}
|