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>? data; const MatDetlsPage({super.key, this.data}); @override State createState() => _MatDetlsState(); } class _MatDetlsState extends State { /// 输入框焦点 FocusNode focusNode = FocusNode(); // List> 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, // 项目数量 ), ), ], ), ), ); } }