#
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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
 
import '../../../common/bottom_bar.dart';
import '../../../common/detl_edit_page.dart';
import '../../../widgets/buttons/custom_elevated_button.dart';
import '../common/order_edit_card.dart';
import '../common/order_page_config.dart';
 
class MatDetlsEditPage extends StatefulWidget {
  final List<Map<String, dynamic>>? data;
 
  const MatDetlsEditPage({super.key, this.data});
 
  @override
  State<MatDetlsEditPage> createState() => _MatDetlsEditPageState();
}
 
class _MatDetlsEditPageState extends State<MatDetlsEditPage> {
  var items = []; // 订单列表数据
  /// 重置按钮
  ///
 
  @override
  void initState() {
    super.initState();
    items = widget.data!;
  }
 
  void _reset() {
    setState(() {});
  }
 
  void _addItem() {
    Get.toNamed('/order_list_page')?.then((result) => {
          setState(() {
            result.forEach((item) {
              addItem(item);
            });
          })
        });
  }
 
  void addItem(item) {
    var fieldsToMatch = ['matnr', 'orderNo'];
    for (var existingItem in items) {
      bool isMatch =
          fieldsToMatch.every((field) => existingItem[field] == item[field]);
 
      if (isMatch) {
        var val = existingItem['count'];
        var newVal = item['count'];
        if ((val is int && newVal is int) ||
            (val is double && newVal is double)) {
          existingItem['count'] = val + newVal;
        }
        return;
      }
    }
    items.add(item);
  }
 
  void _editItem(item, index) async {
    var result = await Get.to(() => DetlEditPage(
        data: item,
        config: orderPageConfig,
        maxCount: item['anfme'] - item['qty']));
    print(result);
    if (result != null) {
      setState(() {
        items[index] = result;
      });
    }
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('物料明细编辑')),
      body: Center(
        child: ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            // 定义边距
            double topPadding = index == 0 ? 8 : 4; // 第一个项目没有上边距
            double bottomPadding = index == 19 ? 8 : 4; // 最后一个项目没有下边距
            var item = items[index];
            return Padding(
              padding: EdgeInsets.only(top: topPadding, bottom: bottomPadding),
              child: OrderEditCard(
                orderNo: item['orderNo'],
                matnr: item['matnr'],
                maktx: item['maktx'],
                count: item['count'],
                edit: () => _editItem(item, index),
              ),
            );
          },
        ),
      ),
      bottomNavigationBar: BottomBar(
        children: [
          CEBtn(
              title: '重置',
              type: Type.cancel,
              btnSize: BtnSize.large,
              onPressed: _reset),
          CEBtn(
              title: '添加',
              type: Type.primary,
              btnSize: BtnSize.large,
              onPressed: _addItem),
          CEBtn(
              title: '完成',
              type: Type.second,
              btnSize: BtnSize.large,
              onPressed: () {
                Get.back(result: items);
              }),
        ],
      ),
    );
  }
}