detail_page.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:like_button/like_button.dart';
  4. import 'package:photos/core/cache/image_cache.dart';
  5. import 'package:photos/favorite_files_repository.dart';
  6. import 'package:photos/file_repository.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/models/file.dart';
  9. import 'package:photos/ui/video_widget.dart';
  10. import 'package:photos/ui/zoomable_image.dart';
  11. import 'package:photos/utils/date_time_util.dart';
  12. import 'package:photos/utils/file_util.dart';
  13. import 'package:photos/utils/share_util.dart';
  14. import 'package:logging/logging.dart';
  15. class DetailPage extends StatefulWidget {
  16. final List<File> files;
  17. final int selectedIndex;
  18. final String tagPrefix;
  19. DetailPage(this.files, this.selectedIndex, this.tagPrefix, {key})
  20. : super(key: key);
  21. @override
  22. _DetailPageState createState() => _DetailPageState();
  23. }
  24. class _DetailPageState extends State<DetailPage> {
  25. final _logger = Logger("DetailPageState");
  26. bool _shouldDisableScroll = false;
  27. List<File> _files;
  28. PageController _pageController;
  29. int _selectedIndex = 0;
  30. bool _hasPageChanged = false;
  31. @override
  32. void initState() {
  33. _files = widget.files;
  34. _selectedIndex = widget.selectedIndex;
  35. super.initState();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. _logger.info("Opening " +
  40. _files[_selectedIndex].toString() +
  41. ". " +
  42. _selectedIndex.toString() +
  43. " / " +
  44. _files.length.toString() +
  45. " files .");
  46. return Scaffold(
  47. appBar: _buildAppBar(),
  48. extendBodyBehindAppBar: true,
  49. body: Center(
  50. child: Container(
  51. child: _buildPageView(),
  52. ),
  53. ),
  54. backgroundColor: Colors.black,
  55. );
  56. }
  57. Widget _buildPageView() {
  58. _pageController = PageController(initialPage: _selectedIndex);
  59. return PageView.builder(
  60. itemBuilder: (context, index) {
  61. final file = _files[index];
  62. Widget content;
  63. if (file.fileType == FileType.image) {
  64. content = ZoomableImage(
  65. file,
  66. shouldDisableScroll: (value) {
  67. setState(() {
  68. _shouldDisableScroll = value;
  69. });
  70. },
  71. tagPrefix: widget.tagPrefix,
  72. );
  73. } else if (file.fileType == FileType.video) {
  74. content = VideoWidget(
  75. file,
  76. autoPlay: !_hasPageChanged, // Autoplay if it was opened directly
  77. tagPrefix: widget.tagPrefix,
  78. );
  79. } else {
  80. content = Icon(Icons.error);
  81. }
  82. _preloadFiles(index);
  83. return content;
  84. },
  85. onPageChanged: (index) {
  86. setState(() {
  87. _selectedIndex = index;
  88. _hasPageChanged = true;
  89. });
  90. _preloadFiles(index);
  91. },
  92. physics: _shouldDisableScroll
  93. ? NeverScrollableScrollPhysics()
  94. : PageScrollPhysics(),
  95. controller: _pageController,
  96. itemCount: _files.length,
  97. );
  98. }
  99. void _preloadFiles(int index) {
  100. if (index > 0) {
  101. preloadFile(_files[index - 1]);
  102. }
  103. if (index < _files.length - 1) {
  104. preloadFile(_files[index + 1]);
  105. }
  106. }
  107. AppBar _buildAppBar() {
  108. final actions = List<Widget>();
  109. if (_files[_selectedIndex].localId != null) {
  110. actions.add(_getFavoriteButton());
  111. actions.add(_getDeleteButton());
  112. }
  113. actions.add(PopupMenuButton(
  114. itemBuilder: (context) {
  115. return [
  116. PopupMenuItem(
  117. value: 1,
  118. child: Row(
  119. children: [
  120. Icon(Icons.share),
  121. Padding(
  122. padding: EdgeInsets.all(8),
  123. ),
  124. Text("Share"),
  125. ],
  126. ),
  127. ),
  128. PopupMenuItem(
  129. value: 2,
  130. child: Row(
  131. children: [
  132. Icon(Icons.info),
  133. Padding(
  134. padding: EdgeInsets.all(8),
  135. ),
  136. Text("Info"),
  137. ],
  138. ),
  139. )
  140. ];
  141. },
  142. onSelected: (value) {
  143. if (value == 1) {
  144. share(context, _files[_selectedIndex]);
  145. } else if (value == 2) {
  146. _displayInfo(_files[_selectedIndex]);
  147. }
  148. },
  149. ));
  150. return AppBar(
  151. actions: actions,
  152. backgroundColor: Color(0x00000000),
  153. elevation: 0,
  154. );
  155. }
  156. Widget _getFavoriteButton() {
  157. final file = _files[_selectedIndex];
  158. return LikeButton(
  159. isLiked: FavoriteFilesRepository.instance.isLiked(file),
  160. onTap: (oldValue) {
  161. return FavoriteFilesRepository.instance.setLiked(file, !oldValue);
  162. },
  163. likeBuilder: (isLiked) {
  164. return Icon(
  165. Icons.favorite_border,
  166. color: isLiked ? Colors.pinkAccent : Colors.white,
  167. size: 30,
  168. );
  169. },
  170. );
  171. }
  172. Widget _getDeleteButton() {
  173. return IconButton(
  174. icon: Icon(Icons.delete_outline),
  175. iconSize: 30,
  176. onPressed: () {
  177. _showDeleteSheet();
  178. },
  179. );
  180. }
  181. Future<void> _displayInfo(File file) async {
  182. final asset = await file.getAsset();
  183. return showDialog<void>(
  184. context: context,
  185. builder: (BuildContext context) {
  186. var items = <Widget>[
  187. Row(
  188. children: [
  189. Icon(Icons.timer),
  190. Padding(padding: EdgeInsets.all(4)),
  191. Text(getFormattedTime(
  192. DateTime.fromMicrosecondsSinceEpoch(file.creationTime))),
  193. ],
  194. ),
  195. Padding(padding: EdgeInsets.all(4)),
  196. Row(
  197. children: [
  198. Icon(Icons.folder),
  199. Padding(padding: EdgeInsets.all(4)),
  200. Text(file.deviceFolder),
  201. ],
  202. ),
  203. Padding(padding: EdgeInsets.all(4)),
  204. ];
  205. if (file.fileType == FileType.image) {
  206. items.add(Row(
  207. children: [
  208. Icon(Icons.photo_size_select_actual),
  209. Padding(padding: EdgeInsets.all(4)),
  210. Text(asset.width.toString() + " x " + asset.height.toString()),
  211. ],
  212. ));
  213. } else {
  214. items.add(Row(
  215. children: [
  216. Icon(Icons.timer),
  217. Padding(padding: EdgeInsets.all(4)),
  218. Text(asset.videoDuration.toString()),
  219. ],
  220. ));
  221. }
  222. return AlertDialog(
  223. title: Text(file.title),
  224. content: SingleChildScrollView(
  225. child: ListBody(
  226. children: items,
  227. ),
  228. ),
  229. actions: <Widget>[
  230. FlatButton(
  231. child: Text('Ok'),
  232. onPressed: () {
  233. Navigator.of(context).pop();
  234. },
  235. ),
  236. ],
  237. );
  238. },
  239. );
  240. }
  241. void _showDeleteSheet() {
  242. final action = CupertinoActionSheet(
  243. actions: <Widget>[
  244. CupertinoActionSheetAction(
  245. child: Text("Delete on device"),
  246. isDestructiveAction: true,
  247. onPressed: () async {
  248. await _delete(false);
  249. },
  250. ),
  251. CupertinoActionSheetAction(
  252. child: Text("Delete everywhere [WiP]"),
  253. isDestructiveAction: true,
  254. onPressed: () async {
  255. await _delete(true);
  256. },
  257. )
  258. ],
  259. cancelButton: CupertinoActionSheetAction(
  260. child: Text("Cancel"),
  261. onPressed: () {
  262. Navigator.of(context, rootNavigator: true).pop();
  263. },
  264. ),
  265. );
  266. showCupertinoModalPopup(context: context, builder: (_) => action);
  267. }
  268. Future _delete(bool deleteEveryWhere) async {
  269. final file = _files[_selectedIndex];
  270. final totalFiles = _files.length;
  271. if (_selectedIndex == totalFiles - 1) {
  272. // Deleted the last file
  273. await _pageController.previousPage(
  274. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  275. } else {
  276. await _pageController.nextPage(
  277. duration: Duration(milliseconds: 200), curve: Curves.easeInOut);
  278. setState(() {
  279. _files.remove(file);
  280. });
  281. Future.delayed(Duration(milliseconds: 200), () {
  282. _pageController.jumpToPage(_selectedIndex - 1);
  283. });
  284. }
  285. Navigator.of(context, rootNavigator: true).pop(); // Close dialog
  286. if (_files.length == 0) {
  287. // Deleted the last file in gallery
  288. Navigator.of(context, rootNavigator: true).pop(); // Close pageview
  289. Navigator.of(context, rootNavigator: true).pop(); // Close gallery
  290. }
  291. await deleteFiles([file], deleteEveryWhere: deleteEveryWhere);
  292. FileRepository.instance.reloadFiles();
  293. }
  294. }