fading_bottom_bar.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:photos/models/file.dart';
  5. import 'package:photos/models/file_type.dart';
  6. import 'package:photos/models/selected_files.dart';
  7. import 'package:photos/models/trash_file.dart';
  8. import 'package:photos/theme/colors.dart';
  9. import 'package:photos/theme/ente_theme.dart';
  10. import "package:photos/ui/actions/file/file_actions.dart";
  11. import 'package:photos/ui/collection_action_sheet.dart';
  12. import 'package:photos/utils/delete_file_util.dart';
  13. import 'package:photos/utils/share_util.dart';
  14. class FadingBottomBar extends StatefulWidget {
  15. final File file;
  16. final Function(File) onEditRequested;
  17. final Function(File) onFileRemoved;
  18. final bool showOnlyInfoButton;
  19. final int? userID;
  20. const FadingBottomBar(
  21. this.file,
  22. this.onEditRequested,
  23. this.showOnlyInfoButton, {
  24. required this.onFileRemoved,
  25. this.userID,
  26. Key? key,
  27. }) : super(key: key);
  28. @override
  29. FadingBottomBarState createState() => FadingBottomBarState();
  30. }
  31. class FadingBottomBarState extends State<FadingBottomBar> {
  32. bool _shouldHide = false;
  33. final GlobalKey shareButtonKey = GlobalKey();
  34. @override
  35. Widget build(BuildContext context) {
  36. return _getBottomBar();
  37. }
  38. void hide() {
  39. setState(() {
  40. _shouldHide = true;
  41. });
  42. }
  43. void show() {
  44. setState(() {
  45. _shouldHide = false;
  46. });
  47. }
  48. void safeRefresh() {
  49. if (mounted) {
  50. setState(() {});
  51. }
  52. }
  53. Widget _getBottomBar() {
  54. final List<Widget> children = [];
  55. final bool isOwnedByUser =
  56. widget.file.ownerID == null || widget.file.ownerID == widget.userID;
  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. if (!widget.showOnlyInfoButton && widget.file is! TrashFile) {
  83. if (widget.file.fileType == FileType.image ||
  84. widget.file.fileType == FileType.livePhoto) {
  85. children.add(
  86. Tooltip(
  87. message: "Edit",
  88. child: Padding(
  89. padding: const EdgeInsets.only(top: 12, bottom: 12),
  90. child: IconButton(
  91. icon: const Icon(
  92. Icons.tune_outlined,
  93. color: Colors.white,
  94. ),
  95. onPressed: () {
  96. widget.onEditRequested(widget.file);
  97. },
  98. ),
  99. ),
  100. ),
  101. );
  102. }
  103. if (isOwnedByUser) {
  104. children.add(
  105. Tooltip(
  106. message: "Delete",
  107. child: Padding(
  108. padding: const EdgeInsets.only(top: 12, bottom: 12),
  109. child: IconButton(
  110. icon: Icon(
  111. Platform.isAndroid
  112. ? Icons.delete_outline
  113. : CupertinoIcons.delete,
  114. color: Colors.white,
  115. ),
  116. onPressed: () async {
  117. await _showSingleFileDeleteSheet(widget.file);
  118. },
  119. ),
  120. ),
  121. ),
  122. );
  123. }
  124. children.add(
  125. Tooltip(
  126. message: "Share",
  127. child: Padding(
  128. padding: const EdgeInsets.only(top: 12, bottom: 12),
  129. child: IconButton(
  130. key: shareButtonKey,
  131. icon: Icon(
  132. Platform.isAndroid
  133. ? Icons.share_outlined
  134. : CupertinoIcons.share,
  135. color: Colors.white,
  136. ),
  137. onPressed: () {
  138. share(context, [widget.file], shareButtonKey: shareButtonKey);
  139. },
  140. ),
  141. ),
  142. ),
  143. );
  144. }
  145. final safeAreaBottomPadding = MediaQuery.of(context).padding.bottom * .5;
  146. return IgnorePointer(
  147. ignoring: _shouldHide,
  148. child: AnimatedOpacity(
  149. opacity: _shouldHide ? 0 : 1,
  150. duration: const Duration(milliseconds: 150),
  151. child: Align(
  152. alignment: Alignment.bottomCenter,
  153. child: Container(
  154. decoration: BoxDecoration(
  155. gradient: LinearGradient(
  156. begin: Alignment.topCenter,
  157. end: Alignment.bottomCenter,
  158. colors: [
  159. Colors.transparent,
  160. Colors.black.withOpacity(0.6),
  161. Colors.black.withOpacity(0.72),
  162. ],
  163. stops: const [0, 0.8, 1],
  164. ),
  165. ),
  166. child: Padding(
  167. padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
  168. child: Column(
  169. mainAxisSize: MainAxisSize.min,
  170. children: [
  171. widget.file.caption?.isNotEmpty ?? false
  172. ? Padding(
  173. padding: const EdgeInsets.fromLTRB(
  174. 16,
  175. 28,
  176. 16,
  177. 12,
  178. ),
  179. child: Text(
  180. widget.file.caption!,
  181. overflow: TextOverflow.ellipsis,
  182. maxLines: 4,
  183. style: getEnteTextTheme(context)
  184. .small
  185. .copyWith(color: textBaseDark),
  186. textAlign: TextAlign.center,
  187. ),
  188. )
  189. : const SizedBox.shrink(),
  190. Row(
  191. mainAxisAlignment: MainAxisAlignment.spaceAround,
  192. children: children,
  193. ),
  194. ],
  195. ),
  196. ),
  197. ),
  198. ),
  199. ),
  200. );
  201. }
  202. Future<void> _showSingleFileDeleteSheet(File file) async {
  203. await showSingleFileDeleteSheet(
  204. context,
  205. file,
  206. onFileRemoved: widget.onFileRemoved,
  207. );
  208. }
  209. void _addTrashOptions(List<Widget> children) {
  210. children.add(
  211. Tooltip(
  212. message: "Restore",
  213. child: Padding(
  214. padding: const EdgeInsets.only(top: 12, bottom: 12),
  215. child: IconButton(
  216. icon: const Icon(
  217. Icons.restore_outlined,
  218. color: Colors.white,
  219. ),
  220. onPressed: () {
  221. final selectedFiles = SelectedFiles();
  222. selectedFiles.toggleSelection(widget.file);
  223. showCollectionActionSheet(
  224. context,
  225. selectedFiles: selectedFiles,
  226. actionType: CollectionActionType.restoreFiles,
  227. );
  228. },
  229. ),
  230. ),
  231. ),
  232. );
  233. children.add(
  234. Tooltip(
  235. message: "Delete",
  236. child: Padding(
  237. padding: const EdgeInsets.only(top: 12, bottom: 12),
  238. child: IconButton(
  239. icon: const Icon(
  240. Icons.delete_forever_outlined,
  241. color: Colors.white,
  242. ),
  243. onPressed: () async {
  244. final trashedFile = <TrashFile>[];
  245. trashedFile.add(widget.file as TrashFile);
  246. if (await deleteFromTrash(context, trashedFile) == true) {
  247. Navigator.pop(context);
  248. }
  249. },
  250. ),
  251. ),
  252. ),
  253. );
  254. }
  255. Future<void> _displayInfo(File file) async {
  256. await showInfoSheet(context, file);
  257. }
  258. }