#
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
 
import '../../common/bottom_bar.dart';
import '../../common/number_stepper.dart';
import '../../common/page_config.dart';
import '../../widgets/buttons/custom_elevated_button.dart';
 
class MatFormPage extends StatefulWidget {
  const MatFormPage({super.key});
  @override
  State<MatFormPage> createState() => _MatFormPageState();
}
 
class _MatFormPageState extends State<MatFormPage> {
  late Map<String, dynamic> args;
  late Map<String, dynamic> mat;
  bool isPick = false;
  int index = 0;
  String backTitle = '提取';
 
  @override
  void initState() {
    super.initState();
    args = Get.arguments;
    if (args['index'] != null && args['item'] != null) {
      backTitle = '修改';
      isPick = true;
      index = args['index'];
      mat = args['item'];
    } else {
      isPick = false;
      backTitle = '提取';
      mat = args;
    }
    // print('mat_form_page mat: ' + mat.toString());
    // 接收传递过来的数据
    pageConfig.forEach((item) {
      mat.forEach((key, value) {
        if (key == item['propName']) {
          item['value'] = value;
        }
        if (!isPick) {
          if (item['isEditable']) {
            item['value'] = item['defaultValue'] == null ? '--' : item['defaultValue'];
          }
        }
      });
    });
  }
  Widget ListItem({
    required String title,
    required String propName,
    required value,
    required String type,
    required bool isShow,
  }) {
    if (!isShow) {
      return SizedBox.shrink();
    } else {
      return Container(
        color: Colors.white,
        child: Container(
          padding: const EdgeInsets.only(top: 10, bottom: 10,right: 10),
          margin: const EdgeInsets.only(left: 10),
          constraints: BoxConstraints(minHeight: 50),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(bottom: BorderSide(color: Colors.grey[300]!)),
          ),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('$title:',style: const TextStyle(fontSize: 16,fontWeight: FontWeight.bold)),
                SizedBox(width: 10),
                CustomText(text: value, type: type, propName: propName,title: title),
              ]
          ),
        ),
      );
    }
 
  }
  Widget CustomText({
    required text,
    required String type,
    required String propName,
    required String title,
  }) {
    switch (type) {
      case 'textFiled':
        return Expanded(
          child: Container(
            constraints: BoxConstraints(maxHeight: double.infinity),
              child: InkWell(
                onTap: () {
                  Get.toNamed('/custom_editor_page',
                      arguments: {'title': title, 'text': text, 'propName': propName}
                  )?.then((result) {
                    print('value: $result');
                    setState(() {
                      pageConfig.forEach((item) {
                        if (item['propName'] == result['propName']) {
                          item['value'] = result['text'];
                        }
                      });
                    });
                  });
                },
                child: Row(
                  children: [
                    Expanded(child: Text(text ??  '--'),),
                    Icon(Icons.arrow_forward_ios_rounded, size: 16, color: Colors.grey),
                  ],
                ),
              )
          ),
        );
      case 'number':
        return Expanded(child: Center(child: NumberStepper(defaultValue: text,onValueChanged: (value) {
          pageConfig.forEach((item) {
            if (item['propName'] == propName) {
              setState(() {
                item['value'] = value;
              });
            }
          });
        },)));
      default:
        return Expanded(child: Text((text != null && text.toString().isNotEmpty) ? text.toString() : '--',softWrap: true, overflow: TextOverflow.ellipsis, maxLines: 3));
    }
  }
 
 
  @override
  Widget build(BuildContext context) {
    // print(pageConfig);
    void _pickMat() {
      pageConfig.forEach((item) {
        final String propName = item['propName'];
        final value = args[propName]; // 获取 args 中对应的 value
        if (value != null) {
          print('key: $propName, value: $value');
          item['value'] = value; // 更新 pageConfig 中的 value
        } else {
          // 如果 args 中没有对应的 propName,则将其添加到 args 中
          args[propName] = item['value']; // 使用 item['value'] 作为初始值
          // print('Added to args: key: $propName, initial value: ${item['value']}');
        }
      });
 
      Get.back(result: args);
    }
    void _updateMat() {
      // print('update mat' + mat.toString());
      pageConfig.forEach((item) {
        final String propName = item['propName'];
        final newValue = item['value'];
        mat[propName] = newValue;
      });
      Get.back(result: {'index': index, 'item': mat});
    }
    return Scaffold(
      appBar: AppBar(
        title: Text('${isPick ? '修改' : '提取'}物料'),
      ),
      body: ListView(
        children: List.generate(pageConfig.length, (index) {
          var title = pageConfig[index]['title'];
          var value = pageConfig[index]['value'];
          var type = pageConfig[index]['type'];
          var propName = pageConfig[index]['propName'];
          var isShow = pageConfig[index]['isShow'];
          if (value == null) {
            value = '';
          }
          return ListItem(
            title: title,
            value: value,
            type: type,
            propName: propName,
            isShow: isShow,
          );
        }),
      ),
      bottomNavigationBar: BottomBar(
        children: [
          CEBtn(title: backTitle, type: Type.second, onPressed: isPick ? _updateMat : _pickMat),
        ],
      ),
    );
  }
}