file_info_dialog.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import 'package:exif/exif.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/ente_theme_data.dart';
  6. import 'package:photos/models/file.dart';
  7. import 'package:photos/models/file_type.dart';
  8. import 'package:photos/services/collections_service.dart';
  9. import 'package:photos/ui/viewer/file/exif_info_dialog.dart';
  10. import 'package:photos/utils/date_time_util.dart';
  11. import 'package:photos/utils/exif_util.dart';
  12. import 'package:photos/utils/file_util.dart';
  13. import 'package:photos/utils/magic_util.dart';
  14. import 'package:photos/utils/toast_util.dart';
  15. class FileInfoWidget extends StatefulWidget {
  16. final File file;
  17. const FileInfoWidget(
  18. this.file, {
  19. Key key,
  20. }) : super(key: key);
  21. @override
  22. _FileInfoWidgetState createState() => _FileInfoWidgetState();
  23. }
  24. class _FileInfoWidgetState extends State<FileInfoWidget> {
  25. Map<String, IfdTag> _exif;
  26. bool _isImage = false;
  27. Color infoColor;
  28. @override
  29. void initState() {
  30. _isImage = widget.file.fileType == FileType.image ||
  31. widget.file.fileType == FileType.livePhoto;
  32. if (_isImage) {
  33. getExif(widget.file).then((exif) {
  34. setState(() {
  35. _exif = exif;
  36. });
  37. });
  38. }
  39. super.initState();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. final file = widget.file;
  44. infoColor = Theme.of(context).colorScheme.onSurface.withOpacity(0.85);
  45. var items = <Widget>[
  46. Row(
  47. children: [
  48. Icon(Icons.calendar_today_outlined, color: infoColor),
  49. Padding(padding: EdgeInsets.all(4)),
  50. Text(
  51. getFormattedTime(
  52. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  53. ),
  54. style: TextStyle(color: infoColor),
  55. ),
  56. ],
  57. ),
  58. Padding(padding: EdgeInsets.all(6)),
  59. Row(
  60. children: [
  61. Icon(Icons.folder_outlined, color: infoColor),
  62. Padding(padding: EdgeInsets.all(4)),
  63. Text(
  64. file.deviceFolder ??
  65. CollectionsService.instance
  66. .getCollectionByID(file.collectionID)
  67. .name,
  68. style: TextStyle(color: infoColor),
  69. ),
  70. ],
  71. ),
  72. Padding(padding: EdgeInsets.all(6)),
  73. ];
  74. items.addAll(
  75. [
  76. Row(
  77. children: [
  78. Icon(Icons.sd_storage_outlined, color: infoColor),
  79. Padding(padding: EdgeInsets.all(4)),
  80. _getFileSize(),
  81. ],
  82. ),
  83. Padding(padding: EdgeInsets.all(6)),
  84. ],
  85. );
  86. if (file.localID != null && !_isImage) {
  87. items.addAll(
  88. [
  89. Row(
  90. children: [
  91. Icon(Icons.timer_outlined, color: infoColor),
  92. Padding(padding: EdgeInsets.all(4)),
  93. FutureBuilder(
  94. future: file.getAsset(),
  95. builder: (context, snapshot) {
  96. if (snapshot.hasData) {
  97. return Text(
  98. snapshot.data.videoDuration.toString().split(".")[0],
  99. style: TextStyle(color: infoColor),
  100. );
  101. } else {
  102. return Center(
  103. child: SizedBox.fromSize(
  104. size: Size.square(24),
  105. child: CupertinoActivityIndicator(
  106. radius: 8,
  107. ),
  108. ),
  109. );
  110. }
  111. },
  112. ),
  113. ],
  114. ),
  115. Padding(padding: EdgeInsets.all(6)),
  116. ],
  117. );
  118. }
  119. if (_isImage && _exif != null) {
  120. items.add(_getExifWidgets(_exif));
  121. }
  122. if (file.uploadedFileID != null && file.updationTime != null) {
  123. items.addAll(
  124. [
  125. Row(
  126. children: [
  127. Icon(Icons.cloud_upload_outlined, color: infoColor),
  128. Padding(padding: EdgeInsets.all(4)),
  129. Text(
  130. getFormattedTime(
  131. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  132. ),
  133. style: TextStyle(color: infoColor),
  134. ),
  135. ],
  136. ),
  137. ],
  138. );
  139. }
  140. items.add(
  141. Padding(padding: EdgeInsets.all(12)),
  142. );
  143. items.add(
  144. Row(
  145. mainAxisAlignment:
  146. _isImage ? MainAxisAlignment.spaceBetween : MainAxisAlignment.end,
  147. children: _getActions(),
  148. ),
  149. );
  150. Widget titleContent;
  151. if (file.uploadedFileID == null ||
  152. file.ownerID != Configuration.instance.getUserID()) {
  153. titleContent = Text(file.getDisplayName());
  154. } else {
  155. titleContent = InkWell(
  156. child: Row(
  157. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  158. children: [
  159. Flexible(
  160. child: Text(
  161. file.getDisplayName(),
  162. style: Theme.of(context).textTheme.headline5,
  163. ),
  164. ),
  165. Padding(padding: EdgeInsets.all(8)),
  166. Icon(Icons.edit, color: infoColor),
  167. ],
  168. ),
  169. onTap: () async {
  170. await editFilename(context, file);
  171. setState(() {});
  172. },
  173. );
  174. }
  175. return AlertDialog(
  176. title: titleContent,
  177. content: SingleChildScrollView(
  178. child: ListBody(
  179. children: items,
  180. ),
  181. ),
  182. );
  183. }
  184. List<Widget> _getActions() {
  185. final List<Widget> actions = [];
  186. if (_isImage) {
  187. if (_exif == null) {
  188. actions.add(
  189. TextButton(
  190. child: Row(
  191. mainAxisAlignment: MainAxisAlignment.spaceAround,
  192. children: [
  193. Center(
  194. child: SizedBox.fromSize(
  195. size: Size.square(24),
  196. child: CupertinoActivityIndicator(
  197. radius: 8,
  198. ),
  199. ),
  200. ),
  201. Padding(padding: EdgeInsets.all(4)),
  202. Text(
  203. "EXIF",
  204. style: TextStyle(color: infoColor),
  205. ),
  206. ],
  207. ),
  208. onPressed: () {
  209. showDialog(
  210. context: context,
  211. builder: (BuildContext context) {
  212. return ExifInfoDialog(widget.file);
  213. },
  214. barrierColor: Colors.black87,
  215. );
  216. },
  217. ),
  218. );
  219. } else if (_exif.isNotEmpty) {
  220. actions.add(
  221. TextButton(
  222. child: Row(
  223. mainAxisAlignment: MainAxisAlignment.spaceAround,
  224. children: [
  225. Icon(Icons.feed_outlined, color: infoColor),
  226. Padding(padding: EdgeInsets.all(4)),
  227. Text(
  228. "View raw EXIF",
  229. style: TextStyle(color: infoColor),
  230. ),
  231. ],
  232. ),
  233. onPressed: () {
  234. showDialog(
  235. context: context,
  236. builder: (BuildContext context) {
  237. return ExifInfoDialog(widget.file);
  238. },
  239. barrierColor: Colors.black87,
  240. );
  241. },
  242. ),
  243. );
  244. } else {
  245. actions.add(
  246. TextButton(
  247. child: Row(
  248. mainAxisAlignment: MainAxisAlignment.spaceAround,
  249. children: [
  250. Icon(
  251. Icons.feed_outlined,
  252. color: Theme.of(context)
  253. .colorScheme
  254. .defaultTextColor
  255. .withOpacity(0.5),
  256. ),
  257. Padding(padding: EdgeInsets.all(4)),
  258. Text(
  259. "No exif",
  260. style: TextStyle(
  261. color: Theme.of(context)
  262. .colorScheme
  263. .defaultTextColor
  264. .withOpacity(0.5),
  265. ),
  266. ),
  267. ],
  268. ),
  269. onPressed: () {
  270. showShortToast(context, "This image has no exif data");
  271. },
  272. ),
  273. );
  274. }
  275. }
  276. actions.add(
  277. TextButton(
  278. child: Text(
  279. "Close",
  280. style: TextStyle(
  281. color: infoColor,
  282. ),
  283. ),
  284. onPressed: () {
  285. Navigator.of(context, rootNavigator: true).pop('dialog');
  286. },
  287. ),
  288. );
  289. return actions;
  290. }
  291. Widget _getExifWidgets(Map<String, IfdTag> exif) {
  292. final focalLength = exif["EXIF FocalLength"] != null
  293. ? (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  294. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).denominator
  295. : null;
  296. final fNumber = exif["EXIF FNumber"] != null
  297. ? (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  298. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator
  299. : null;
  300. final List<Widget> children = [];
  301. if (exif["EXIF ExifImageWidth"] != null &&
  302. exif["EXIF ExifImageLength"] != null) {
  303. children.addAll([
  304. Row(
  305. children: [
  306. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  307. Padding(padding: EdgeInsets.all(4)),
  308. Text(
  309. exif["EXIF ExifImageWidth"].toString() +
  310. " x " +
  311. exif["EXIF ExifImageLength"].toString(),
  312. style: TextStyle(color: infoColor),
  313. ),
  314. ],
  315. ),
  316. Padding(padding: EdgeInsets.all(6)),
  317. ]);
  318. } else if (exif["Image ImageWidth"] != null &&
  319. exif["Image ImageLength"] != null) {
  320. children.addAll([
  321. Row(
  322. children: [
  323. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  324. Padding(padding: EdgeInsets.all(4)),
  325. Text(
  326. exif["Image ImageWidth"].toString() +
  327. " x " +
  328. exif["Image ImageLength"].toString(),
  329. style: TextStyle(color: infoColor),
  330. ),
  331. ],
  332. ),
  333. Padding(padding: EdgeInsets.all(6)),
  334. ]);
  335. }
  336. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  337. children.addAll(
  338. [
  339. Row(
  340. children: [
  341. Icon(Icons.camera_outlined, color: infoColor),
  342. Padding(padding: EdgeInsets.all(4)),
  343. Flexible(
  344. child: Text(
  345. exif["Image Make"].toString() +
  346. " " +
  347. exif["Image Model"].toString(),
  348. style: TextStyle(color: infoColor),
  349. overflow: TextOverflow.clip,
  350. ),
  351. ),
  352. ],
  353. ),
  354. Padding(padding: EdgeInsets.all(6)),
  355. ],
  356. );
  357. }
  358. if (fNumber != null) {
  359. children.addAll([
  360. Row(
  361. children: [
  362. Icon(CupertinoIcons.f_cursive, color: infoColor),
  363. Padding(padding: EdgeInsets.all(4)),
  364. Text(
  365. fNumber.toString(),
  366. style: TextStyle(color: infoColor),
  367. ),
  368. ],
  369. ),
  370. Padding(padding: EdgeInsets.all(6)),
  371. ]);
  372. }
  373. if (focalLength != null) {
  374. children.addAll([
  375. Row(
  376. children: [
  377. Icon(Icons.center_focus_strong_outlined, color: infoColor),
  378. Padding(padding: EdgeInsets.all(4)),
  379. Text(
  380. focalLength.toString() + " mm",
  381. style: TextStyle(color: infoColor),
  382. ),
  383. ],
  384. ),
  385. Padding(padding: EdgeInsets.all(6)),
  386. ]);
  387. }
  388. if (exif["EXIF ExposureTime"] != null) {
  389. children.addAll([
  390. Row(
  391. children: [
  392. Icon(Icons.shutter_speed, color: infoColor),
  393. Padding(padding: EdgeInsets.all(4)),
  394. Text(
  395. exif["EXIF ExposureTime"].toString(),
  396. style: TextStyle(color: infoColor),
  397. ),
  398. ],
  399. ),
  400. Padding(padding: EdgeInsets.all(6)),
  401. ]);
  402. }
  403. return Column(
  404. children: children,
  405. );
  406. }
  407. Widget _getFileSize() {
  408. return FutureBuilder(
  409. future: getFile(widget.file).then((f) => f.length()),
  410. builder: (context, snapshot) {
  411. if (snapshot.hasData) {
  412. return Text(
  413. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  414. style: TextStyle(color: infoColor),
  415. );
  416. } else {
  417. return Center(
  418. child: SizedBox.fromSize(
  419. size: Size.square(24),
  420. child: CupertinoActivityIndicator(
  421. radius: 8,
  422. ),
  423. ),
  424. );
  425. }
  426. },
  427. );
  428. }
  429. }