panhui 6 tahun lalu
induk
melakukan
a177a93e9f
3 mengubah file dengan 123 tambahan dan 51 penghapusan
  1. 22 0
      lib/model/ProductInfo.dart
  2. 26 0
      lib/model/ProductInfo.g.dart
  3. 75 51
      lib/pages/ShoppingMall.dart

+ 22 - 0
lib/model/ProductInfo.dart

@@ -0,0 +1,22 @@
+import 'package:json_annotation/json_annotation.dart';
+
+part 'ProductInfo.g.dart';
+
+@JsonSerializable()
+class ProductInfo {
+  ProductInfo(this.id, this.productName, this.money, this.typeFlag);
+  int id;
+  String productName;//名称
+  int money;//钱
+  int typeFlag;//类型
+  factory ProductInfo.fromJson(Map<String, dynamic> json) =>
+      _$ProductInfoFromJson(json);
+
+  Map<String, dynamic> toJson() => _$ProductInfoToJson(this);
+  // 命名构造函数
+  ProductInfo.empty();
+  @override
+  String toString() {
+    return _$ProductInfoToJson(this).toString();
+  }
+}

+ 26 - 0
lib/model/ProductInfo.g.dart

@@ -0,0 +1,26 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of 'ProductInfo.dart';
+
+// **************************************************************************
+// JsonSerializableGenerator
+// **************************************************************************
+
+ProductInfo _$ProductInfoFromJson(Map<String, dynamic> json) {
+  return ProductInfo(
+      json['id'] as int,
+      json['productName'] as String,
+      json['money'] as int,
+      json['typeFlag'] as int);
+}
+
+//  int id;
+//  String productName;//名称
+//   int money;//钱
+//   int typeFlag;//类型
+Map<String, dynamic> _$ProductInfoToJson(ProductInfo instance) => <String, dynamic>{
+      'id': instance.id,
+      'productName': instance.productName,
+      'money': instance.money,
+      'typeFlag': instance.typeFlag
+    };

+ 75 - 51
lib/pages/ShoppingMall.dart

@@ -1,5 +1,11 @@
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import '../styles/colors.dart';
 import '../styles/colors.dart';
+import '../net/HttpManager.dart';
+import '../net/Result.dart';
+import '../styles/totast.dart';
+import 'package:flutter_redux/flutter_redux.dart';
+import '../redux/AppState.dart';
+import '../model/ProductInfo.dart';
 
 
 class ShoppingMall extends StatefulWidget {
 class ShoppingMall extends StatefulWidget {
   @override
   @override
@@ -20,18 +26,19 @@ class ShoppingMallState extends State<ShoppingMall> {
             padding: EdgeInsets.only(right: 56),
             padding: EdgeInsets.only(right: 56),
             child: TabBar(
             child: TabBar(
               tabs: myTabs,
               tabs: myTabs,
-              indicatorColor: Colors.white,   
+              indicatorColor: Colors.white,
               labelColor: Colors.white,
               labelColor: Colors.white,
               indicatorSize: TabBarIndicatorSize.label,
               indicatorSize: TabBarIndicatorSize.label,
             ),
             ),
           ),
           ),
         ),
         ),
         body: TabBarView(
         body: TabBarView(
-          children: <Widget>[Recharge(
-            type:0
-          ), Recharge(
-            type: 1,
-          )],
+          children: <Widget>[
+            Recharge(type: 0),
+            Recharge(
+              type: 1,
+            )
+          ],
         ),
         ),
       ),
       ),
     );
     );
@@ -39,58 +46,65 @@ class ShoppingMallState extends State<ShoppingMall> {
 }
 }
 
 
 class Recharge extends StatefulWidget {
 class Recharge extends StatefulWidget {
-    Recharge({Key key, this.type}) : super(key: key);
-final int type;//类型 0 金币 1 会员
+  Recharge({Key key, this.type}) : super(key: key);
+  final int type; //类型 0 金币 1 会员
   @override
   @override
   RechargeState createState() => RechargeState();
   RechargeState createState() => RechargeState();
 }
 }
 
 
 class RechargeState extends State<Recharge> {
 class RechargeState extends State<Recharge> {
   List moneyList = [100, 300, 500, 1000, 2000, 5000];
   List moneyList = [100, 300, 500, 1000, 2000, 5000];
-   List vipList = [{
-     "name":'VIP1',
-     "value":100
-   },{
-     "name":'VIP2',
-     "value":300
-   },{
-     "name":'VIP3',
-     "value":500
-   },{
-     "name":'VIP4',
-     "value":1000
-   },{
-     "name":'VIP5',
-     "value":2000
-   },{
-     "name":'VIP6',
-     "value":5000
-   }];
-  int chooseMoney = 100;
+  List vipList = [
+    {"name": 'VIP1', "value": 100},
+    {"name": 'VIP2', "value": 300},
+    {"name": 'VIP3', "value": 500},
+    {"name": 'VIP4', "value": 1000},
+    {"name": 'VIP5', "value": 2000},
+    {"name": 'VIP6', "value": 5000}
+  ];
+  List<ProductInfo> productInfoList = [];
+  int chooseMoney = 0;
+  ProductInfo chooseProduct;
+
+  getInfoList() async {
+    Toast.show(context, '加载中', -1, 'loading');
+    final Result res = await HttpManager.get('productInfo/all',
+        data: {"typeFlag": widget.type});
+    Toast.hide();
+    if (res.success) {
+      for (var item in res.data) {
+        ProductInfo product;
+        product = ProductInfo.fromJson(item);
+        setState(() {
+          productInfoList.add(product);
+        });
+      }
+      if (productInfoList.length > 0) {
+        setState(() {
+          chooseProduct = productInfoList[0];
+        });
+      }
+    }
+  }
+
   @override
   @override
   void initState() {
   void initState() {
     // TODO: implement initState
     // TODO: implement initState
     super.initState();
     super.initState();
-    print(widget.type);
+    getInfoList();
   }
   }
+
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     return Scaffold(
     return Scaffold(
       body: Container(
       body: Container(
         color: BG_COLOR,
         color: BG_COLOR,
         child: ListView.builder(
         child: ListView.builder(
-            itemCount: widget.type==0?moneyList.length:vipList.length,
+            itemCount: productInfoList.length,
             itemBuilder: (BuildContext context, int index) {
             itemBuilder: (BuildContext context, int index) {
-              bool isChoose=false;
-              if(widget.type==0){
-                if(moneyList[index]==chooseMoney){
-                  isChoose=true;
-                }
-              }
-              else{
-                if(vipList[index]['value']==chooseMoney){
-                  isChoose=true;
-                }
+              bool isChoose = false;
+              if (productInfoList[index].id == chooseProduct.id) {
+                isChoose = true;
               }
               }
               return Container(
               return Container(
                 padding: EdgeInsets.symmetric(horizontal: 15),
                 padding: EdgeInsets.symmetric(horizontal: 15),
@@ -100,9 +114,7 @@ class RechargeState extends State<Recharge> {
                   color: Color(0xFF222335),
                   color: Color(0xFF222335),
                   padding: EdgeInsets.all(0),
                   padding: EdgeInsets.all(0),
                   shape: Border.all(
                   shape: Border.all(
-                      color: isChoose
-                          ? Color(0xFFC2524D)
-                          : Colors.transparent,
+                      color: isChoose ? Color(0xFFC2524D) : Colors.transparent,
                       width: 1),
                       width: 1),
                   disabledColor: Color(0xFF222335),
                   disabledColor: Color(0xFF222335),
                   child: Container(
                   child: Container(
@@ -112,7 +124,8 @@ class RechargeState extends State<Recharge> {
                           child: Row(
                           child: Row(
                             mainAxisAlignment: MainAxisAlignment.center,
                             mainAxisAlignment: MainAxisAlignment.center,
                             children: <Widget>[
                             children: <Widget>[
-                              Text(widget.type==0?(moneyList[index].toString() + "金币"):vipList[index]['name'],
+                              Text(
+                                  productInfoList[index].productName,
                                   style: TextStyle(
                                   style: TextStyle(
                                       color: Color(0xFFFFFFFF),
                                       color: Color(0xFFFFFFFF),
                                       fontWeight: FontWeight.w500,
                                       fontWeight: FontWeight.w500,
@@ -121,7 +134,7 @@ class RechargeState extends State<Recharge> {
                                 width: 10,
                                 width: 10,
                               ),
                               ),
                               Text(
                               Text(
-                                widget.type==0?("¥"+moneyList[index].toString()):("¥"+vipList[index]['value'].toString()),
+                               "¥" + productInfoList[index].money.toString(),
                                 style: TextStyle(
                                 style: TextStyle(
                                     color: Color(0xFF727785),
                                     color: Color(0xFF727785),
                                     fontWeight: FontWeight.w400,
                                     fontWeight: FontWeight.w400,
@@ -144,9 +157,8 @@ class RechargeState extends State<Recharge> {
                   onPressed: isChoose
                   onPressed: isChoose
                       ? null
                       ? null
                       : () {
                       : () {
-
                           setState(() {
                           setState(() {
-                            chooseMoney =  widget.type==0?moneyList[index]:vipList[index]['value'];
+                            chooseProduct =productInfoList[index];
                           });
                           });
                         },
                         },
                 ),
                 ),
@@ -155,18 +167,30 @@ class RechargeState extends State<Recharge> {
       ),
       ),
       floatingActionButton: Container(
       floatingActionButton: Container(
         width: double.infinity,
         width: double.infinity,
-        padding: EdgeInsets.symmetric(horizontal:15),
+        padding: EdgeInsets.symmetric(horizontal: 15),
         height: 48,
         height: 48,
         child: FlatButton(
         child: FlatButton(
           color: PRIMARY_COLOR,
           color: PRIMARY_COLOR,
           textColor: Colors.white,
           textColor: Colors.white,
           child: Text('立即充值'),
           child: Text('立即充值'),
-          onPressed: (){
-              
+          onPressed: () async {
+            Toast.show(context, '加载中', -1, 'loading');
+            final Result res = await HttpManager.post('productInfo/buy', data: {
+              "userId": StoreProvider.of<AppState>(context).state.userInfo.id,
+              "typeFlag": widget.type,
+              "money": chooseProduct.money,
+              'id':chooseProduct.id
+            });
+            Toast.hide();
+            if (res.success) {
+              Toast.show(context, '购买成功', 1500, 'success');
+            } else {
+              Toast.show(context, res.error, 1500, 'info');
+            }
           },
           },
         ),
         ),
       ),
       ),
-      floatingActionButtonLocation:FloatingActionButtonLocation.centerFloat,
+      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
     );
     );
   }
   }
 }
 }