|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; |
| 3 | +import 'package:piwigo_ng/components/fields/app_field.dart'; |
| 4 | +import 'package:piwigo_ng/models/tag_model.dart'; |
| 5 | +import 'package:piwigo_ng/network/api_error.dart'; |
| 6 | +import 'package:piwigo_ng/network/tags.dart'; |
| 7 | +import 'package:piwigo_ng/utils/localizations.dart'; |
| 8 | +import 'package:piwigo_ng/views/image/image_tags_page.dart'; |
| 9 | + |
| 10 | +class OpenTagModal extends StatefulWidget { |
| 11 | + const OpenTagModal({super.key}); |
| 12 | + |
| 13 | + @override |
| 14 | + _OpenTagModalState createState() => _OpenTagModalState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _OpenTagModalState extends State<OpenTagModal> { |
| 18 | + final ScrollController _scrollController = ScrollController(); |
| 19 | + late final Future _tagsFuture; |
| 20 | + |
| 21 | + String _searchQuery = ''; |
| 22 | + List<TagModel>? _tagList; |
| 23 | + |
| 24 | + @override |
| 25 | + void initState() { |
| 26 | + super.initState(); |
| 27 | + _tagsFuture = _onRefresh(); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + void dispose() { |
| 32 | + _scrollController.dispose(); |
| 33 | + super.dispose(); |
| 34 | + } |
| 35 | + |
| 36 | + Future<void> _onRefresh() async { |
| 37 | + try { |
| 38 | + final ApiResponse<List<TagModel>> result = await getTags(); |
| 39 | + if (!result.hasData) return; |
| 40 | + setState(() { |
| 41 | + _tagList = result.data!.where((tag) => tag.counter > 0).toList()..sort((a, b) => a.name.compareTo(b.name)); |
| 42 | + }); |
| 43 | + } catch (e) { |
| 44 | + setState(() { |
| 45 | + _tagList = null; |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + void _onSelectTag(TagModel tag) { |
| 51 | + Navigator.of(context).pop(); |
| 52 | + Navigator.of(context).pushNamed( |
| 53 | + ImageTagsPage.routeName, |
| 54 | + arguments: { |
| 55 | + 'tag': tag, |
| 56 | + }, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + Widget build(BuildContext context) { |
| 62 | + return Scaffold( |
| 63 | + backgroundColor: Colors.transparent, |
| 64 | + appBar: AppBar( |
| 65 | + shape: const RoundedRectangleBorder( |
| 66 | + borderRadius: BorderRadius.vertical( |
| 67 | + top: Radius.circular(15.0), |
| 68 | + ), |
| 69 | + ), |
| 70 | + elevation: 0.0, |
| 71 | + scrolledUnderElevation: 5.0, |
| 72 | + leading: IconButton( |
| 73 | + icon: Icon(Icons.close), |
| 74 | + onPressed: () => Navigator.of(context).pop(), |
| 75 | + ), |
| 76 | + title: Text(appStrings.tags), |
| 77 | + ), |
| 78 | + body: ListView( |
| 79 | + controller: ModalScrollController.of(context), |
| 80 | + physics: const AlwaysScrollableScrollPhysics(), |
| 81 | + children: [ |
| 82 | + Padding( |
| 83 | + padding: const EdgeInsets.symmetric( |
| 84 | + horizontal: 16.0, |
| 85 | + vertical: 8.0, |
| 86 | + ), |
| 87 | + child: AppField( |
| 88 | + padding: const EdgeInsets.symmetric(vertical: 8.0), |
| 89 | + prefix: Icon(Icons.search), |
| 90 | + onChanged: (query) => setState(() { |
| 91 | + _searchQuery = query; |
| 92 | + }), |
| 93 | + ), |
| 94 | + ), |
| 95 | + FutureBuilder( |
| 96 | + future: _tagsFuture, |
| 97 | + builder: (context, snapshot) { |
| 98 | + switch (snapshot.connectionState) { |
| 99 | + case ConnectionState.done: |
| 100 | + return _buildTagList(); |
| 101 | + default: |
| 102 | + return Center( |
| 103 | + child: CircularProgressIndicator(), |
| 104 | + ); |
| 105 | + } |
| 106 | + }, |
| 107 | + ), |
| 108 | + ], |
| 109 | + ), |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + Widget _buildTagList() { |
| 114 | + if (_tagList == null) { |
| 115 | + return Center( |
| 116 | + child: Text(appStrings.coreDataFetch_TagError), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + List<TagModel> tags = |
| 121 | + _tagList!.where((tag) => tag.name.toLowerCase().contains(_searchQuery.toLowerCase())).toList(); |
| 122 | + if (tags.isEmpty) { |
| 123 | + return Center( |
| 124 | + child: Text(appStrings.none), |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return Column( |
| 129 | + children: tags.map((tag) => _buildItem(tag)).toList(), |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + Widget _buildItem(TagModel tag) => ListTile( |
| 134 | + visualDensity: VisualDensity.compact, |
| 135 | + shape: Border( |
| 136 | + bottom: BorderSide(color: Theme.of(context).scaffoldBackgroundColor), |
| 137 | + ), |
| 138 | + title: Text(tag.name), |
| 139 | + trailing: Text(appStrings.imageCount(tag.counter)), |
| 140 | + onTap: () => _onSelectTag(tag), |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +Future<int?> showOpenTagModal(BuildContext context) async { |
| 145 | + return showMaterialModalBottomSheet<int>( |
| 146 | + context: context, |
| 147 | + enableDrag: false, |
| 148 | + shape: RoundedRectangleBorder( |
| 149 | + borderRadius: BorderRadius.vertical(top: Radius.circular(30.0)), |
| 150 | + ), |
| 151 | + backgroundColor: Theme.of(context).scaffoldBackgroundColor, |
| 152 | + builder: (context) => OpenTagModal(), |
| 153 | + ); |
| 154 | +} |
0 commit comments