aman-satu-flutter/lib/views/work/tabs/buy/buy_view.dart

145 lines
5.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:intl/intl.dart';
import 'package:satu/core/services/buy_service.dart';
import 'package:satu/core/utils/utils_parse.dart';
import '../../../../core/models/buy_invoice/buy_invoice_response.dart';
import '../../../../core/services/navigator_service.dart';
import '../../../../core/utils/locator.dart';
import '../../../../routes/route_names.dart';
import '../../../../shared/app_colors.dart';
import '../../../../shared/shared_styles.dart';
import '../../../../shared/ui_helpers.dart';
import '../../../../widgets/bar/products_app_bar.dart';
import '../../../dictionaries/component/dictionary_list_tile.dart';
import '../../../inventarization/widget/inventarization_list_tile.dart';
class BuyView extends StatefulWidget {
@override
State<BuyView> createState() => _BuyViewState();
}
class _BuyViewState extends State<BuyView> {
final BuyService _service = locator<BuyService>();
final NavigatorService _navigatorService = locator<NavigatorService>();
static const _pageSize = 20;
final PagingController<int, BuyInvoiceResponse> _pagingController =
PagingController(firstPageKey: 1);
final DateFormat formatterDay = DateFormat('dd.MM.yyyy');
@override
void initState() {
_pagingController.addPageRequestListener((pageKey) {
_fetchData(pageKey, _pageSize);
});
super.initState();
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const ProductsAppBar(
title: 'Накладные покупок',
drawerShow: true,
),
body: Column(
children: [
Expanded(
child: PagedListView<int, BuyInvoiceResponse>.separated(
physics: const BouncingScrollPhysics(),
separatorBuilder: (BuildContext context, int index) {
return const Divider(
height: 1.0,
color: disableColor,
);
},
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<BuyInvoiceResponse>(
itemBuilder: (BuildContext context, BuyInvoiceResponse item,
int index) {
return DictionaryTile(
key: Key('category_${item.id}'),
onPress: () async {
final dynamic result = await _navigatorService
.push(buyEditRoute, arguments: item);
if (result != null && true == (result as bool)) {
_pagingController.refresh();
}
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
flex: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${item.eaccContragentId.toString()}',
style: textBlack12Style,
),
SizedBox(
height: 5.0,
),
Text(
'Статус: ${item.eaccContragentId.toString()}',
style: textGray11Style,
),
],
)),
Expanded(
flex: 2,
child: Text(
'${formatterDay.format(item.invoiceDate)}',
style: textBlack12Style,
),
),
Expanded(
flex: 1,
child: Column(
children: [
Text(
'${formatDecimal(item.summ ?? 0)} т.',
style: textGray11Style,
),
],
),
),
],
),
);
},
),
),
)
],
),
);
}
Future<void> _fetchData(int pageKey, int perPage) async {
final List<BuyInvoiceResponse> newItems = await _service
.getList(page: pageKey, perpage: perPage);
final isLastPage = newItems.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + 1;
_pagingController.appendPage(newItems, nextPageKey);
}
}
}