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 createState() => _MatFormPageState(); } class _MatFormPageState extends State { late Map args; late Map 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), ], ), ); } }