import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; import 'package:satu/core/models/but_item/buy_item_response.dart'; import 'package:satu/core/services/dialog_service.dart'; import 'package:satu/views/work/tabs/buy/component/product_buy_tile.dart'; import '../../../../core/entity/goods_entity.dart'; import '../../../../core/models/buy_invoice/buy_invoice_response.dart'; import '../../../../core/services/buy_service.dart'; import '../../../../core/services/dictionary_service.dart'; import '../../../../core/services/navigator_service.dart'; import '../../../../core/utils/locator.dart'; import '../../../../routes/route_names.dart'; import '../../../../shared/app_colors.dart'; import '../../../../shared/ui_helpers.dart'; import '../../../../widgets/bar/products_app_bar.dart'; class BuyEditView extends StatefulWidget { const BuyEditView({required this.invoice, Key? key}) : super(key: key); final BuyInvoiceResponse invoice; @override State createState() => _BuyEditViewState(); } class _BuyEditViewState extends State { final BuyService _service = locator(); final DialogService _dialogService = locator(); static const _pageSize = 20; final PagingController _pagingController = PagingController(firstPageKey: 1); bool editable = false; @override void initState() { log('initState'); _pagingController.addPageRequestListener((pageKey) { _fetchData(pageKey, _pageSize); }); log('refBuyInvoiceStatusId: ${widget.invoice.refBuyInvoiceStatusId}'); if(widget.invoice.refBuyInvoiceStatusId == 1){ editable = true; } super.initState(); } @override void dispose() { _pagingController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: const ProductsAppBar( title: 'Покупка', ), body: Column( children: [ Expanded( child: PagedListView.separated( physics: const BouncingScrollPhysics(), separatorBuilder: (BuildContext context, int index) { return const Divider( height: 1.0, color: disableColor, ); }, pagingController: _pagingController, builderDelegate: PagedChildBuilderDelegate( itemBuilder: (BuildContext context, BuyItemResponse item, int index) { return ProductBuyTile( key: ValueKey(item.id), ean: '1234567890123', name: 'Картофель', price: item.price, count: item.cnt, categoryName: 'Овощи', editable: editable, invoiceId: widget.invoice.id, id: item.id, editData: (id, price, count) { _editData(id, price, count); }, ); }, ), ), ) ], ), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButton: editable ? floatingActionButtonRender() : null, ); } /// render floating buttons Widget floatingActionButtonRender() { return Padding( padding: EdgeInsets.all(15), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.end, children: [ Visibility( visible: false, child: Padding( padding: const EdgeInsets.all(8.0), child: FloatingActionButton( mini: true, elevation: 2, backgroundColor: successColor, onPressed: () {}, child: Icon( Icons.check, color: whiteColor, size: 35, ), ), )), Column( mainAxisAlignment: MainAxisAlignment.end, children: [ FloatingActionButton( elevation: 2, mini: true, onPressed: () async { final Good? good = await locator() .push(addProductViewRoute) as Good?; if (good != null && good.id != null) { bool result = await _service.addItem(widget.invoice.id, good.id!); if (result) { _pagingController.refresh(); } else { _dialogService.showDialog( description: 'Товара отсутсвует в остатке или ранее не' ' использователся в системе', ); } } }, child: Icon( Icons.add_rounded, size: 40, color: whiteColor, ), ), verticalSpaceSmall, FloatingActionButton( elevation: 2, mini: true, onPressed: () async { final NavigatorService _nav = locator(); final dynamic result = await _nav.push(addByBarcodeViewRoute); if (result != null) { final List goods = await locator() .getGoodsByNameOrEan(result as String); if (goods.isNotEmpty) {} } }, child: Icon(Icons.qr_code_rounded, size: 30, color: whiteColor), ), ], ) ], ), ); } Future _fetchData(int pageKey, int perPage) async { final List newItems = await _service .getItemList(widget.invoice.id, page: pageKey, perpage: perPage); final isLastPage = newItems.length < _pageSize; if (isLastPage) { _pagingController.appendLastPage(newItems); } else { final nextPageKey = pageKey + 1; _pagingController.appendPage(newItems, nextPageKey); } } void _editData(int id, double price, double count) { final List oldList = _pagingController.value.itemList ?? []; oldList..firstWhere((element) => element.id == id).price = price ..firstWhere((element) => element.id == id).cnt = count; setState(() { _pagingController.itemList = oldList; }); } }