张延森 před 2 roky
rodič
revize
e515573bd9

+ 40
- 41
lib/pages/addressList/index.dart Zobrazit soubor

57
           child: const Text('确定要删除此地址吗?'),
57
           child: const Text('确定要删除此地址吗?'),
58
         ),
58
         ),
59
         onCancel: () => true,
59
         onCancel: () => true,
60
-        onConfirm: () => {
60
+        onConfirm: () {
61
               addressDelete(id).then((value) {
61
               addressDelete(id).then((value) {
62
-                List<Address> newslist = [];
63
-                for (var item in addressController.addressList()) {
64
-                  if (item.addressId != id) {
65
-                    newslist.add(item);
66
-                  }
67
-                }
68
-                addressController.addressList(newslist);
69
-
62
+                List<Address> list = addressController.addressList.value.toList();
63
+                list.removeWhere((element) => element.addressId == id);
64
+                addressController.addressList(list);
70
                 Fluttertoast.showToast(msg: '删除成功!');
65
                 Fluttertoast.showToast(msg: '删除成功!');
71
-              }),
66
+              });
67
+            });
68
+  }
69
+
70
+  Widget buildList() {
71
+    return Obx(() {
72
+      var index = 0;
73
+      var list = addressController.addressList.value;
74
+      List<Widget> children = list.map((item) {
75
+        return AddressCard(
76
+          No: index ++,
77
+          item: item,
78
+          isBack: isBack,
79
+          onChange: () {
80
+            onChange(item.addressId);
81
+          },
82
+          onEdit: () {
83
+            Get.offNamed('/addAddress', arguments: {
84
+              'isBack': isBack,
85
+              'id': item.addressId,
86
+              'item': item.address.toString()
72
             });
87
             });
88
+          },
89
+          onDelete: () {
90
+            onDelete(item.addressId);
91
+          },
92
+          onBack: () {
93
+            //选择地址
94
+            addressController.address.value = item;
95
+            Get.back();
96
+          },
97
+        );
98
+      }).toList();
99
+
100
+      return Column(children: children,);
101
+    });
73
   }
102
   }
74
 
103
 
75
   @override
104
   @override
83
               //左对齐
112
               //左对齐
84
               crossAxisAlignment: CrossAxisAlignment.start,
113
               crossAxisAlignment: CrossAxisAlignment.start,
85
               children: [
114
               children: [
86
-                Obx(
87
-                  () => Column(children: [
88
-                    ...addressController.addressList().map((item) {
89
-                      int e = addressController.addressList().indexOf(item);
90
-
91
-                      return AddressCard(
92
-                        No: e + 1,
93
-                        item: item,
94
-                        isBack: isBack,
95
-                        onChange: () {
96
-                          onChange(item.addressId);
97
-                        },
98
-                        onEdit: () {
99
-                          Get.offNamed('/addAddress', arguments: {
100
-                            'isBack': isBack,
101
-                            'id': item.addressId,
102
-                            'item': item.address.toString()
103
-                          });
104
-                        },
105
-                        onDelete: () {
106
-                          onDelete(item.addressId);
107
-                        },
108
-                        onBack: () {
109
-                          //选择地址
110
-                          addressController.address.value = item;
111
-                          Get.back();
112
-                        },
113
-                      );
114
-                    })
115
-                  ]),
116
-                ),
115
+                buildList(),
117
                 DefaultButton(
116
                 DefaultButton(
118
                   color: const Color(0xffffffff),
117
                   color: const Color(0xffffffff),
119
                   backColor: const Color(0xFFFF703B),
118
                   backColor: const Color(0xFFFF703B),

+ 5
- 20
lib/pages/addressList/widget/AddressCard.dart Zobrazit soubor

2
 import 'package:flutter/material.dart';
2
 import 'package:flutter/material.dart';
3
 import 'package:flutter_screenutil/flutter_screenutil.dart';
3
 import 'package:flutter_screenutil/flutter_screenutil.dart';
4
 
4
 
5
-class AddressCard extends StatefulWidget {
5
+class AddressCard extends StatelessWidget {
6
   final Address item;
6
   final Address item;
7
   final int No;
7
   final int No;
8
   final bool isBack;
8
   final bool isBack;
21
       required this.onBack})
21
       required this.onBack})
22
       : super(key: key);
22
       : super(key: key);
23
 
23
 
24
-  @override
25
-  _AddressCard createState() =>
26
-      _AddressCard(item, No, onChange, onDelete, onEdit, isBack, onBack);
27
-}
28
-
29
-class _AddressCard extends State<AddressCard> {
30
-  final Address item;
31
-  final int No;
32
-  final bool isBack;
33
-  final GestureTapCallback onChange;
34
-  final GestureTapCallback onDelete;
35
-  final GestureTapCallback onEdit;
36
-  final GestureTapCallback onBack;
37
-  _AddressCard(this.item, this.No, this.onChange, this.onDelete, this.onEdit,
38
-      this.isBack, this.onBack);
39
-
40
   @override
24
   @override
41
   Widget build(BuildContext context) {
25
   Widget build(BuildContext context) {
42
     return Container(
26
     return Container(
64
                   Container(
48
                   Container(
65
                     margin: EdgeInsets.fromLTRB(0, 0, 5.w, 0),
49
                     margin: EdgeInsets.fromLTRB(0, 0, 5.w, 0),
66
                     padding:
50
                     padding:
67
-                        EdgeInsets.symmetric(vertical: 1.w, horizontal: 5.w),
51
+                    EdgeInsets.symmetric(vertical: 1.w, horizontal: 5.w),
68
                     decoration: BoxDecoration(
52
                     decoration: BoxDecoration(
69
                       color: const Color(0xffff0000),
53
                       color: const Color(0xffff0000),
70
                       borderRadius: BorderRadius.all(Radius.circular(5.w)),
54
                       borderRadius: BorderRadius.all(Radius.circular(5.w)),
81
                 Text(
65
                 Text(
82
                   '我的地址' + No.toString(),
66
                   '我的地址' + No.toString(),
83
                   style:
67
                   style:
84
-                      TextStyle(fontSize: 15.sp, fontWeight: FontWeight.w700),
68
+                  TextStyle(fontSize: 15.sp, fontWeight: FontWeight.w700),
85
                 ),
69
                 ),
86
               ],
70
               ],
87
             ),
71
             ),
137
                 child: Text(
121
                 child: Text(
138
                   '选择此地址',
122
                   '选择此地址',
139
                   style:
123
                   style:
140
-                      TextStyle(fontSize: 17.sp, fontWeight: FontWeight.w800),
124
+                  TextStyle(fontSize: 17.sp, fontWeight: FontWeight.w800),
141
                 ),
125
                 ),
142
               ),
126
               ),
143
             ),
127
             ),
179
       ),
163
       ),
180
     );
164
     );
181
   }
165
   }
166
+
182
 }
167
 }