#
whycq
2025-03-03 6a90c5bde0facc8330ce4c7c7d89292717b7ac65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
 
import '../../api/api_service.dart';
import '../../common/custom_search_bar.dart';
import '../../common/empty_count.dart';
import '../../common/my_sliver_persistent_header_delegate.dart';
import 'mat_card.dart';
 
class MatDetlsPage extends StatefulWidget {
  final List<Map<String, dynamic>>? data;
 
  const MatDetlsPage({super.key, this.data});
 
  @override
  State<MatDetlsPage> createState() => _MatDetlsState();
}
 
class _MatDetlsState extends State<MatDetlsPage> {
  /// 输入框焦点
  FocusNode focusNode = FocusNode();
 
  // List<Map<String, dynamic>> items = [
  //
  // ];
  var items = [];
 
  @override
  void initState() {
    super.initState();
    items = widget.data!;
  }
 
  ///
  void _searchMat(value) async {
    var matAuth = await ApiService.matAuth(value);
    if (matAuth['data'] != null) {
      Get.toNamed('/mat_form_page', arguments: matAuth['data'])
          ?.then((result) => {
        if (result != null)
          {
            setState(() {
              addItem(result);
            }),
          }
      });
    } else {
      Get.snackbar(
        "检索失败",
        '未检索到该物料:$value',
        duration: Duration(seconds: 2),
        backgroundColor: Color.fromRGBO(216, 97, 97, .8),
        colorText: Colors.white,
      );
    }
  }
 
  void addItem(item) {
    var fieldsToMatch = ['matnr', 'batch'];
    for (var existingItem in items) {
      bool isMatch =
          fieldsToMatch.every((field) => existingItem[field] == item[field]);
 
      if (isMatch) {
        var val = existingItem['anfme'];
        print('Existing value: $val');
        var newVal = item['anfme'];
        print('New value: $newVal');
        if ((val is int && newVal is int) || (val is double && newVal is double)) {
          existingItem['anfme'] = val + newVal;
          print('Updated value: $val');
        } else {
          print('Error: Value is not a number');
        }
        return;
      }
    }
    items.add(item);
  }
 
  void _updateItem(int index, item) {
    var args = {'index': index, 'item': item};
    Get.toNamed('/mat_form_page', arguments: args)?.then((result) => {
          if (result != null)
            {
              setState(() {
                var index = result['index'];
                var newItem = result['item'];
                setState(() {
                  items[index] = newItem;
                });
              }),
            }
        });
  }
 
  void deleteItem(int index) {
    setState(() {
      // 删除指定索引的对象
      items.removeAt(index);
    });
  }
 
 
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('组托物料明细'),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            // 在这里处理返回逻辑
            Get.back(result: items);
          },
        ),
      ),
      body: WillPopScope(
        onWillPop: () async {
          // 当按下返回按钮时,返回数据给父页面
          Get.back(result: items);
          return true; // 允许返回
        },
        child: CustomScrollView(
          slivers: [
            SliverPersistentHeader(
              pinned: true, // 设置为true,表示吸顶效果
              delegate: MySliverPersistentHeaderDelegate(
                  minHeight: 60, // 最小高度
                  maxHeight: 60, // 最大高度
                  child: CustomSearchBar(
                    onChanged: _searchMat,
                    onTap: () {
                      Get.toNamed('/get_mat_page')?.then((result) => {
                            if (result != null)
                              {
                                _searchMat(result),
                              }
                          });
                    },
                  )),
            ),
            SliverList(
              delegate: items.isEmpty
                  ? SliverChildBuilderDelegate(
                      (context, index) {
                        print('Building item at index: $index'); // 添加调试信息
                        return EmptyCount();
                      },
                      childCount: 1, // 项目数量
                    )
                  : SliverChildBuilderDelegate(
                      (context, index) {
                        var item = items[index];
                        return Padding(
                          padding: EdgeInsets.only(
                              top: 0, right: 16, left: 16, bottom: 0),
                          child: MatCard(
                            item: item,
                            isEdit: true,
                            update: () => _updateItem(index, item),
                            delete: () => deleteItem(index),
                          ),
                        );
                      },
                      childCount: items.length, // 项目数量
                    ),
            ),
          ],
        ),
      ),
    );
  }
}