fading_bottom_bar.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import "package:photos/generated/l10n.dart";
  5. import 'package:photos/models/file.dart';
  6. import 'package:photos/models/file_type.dart';
  7. import 'package:photos/models/selected_files.dart';
  8. import 'package:photos/models/trash_file.dart';
  9. import 'package:photos/theme/colors.dart';
  10. import 'package:photos/theme/ente_theme.dart';
  11. import "package:photos/ui/actions/file/file_actions.dart";
  12. import 'package:photos/ui/collection_action_sheet.dart';
  13. import 'package:photos/utils/delete_file_util.dart';
  14. import 'package:photos/utils/share_util.dart';
  15. class FadingBottomBar extends StatefulWidget {
  16. final File file;
  17. final Function(File) onEditRequested;
  18. final Function(File) onFileRemoved;
  19. final bool showOnlyInfoButton;
  20. final int? userID;
  21. const FadingBottomBar(
  22. this.file,
  23. this.onEditRequested,
  24. this.showOnlyInfoButton, {
  25. required this.onFileRemoved,
  26. this.userID,
  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. final bool isOwnedByUser =
  57. widget.file.ownerID == null || widget.file.ownerID == widget.userID;
  58. children.add(
  59. Tooltip(
  60. message: "Info",
  61. child: Padding(
  62. padding: const EdgeInsets.only(top: 12, bottom: 12),
  63. child: IconButton(
  64. icon: Icon(
  65. Platform.isAndroid ? Icons.info_outline : CupertinoIcons.info,
  66. color: Colors.white,
  67. ),
  68. onPressed: () async {
  69. await _displayDetails(widget.file);
  70. 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
  71. await Future.delayed(
  72. const Duration(milliseconds: 500),
  73. ); //Waiting for some time till the caption gets updated in db if the user closes the bottom sheet without pressing 'done'
  74. safeRefresh();
  75. },
  76. ),
  77. ),
  78. ),
  79. );
  80. if (widget.file is TrashFile) {
  81. _addTrashOptions(children);
  82. }
  83. if (!widget.showOnlyInfoButton && widget.file is! TrashFile) {
  84. if (widget.file.fileType == FileType.image ||
  85. widget.file.fileType == FileType.livePhoto) {
  86. children.add(
  87. Tooltip(
  88. message: "Edit",
  89. child: Padding(
  90. padding: const EdgeInsets.only(top: 12, bottom: 12),
  91. child: IconButton(
  92. icon: const Icon(
  93. Icons.tune_outlined,
  94. color: Colors.white,
  95. ),
  96. onPressed: () {
  97. widget.onEditRequested(widget.file);
  98. },
  99. ),
  100. ),
  101. ),
  102. );
  103. }
  104. if (isOwnedByUser) {
  105. children.add(
  106. Tooltip(
  107. message: S.of(context).delete,
  108. child: Padding(
  109. padding: const EdgeInsets.only(top: 12, bottom: 12),
  110. child: IconButton(
  111. icon: Icon(
  112. Platform.isAndroid
  113. ? Icons.delete_outline
  114. : CupertinoIcons.delete,
  115. color: Colors.white,
  116. ),
  117. onPressed: () async {
  118. await _showSingleFileDeleteSheet(widget.file);
  119. },
  120. ),
  121. ),
  122. ),
  123. );
  124. }
  125. children.add(
  126. Tooltip(
  127. message: S.of(context).share,
  128. child: Padding(
  129. padding: const EdgeInsets.only(top: 12, bottom: 12),
  130. child: IconButton(
  131. key: shareButtonKey,
  132. icon: Icon(
  133. Platform.isAndroid
  134. ? Icons.share_outlined
  135. : CupertinoIcons.share,
  136. color: Colors.white,
  137. ),
  138. onPressed: () {
  139. share(context, [widget.file], shareButtonKey: shareButtonKey);
  140. },
  141. ),
  142. ),
  143. ),
  144. );
  145. }
  146. final safeAreaBottomPadding = MediaQuery.of(context).padding.bottom * .5;
  147. return IgnorePointer(
  148. ignoring: _shouldHide,
  149. child: AnimatedOpacity(
  150. opacity: _shouldHide ? 0 : 1,
  151. duration: const Duration(milliseconds: 150),
  152. child: Align(
  153. alignment: Alignment.bottomCenter,
  154. child: Container(
  155. decoration: BoxDecoration(
  156. gradient: LinearGradient(
  157. begin: Alignment.topCenter,
  158. end: Alignment.bottomCenter,
  159. colors: [
  160. Colors.transparent,
  161. Colors.black.withOpacity(0.6),
  162. Colors.black.withOpacity(0.72),
  163. ],
  164. stops: const [0, 0.8, 1],
  165. ),
  166. ),
  167. child: Padding(
  168. padding: EdgeInsets.only(bottom: safeAreaBottomPadding),
  169. child: Column(
  170. mainAxisSize: MainAxisSize.min,
  171. children: [
  172. widget.file.caption?.isNotEmpty ?? false
  173. ? Padding(
  174. padding: const EdgeInsets.fromLTRB(
  175. 16,
  176. 28,
  177. 16,
  178. 12,
  179. ),
  180. child: Text(
  181. widget.file.caption!,
  182. overflow: TextOverflow.ellipsis,
  183. maxLines: 4,
  184. style: getEnteTextTheme(context)
  185. .small
  186. .copyWith(color: textBaseDark),
  187. textAlign: TextAlign.center,
  188. ),
  189. )
  190. : const SizedBox.shrink(),
  191. Row(
  192. mainAxisAlignment: MainAxisAlignment.spaceAround,
  193. children: children,
  194. ),
  195. ],
  196. ),
  197. ),
  198. ),
  199. ),
  200. ),
  201. );
  202. }
  203. Future<void> _showSingleFileDeleteSheet(File file) async {
  204. await showSingleFileDeleteSheet(
  205. context,
  206. file,
  207. onFileRemoved: widget.onFileRemoved,
  208. );
  209. }
  210. void _addTrashOptions(List<Widget> children) {
  211. children.add(
  212. Tooltip(
  213. message: S.of(context).restore,
  214. child: Padding(
  215. padding: const EdgeInsets.only(top: 12, bottom: 12),
  216. child: IconButton(
  217. icon: const Icon(
  218. Icons.restore_outlined,
  219. color: Colors.white,
  220. ),
  221. onPressed: () {
  222. final selectedFiles = SelectedFiles();
  223. selectedFiles.toggleSelection(widget.file);
  224. showCollectionActionSheet(
  225. context,
  226. selectedFiles: selectedFiles,
  227. actionType: CollectionActionType.restoreFiles,
  228. );
  229. },
  230. ),
  231. ),
  232. ),
  233. );
  234. children.add(
  235. Tooltip(
  236. message: S.of(context).delete,
  237. child: Padding(
  238. padding: const EdgeInsets.only(top: 12, bottom: 12),
  239. child: IconButton(
  240. icon: const Icon(
  241. Icons.delete_forever_outlined,
  242. color: Colors.white,
  243. ),
  244. onPressed: () async {
  245. final trashedFile = <TrashFile>[];
  246. trashedFile.add(widget.file as TrashFile);
  247. if (await deleteFromTrash(context, trashedFile) == true) {
  248. Navigator.pop(context);
  249. }
  250. },
  251. ),
  252. ),
  253. ),
  254. );
  255. }
  256. Future<void> _displayDetails(File file) async {
  257. await showDetailsSheet(context, file);
  258. }
  259. }