|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; |
| 4 | +import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; |
| 5 | +import 'package:phosphor_flutter/phosphor_flutter.dart'; |
| 6 | +import 'package:flow_api/models/model.dart'; |
| 7 | +import 'package:flow_api/models/resource/model.dart'; |
| 8 | +import 'package:flow_api/models/resource/service.dart'; |
| 9 | + |
| 10 | +import '../../cubits/flow.dart'; |
| 11 | +import '../../widgets/builder_delegate.dart'; |
| 12 | +import '../resources/resource.dart'; |
| 13 | + |
| 14 | +class ResourcesView<T extends DescriptiveModel> extends StatefulWidget { |
| 15 | + final T model; |
| 16 | + final String source; |
| 17 | + final ResourceConnector<T> connector; |
| 18 | + |
| 19 | + const ResourcesView( |
| 20 | + {super.key, |
| 21 | + required this.source, |
| 22 | + required this.connector, |
| 23 | + required this.model}); |
| 24 | + |
| 25 | + @override |
| 26 | + State<ResourcesView<T>> createState() => _ResourcesViewState(); |
| 27 | +} |
| 28 | + |
| 29 | +class _ResourcesViewState<T extends DescriptiveModel> |
| 30 | + extends State<ResourcesView<T>> { |
| 31 | + static const _pageSize = 20; |
| 32 | + |
| 33 | + late final ResourceService? _resourceService; |
| 34 | + |
| 35 | + final PagingController<int, Resource> _pagingController = |
| 36 | + PagingController(firstPageKey: 0); |
| 37 | + |
| 38 | + @override |
| 39 | + void initState() { |
| 40 | + final service = context.read<FlowCubit>().getService(widget.source); |
| 41 | + _resourceService = service.resource; |
| 42 | + _pagingController.addPageRequestListener((pageKey) { |
| 43 | + _fetchPage(pageKey); |
| 44 | + }); |
| 45 | + super.initState(); |
| 46 | + } |
| 47 | + |
| 48 | + Future<void> _fetchPage(int pageKey) async { |
| 49 | + try { |
| 50 | + final newItems = await widget.connector.getResources(widget.model.id!, |
| 51 | + offset: pageKey * _pageSize, limit: _pageSize); |
| 52 | + final isLastPage = newItems.length < _pageSize; |
| 53 | + if (isLastPage) { |
| 54 | + _pagingController.appendLastPage(newItems); |
| 55 | + } else { |
| 56 | + final nextPageKey = pageKey + 1; |
| 57 | + _pagingController.appendPage(newItems, nextPageKey); |
| 58 | + } |
| 59 | + } catch (error) { |
| 60 | + _pagingController.error = error; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + Widget build(BuildContext context) => |
| 66 | + // Don't worry about displaying progress or error indicators on screen; the |
| 67 | + // package takes care of that. If you want to customize them, use the |
| 68 | + // [PagedChildBuilderDelegate] properties. |
| 69 | + Stack( |
| 70 | + children: [ |
| 71 | + Column( |
| 72 | + children: [ |
| 73 | + Flexible( |
| 74 | + child: PagedListView<int, Resource>( |
| 75 | + pagingController: _pagingController, |
| 76 | + builderDelegate: buildMaterialPagedDelegate<Resource>( |
| 77 | + _pagingController, |
| 78 | + (context, item, index) { |
| 79 | + return Dismissible( |
| 80 | + key: ValueKey(item.id), |
| 81 | + background: Container(color: Colors.red), |
| 82 | + onDismissed: (direction) { |
| 83 | + _resourceService?.deleteResource(item.id!); |
| 84 | + _pagingController.itemList!.remove(item); |
| 85 | + }, |
| 86 | + child: ListTile( |
| 87 | + title: Text(item.name), |
| 88 | + onTap: () async { |
| 89 | + await showDialog<Resource>( |
| 90 | + context: context, |
| 91 | + builder: (context) => ResourceDialog( |
| 92 | + source: widget.source, |
| 93 | + resource: item, |
| 94 | + ), |
| 95 | + ); |
| 96 | + _pagingController.refresh(); |
| 97 | + }, |
| 98 | + ), |
| 99 | + ); |
| 100 | + }, |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + const SizedBox(height: 64), |
| 105 | + ], |
| 106 | + ), |
| 107 | + Align( |
| 108 | + alignment: Alignment.bottomCenter, |
| 109 | + child: Padding( |
| 110 | + padding: const EdgeInsets.all(16.0), |
| 111 | + child: FloatingActionButton.extended( |
| 112 | + label: Text(AppLocalizations.of(context).create), |
| 113 | + icon: const PhosphorIcon(PhosphorIconsLight.plus), |
| 114 | + onPressed: () async { |
| 115 | + final resource = await showDialog<Resource>( |
| 116 | + context: context, |
| 117 | + builder: (context) => ResourceDialog( |
| 118 | + source: widget.source, |
| 119 | + ), |
| 120 | + ); |
| 121 | + if (resource != null) { |
| 122 | + await widget.connector |
| 123 | + .connect(widget.model.id!, resource.id!); |
| 124 | + } |
| 125 | + _pagingController.refresh(); |
| 126 | + }, |
| 127 | + ), |
| 128 | + ), |
| 129 | + ), |
| 130 | + ], |
| 131 | + ); |
| 132 | + |
| 133 | + @override |
| 134 | + void dispose() { |
| 135 | + _pagingController.dispose(); |
| 136 | + super.dispose(); |
| 137 | + } |
| 138 | +} |
0 commit comments