fading_bottom_bar.dart 9.3 KB

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