flutter-freaccess-hub/lib/features/documents/document_page_model.dart

157 lines
4.6 KiB
Dart

part of 'index.dart';
class DocumentPageModel extends FlutterFlowModel<DocumentPage> {
@override
void dispose() {}
@override
void initState(BuildContext context) {}
final SearchKey searchKey = SearchKey();
final DocumentKey docKey = DocumentKey();
final PagingController<int, Document> _pagingController =
PagingController<int, Document>(firstPageKey: 1);
int count = 0;
final dynamic page = 1;
Query query = Document.fromDesc('');
late Document currentDocument;
bool isCategorySelected = false;
late Category currentCategory;
List<Document?> documents = [];
List<Category?> categories = [];
/// [listBodyBuilder]
Widget listBodyBuilder<T>(BuildContext context, Document item, int index) {
return DocumentItem(document: item);
}
/// [listHeaderBuilder]
Widget listHeaderBuilder(BuildContext context) => Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(15, 0, 50, 0),
child: Text(
'Últimos Documentos',
style: TextStyle(
color: FlutterFlowTheme.of(context).primaryText,
fontSize: LimitedFontSizeUtil.getHeaderFontSize(context),
),
),
),
CategoryCarousel<Category>(
categories: categories,
filter: filterByCategory,
),
],
);
/// [generateDocuments]
Future<(bool, List<Document?>)> generateDocuments(
pageKey, Query query) async {
final List<Document?> error = [null];
print('Query: ${query is Document}');
final GetDocuments getDocuments = FreAccessWSGlobal.getDocuments;
final ApiCallResponse newItems = await getDocuments.call(pageKey, query);
if (newItems.jsonBody == null) return (false, error);
if (newItems.jsonBody['error'] == true) return (false, error);
final List<dynamic> list = newItems.jsonBody['value']['list'];
late final List<Document> docs = [];
for (var item in list) {
log('-> generateDocuments: $item');
final String description = item['description'];
final String type = item['type'];
final String category = item['category']['description'];
final String color = item['category']['color'];
final String person = item['person'] ?? '';
final String property = item['property'] ?? '';
final String createdAt = item['createdAt'];
final String updatedAt = item['updatedAt'];
final int categoryId = item['category']['id'];
final int documentId = item['id'];
final doc = Document(
id: documentId,
description: description,
type: type,
category: Category(
id: categoryId,
color: color.toColor(),
title: category,
),
person: person,
property: property,
createdAt: createdAt,
updatedAt: updatedAt,
);
docs.add(doc);
}
return (true, docs);
// listViewKey.currentState!.count = newItems.jsonBody['value']['count'] ?? 0;
}
/// [generateCategories]
Future<List<Category?>> generateCategories(List<Document?> documents) async {
final List<Category?> error = [null];
if (documents == []) return error;
final GetCategories getCategories = FreAccessWSGlobal.getCategories;
final ApiCallResponse newItems = await getCategories.call();
if (newItems.jsonBody['error'] == true) return error;
if (newItems.jsonBody == null) return error;
final list = newItems.jsonBody['value'] as List<dynamic>;
late final List<Category> cats = [];
for (var item in list) {
final String color = item['color'];
final String title = item['description'];
final int id = item['id'];
final cat = Category(
id: id,
color: color.toColor(),
title: title,
);
cats.add(cat);
}
log('cats: $cats');
return cats;
}
void filterByCategory(Category query) {
final state = searchKey.currentState;
if (state != null) {
log('filterByCategories: ');
state.safeSetState(() {
if (isCategorySelected) {
state.filter(null);
isCategorySelected = false;
} else {
state.filter(query);
isCategorySelected = true;
}
});
}
}
void onFetchError(Object e, StackTrace s) {
DialogUtil.errorDefault(docKey.currentContext!);
LogUtil.requestAPIFailed(
"proccessRequest.php", "", "Consulta de Veículo", e, s);
}
}