166 lines
5.3 KiB
Dart
166 lines
5.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/svg.dart';
|
|
import 'package:satu/core/entity/category_entity.dart';
|
|
import 'package:satu/core/entity/goods_entity.dart';
|
|
import 'package:satu/core/models/dictionary/good_row_data.dart';
|
|
import 'package:satu/core/redux/actions/sell_actions.dart';
|
|
import 'package:satu/core/services/dictionary_service.dart';
|
|
import 'package:satu/core/services/navigator_service.dart';
|
|
import 'package:satu/core/utils/locator.dart';
|
|
import 'package:satu/routes/route_names.dart';
|
|
import 'package:satu/shared/app_colors.dart';
|
|
import 'package:satu/views/dictionaries/component/dictionary_list_tile.dart';
|
|
import 'package:satu/widgets/bar/products_app_bar.dart';
|
|
import 'package:satu/widgets/bar/products_title_bar.dart';
|
|
import 'package:satu/widgets/fields/input_field.dart';
|
|
import 'package:satu/widgets/ui/product_title_widget.dart';
|
|
|
|
class GoodsDictionaryView extends StatefulWidget {
|
|
@override
|
|
_GoodsDictionaryViewState createState() => _GoodsDictionaryViewState();
|
|
}
|
|
|
|
class _GoodsDictionaryViewState extends State<GoodsDictionaryView> {
|
|
final DictionaryService _dictionaryService = locator<DictionaryService>();
|
|
final NavigatorService _navigatorService = locator<NavigatorService>();
|
|
late TextEditingController _searchTextController;
|
|
final FocusNode _searchFocusNode = FocusNode();
|
|
|
|
late List<Good> _goods = [];
|
|
late List<Category> _categories = [];
|
|
late List<GoodRowDao> items = [];
|
|
|
|
@override
|
|
void initState() {
|
|
_searchTextController = TextEditingController();
|
|
_searchTextController.addListener(() {
|
|
if (_searchTextController.text.isNotEmpty) {
|
|
searchByField(_searchTextController.text);
|
|
} else {
|
|
reset();
|
|
}
|
|
});
|
|
initQuery();
|
|
super.initState();
|
|
}
|
|
|
|
Future<void> initQuery() async {
|
|
_goods = await _dictionaryService.getGoodsByNameOrEan('');
|
|
_categories = await _dictionaryService.getCategoriesAll();
|
|
searchByField('');
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchTextController.dispose();
|
|
_searchFocusNode.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: ProductsAppBar(
|
|
title: 'Товары',
|
|
drawerShow: true,
|
|
actions: [
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(90),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: () {},
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: SvgPicture.asset(
|
|
'assets/images/svg/options.svg',
|
|
height: 20,
|
|
width: 20,
|
|
color: textColor,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
InputField(
|
|
placeholder: 'Поиск по наименованию товара или штрих-код',
|
|
search: true,
|
|
controller: _searchTextController,
|
|
fieldFocusNode: _searchFocusNode,
|
|
),
|
|
const ProductsTitleBarBar(title: 'Список товаров'),
|
|
Expanded(
|
|
child: ListView.separated(
|
|
physics: const BouncingScrollPhysics(),
|
|
itemCount: items.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
final GoodRowDao good = items[index];
|
|
return DictionaryTile(
|
|
onPress: () {
|
|
locator<NavigatorService>()
|
|
.push(goodsEditRoute, arguments: good);
|
|
},
|
|
child: ProductTitleWidget(
|
|
name: good.name,
|
|
categoryName: good.categoryName,
|
|
ean: good.ean,
|
|
),
|
|
);
|
|
},
|
|
separatorBuilder: (BuildContext context, int index) {
|
|
return const Divider(
|
|
height: 1.0,
|
|
color: disableColor,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
elevation: 2,
|
|
onPressed: () => locator<NavigatorService>()
|
|
.push(goodsEditRoute, arguments: GoodRowDao('', '')),
|
|
child: const Icon(
|
|
Icons.add_rounded,
|
|
size: 34.0,
|
|
color: whiteColor,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void reset() {
|
|
_searchTextController.clear();
|
|
searchByField('');
|
|
}
|
|
|
|
Future<void> searchByField(String query) async {
|
|
log.i(query);
|
|
final List<GoodRowDao> list = [];
|
|
final Iterable<Good> filtered = query == ''
|
|
? _goods
|
|
: _goods.where((element) =>
|
|
element.name.toLowerCase().contains(query.toLowerCase()) ||
|
|
(element.ean != null &&
|
|
element.ean!.contains(query.toLowerCase())));
|
|
filtered.forEach((element) {
|
|
final Category category = _categories
|
|
.firstWhere((parent) => parent.id == element.categoryId, orElse: () {
|
|
return Category();
|
|
});
|
|
final String parentName = category.name;
|
|
final GoodRowDao rowDao = GoodRowDao(element.name, parentName,
|
|
ean: element.ean, id: element.id, categoryId: element.categoryId);
|
|
list.add(rowDao);
|
|
});
|
|
setState(() {
|
|
items = list;
|
|
});
|
|
}
|
|
}
|