fading_bottom_bar.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/models/file.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/models/magic_metadata.dart';
  9. import 'package:photos/models/selected_files.dart';
  10. import 'package:photos/models/trash_file.dart';
  11. import 'package:photos/services/collections_service.dart';
  12. import 'package:photos/theme/colors.dart';
  13. import 'package:photos/theme/ente_theme.dart';
  14. import 'package:photos/ui/create_collection_sheet.dart';
  15. import 'package:photos/ui/viewer/file/file_info_widget.dart';
  16. import 'package:photos/utils/delete_file_util.dart';
  17. import 'package:photos/utils/magic_util.dart';
  18. import 'package:photos/utils/share_util.dart';
  19. class FadingBottomBar extends StatefulWidget {
  20. final File file;
  21. final Function(File) onEditRequested;
  22. final bool showOnlyInfoButton;
  23. const FadingBottomBar(
  24. this.file,
  25. this.onEditRequested,
  26. this.showOnlyInfoButton, {
  27. Key? key,
  28. }) : super(key: key);
  29. @override
  30. FadingBottomBarState createState() => FadingBottomBarState();
  31. }
  32. class FadingBottomBarState extends State<FadingBottomBar> {
  33. bool _shouldHide = false;
  34. final GlobalKey shareButtonKey = GlobalKey();
  35. @override
  36. Widget build(BuildContext context) {
  37. return _getBottomBar();
  38. }
  39. void hide() {
  40. setState(() {
  41. _shouldHide = true;
  42. });
  43. }
  44. void show() {
  45. setState(() {
  46. _shouldHide = false;
  47. });
  48. }
  49. void safeRefresh() {
  50. if (mounted) {
  51. setState(() {});
  52. }
  53. }
  54. Widget _getBottomBar() {
  55. final List<Widget> children = [];
  56. children.add(
  57. Tooltip(
  58. message: "Info",
  59. child: Padding(
  60. padding: const EdgeInsets.only(top: 12, bottom: 12),
  61. child: IconButton(
  62. icon: Icon(
  63. Platform.isAndroid ? Icons.info_outline : CupertinoIcons.info,
  64. color: Colors.white,
  65. ),
  66. onPressed: () async {
  67. await _displayInfo(widget.file);
  68. safeRefresh(); //to instantly show the new caption if keypad is closed after pressing 'done' - here the caption will be updated before the bottom sheet is closed
  69. await Future.delayed(
  70. const Duration(milliseconds: 500),
  71. ); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
  72. safeRefresh();
  73. },
  74. ),
  75. ),
  76. ),
  77. );
  78. if (widget.file is TrashFile) {
  79. _addTrashOptions(children);
  80. }
  81. final bool isUploadedByUser = widget.file.uploadedFileID != null &&
  82. widget.file.ownerID == Configuration.instance.getUserID();
  83. bool isFileHidden = false;
  84. if (isUploadedByUser) {
  85. isFileHidden = CollectionsService.instance
  86. .getCollectionByID(widget.file.collectionID!)
  87. ?.isHidden() ??
  88. false;
  89. }
  90. if (!widget.showOnlyInfoButton && widget.file is! TrashFile) {
  91. if (widget.file.fileType == FileType.image ||
  92. widget.file.fileType == FileType.livePhoto) {
  93. children.add(
  94. Tooltip(
  95. message: "Edit",
  96. child: Padding(
  97. padding: const EdgeInsets.only(top: 12, bottom: 12),
  98. child: IconButton(
  99. icon: const Icon(
  100. Icons.tune_outlined,
  101. color: Colors.white,
  102. ),
  103. onPressed: () {
  104. widget.onEditRequested(widget.file);
  105. },
  106. ),
  107. ),
  108. ),
  109. );
  110. }
  111. if (isUploadedByUser && !isFileHidden) {
  112. final bool isArchived =
  113. widget.file.magicMetadata.visibility == visibilityArchive;
  114. children.add(
  115. Tooltip(
  116. message: isArchived ? "Unarchive" : "Archive",
  117. child: Padding(
  118. padding: const EdgeInsets.only(top: 12, bottom: 12),
  119. child: IconButton(
  120. icon: Icon(
  121. isArchived ? Icons.unarchive : Icons.archive_outlined,
  122. color: Colors.white,
  123. ),
  124. onPressed: () async {
  125. await changeVisibility(
  126. context,
  127. [widget.file],
  128. isArchived ? visibilityVisible : visibilityArchive,
  129. );
  130. safeRefresh();
  131. },
  132. ),
  133. ),
  134. ),
  135. );
  136. }
  137. children.add(
  138. Tooltip(
  139. message: "Share",
  140. child: Padding(
  141. padding: const EdgeInsets.only(top: 12, bottom: 12),
  142. child: IconButton(
  143. key: shareButtonKey,
  144. icon: Icon(
  145. Platform.isAndroid
  146. ? Icons.share_outlined
  147. : CupertinoIcons.share,
  148. color: Colors.white,
  149. ),
  150. onPressed: () {
  151. share(context, [widget.file], shareButtonKey: shareButtonKey);
  152. },
  153. ),
  154. ),
  155. ),
  156. );
  157. }
  158. final safeAreaBottomPadding = MediaQuery.of(context).padding.bottom * .5;
  159. return IgnorePointer(
  160. ignoring: _shouldHide,
  161. child: AnimatedOpacity(
  162. opacity: _shouldHide ? 0 : 1,
  163. duration: const Duration(milliseconds: 150),
  164. child: Align(
  165. alignment: Alignment.bottomCenter,
  166. child: Container(
  167. decoration: BoxDecoration(
  168. gradient: LinearGradient(
  169. begin: Alignment.topCenter,
  170. end: Alignment.bottomCenter,
  171. colors: [
  172. Colors.transparent,
  173. Colors.black.withOpacity(0.6),
  174. Colors.black.withOpacity(0.72),
  175. ],
  176. stops: const [0, 0.8, 1],
  177. ),
  178. ),
  179. child: Padding(
  180. padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
  181. child: Column(
  182. mainAxisSize: MainAxisSize.min,
  183. children: [
  184. widget.file.caption?.isNotEmpty ?? false
  185. ? Padding(
  186. padding: const EdgeInsets.fromLTRB(
  187. 16,
  188. 28,
  189. 16,
  190. 12,
  191. ),
  192. child: Text(
  193. widget.file.caption!,
  194. overflow: TextOverflow.ellipsis,
  195. maxLines: 4,
  196. style: getEnteTextTheme(context)
  197. .small
  198. .copyWith(color: textBaseDark),
  199. textAlign: TextAlign.center,
  200. ),
  201. )
  202. : const SizedBox.shrink(),
  203. Row(
  204. mainAxisAlignment: MainAxisAlignment.spaceAround,
  205. children: children,
  206. ),
  207. ],
  208. ),
  209. ),
  210. ),
  211. ),
  212. ),
  213. );
  214. }
  215. void _addTrashOptions(List<Widget> children) {
  216. children.add(
  217. Tooltip(
  218. message: "Restore",
  219. child: Padding(
  220. padding: const EdgeInsets.only(top: 12, bottom: 12),
  221. child: IconButton(
  222. icon: const Icon(
  223. Icons.restore_outlined,
  224. color: Colors.white,
  225. ),
  226. onPressed: () {
  227. final selectedFiles = SelectedFiles();
  228. selectedFiles.toggleSelection(widget.file);
  229. createCollectionSheet(
  230. selectedFiles,
  231. null,
  232. context,
  233. actionType: CollectionActionType.restoreFiles,
  234. );
  235. },
  236. ),
  237. ),
  238. ),
  239. );
  240. children.add(
  241. Tooltip(
  242. message: "Delete",
  243. child: Padding(
  244. padding: const EdgeInsets.only(top: 12, bottom: 12),
  245. child: IconButton(
  246. icon: const Icon(
  247. Icons.delete_forever_outlined,
  248. color: Colors.white,
  249. ),
  250. onPressed: () async {
  251. final trashedFile = <TrashFile>[];
  252. trashedFile.add(widget.file as TrashFile);
  253. if (await deleteFromTrash(context, trashedFile) == true) {
  254. Navigator.pop(context);
  255. }
  256. },
  257. ),
  258. ),
  259. ),
  260. );
  261. }
  262. Future<void> _displayInfo(File file) async {
  263. final colorScheme = getEnteColorScheme(context);
  264. return showBarModalBottomSheet(
  265. topControl: const SizedBox.shrink(),
  266. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
  267. backgroundColor: colorScheme.backgroundElevated,
  268. barrierColor: backdropFaintDark,
  269. context: context,
  270. builder: (BuildContext context) {
  271. return Padding(
  272. padding:
  273. EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  274. child: FileInfoWidget(file),
  275. );
  276. },
  277. );
  278. }
  279. }