file_info_dialog.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/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. style: TextStyle(color: infoColor),
  133. ),
  134. ],
  135. ),
  136. ],
  137. );
  138. }
  139. items.add(
  140. Padding(padding: EdgeInsets.all(12)),
  141. );
  142. items.add(
  143. Row(
  144. mainAxisAlignment:
  145. _isImage ? MainAxisAlignment.spaceBetween : MainAxisAlignment.end,
  146. children: _getActions(),
  147. ),
  148. );
  149. Widget titleContent;
  150. if (file.uploadedFileID == null ||
  151. file.ownerID != Configuration.instance.getUserID()) {
  152. titleContent = Text(file.getDisplayName());
  153. } else {
  154. titleContent = InkWell(
  155. child: Row(
  156. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  157. children: [
  158. Flexible(
  159. child: Text(
  160. file.getDisplayName(),
  161. style: Theme.of(context).textTheme.headline5,
  162. ),
  163. ),
  164. Padding(padding: EdgeInsets.all(8)),
  165. Icon(Icons.edit, color: infoColor),
  166. ],
  167. ),
  168. onTap: () async {
  169. await editFilename(context, file);
  170. setState(() {});
  171. },
  172. );
  173. }
  174. return AlertDialog(
  175. title: titleContent,
  176. content: SingleChildScrollView(
  177. child: ListBody(
  178. children: items,
  179. ),
  180. ),
  181. );
  182. }
  183. List<Widget> _getActions() {
  184. final List<Widget> actions = [];
  185. if (_isImage) {
  186. if (_exif == null) {
  187. actions.add(
  188. TextButton(
  189. child: Row(
  190. mainAxisAlignment: MainAxisAlignment.spaceAround,
  191. children: [
  192. Center(
  193. child: SizedBox.fromSize(
  194. size: Size.square(24),
  195. child: CupertinoActivityIndicator(
  196. radius: 8,
  197. ),
  198. ),
  199. ),
  200. Padding(padding: EdgeInsets.all(4)),
  201. Text(
  202. "EXIF",
  203. style: TextStyle(color: infoColor),
  204. ),
  205. ],
  206. ),
  207. onPressed: () {
  208. showDialog(
  209. context: context,
  210. builder: (BuildContext context) {
  211. return ExifInfoDialog(widget.file);
  212. },
  213. barrierColor: Colors.black87,
  214. );
  215. },
  216. ),
  217. );
  218. } else if (_exif.isNotEmpty) {
  219. actions.add(
  220. TextButton(
  221. child: Row(
  222. mainAxisAlignment: MainAxisAlignment.spaceAround,
  223. children: [
  224. Icon(Icons.feed_outlined, color: infoColor),
  225. Padding(padding: EdgeInsets.all(4)),
  226. Text(
  227. "View raw EXIF",
  228. style: TextStyle(color: infoColor),
  229. ),
  230. ],
  231. ),
  232. onPressed: () {
  233. showDialog(
  234. context: context,
  235. builder: (BuildContext context) {
  236. return ExifInfoDialog(widget.file);
  237. },
  238. barrierColor: Colors.black87,
  239. );
  240. },
  241. ),
  242. );
  243. } else {
  244. actions.add(
  245. TextButton(
  246. child: Row(
  247. mainAxisAlignment: MainAxisAlignment.spaceAround,
  248. children: [
  249. Icon(
  250. Icons.feed_outlined,
  251. color: Theme.of(context)
  252. .colorScheme
  253. .defaultTextColor
  254. .withOpacity(0.5),
  255. ),
  256. Padding(padding: EdgeInsets.all(4)),
  257. Text(
  258. "No exif",
  259. style: TextStyle(
  260. color: Theme.of(context)
  261. .colorScheme
  262. .defaultTextColor
  263. .withOpacity(0.5),
  264. ),
  265. ),
  266. ],
  267. ),
  268. onPressed: () {
  269. showShortToast(context, "This image has no exif data");
  270. },
  271. ),
  272. );
  273. }
  274. }
  275. actions.add(
  276. TextButton(
  277. child: Text(
  278. "Close",
  279. style: TextStyle(
  280. color: infoColor,
  281. ),
  282. ),
  283. onPressed: () {
  284. Navigator.of(context, rootNavigator: true).pop('dialog');
  285. },
  286. ),
  287. );
  288. return actions;
  289. }
  290. Widget _getExifWidgets(Map<String, IfdTag> exif) {
  291. final focalLength = exif["EXIF FocalLength"] != null
  292. ? (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  293. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).denominator
  294. : null;
  295. final fNumber = exif["EXIF FNumber"] != null
  296. ? (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  297. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator
  298. : null;
  299. final List<Widget> children = [];
  300. if (exif["EXIF ExifImageWidth"] != null &&
  301. exif["EXIF ExifImageLength"] != null) {
  302. children.addAll([
  303. Row(
  304. children: [
  305. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  306. Padding(padding: EdgeInsets.all(4)),
  307. Text(
  308. exif["EXIF ExifImageWidth"].toString() +
  309. " x " +
  310. exif["EXIF ExifImageLength"].toString(),
  311. style: TextStyle(color: infoColor),
  312. ),
  313. ],
  314. ),
  315. Padding(padding: EdgeInsets.all(6)),
  316. ]);
  317. } else if (exif["Image ImageWidth"] != null &&
  318. exif["Image ImageLength"] != null) {
  319. children.addAll([
  320. Row(
  321. children: [
  322. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  323. Padding(padding: EdgeInsets.all(4)),
  324. Text(
  325. exif["Image ImageWidth"].toString() +
  326. " x " +
  327. exif["Image ImageLength"].toString(),
  328. style: TextStyle(color: infoColor),
  329. ),
  330. ],
  331. ),
  332. Padding(padding: EdgeInsets.all(6)),
  333. ]);
  334. }
  335. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  336. children.addAll(
  337. [
  338. Row(
  339. children: [
  340. Icon(Icons.camera_outlined, color: infoColor),
  341. Padding(padding: EdgeInsets.all(4)),
  342. Flexible(
  343. child: Text(
  344. exif["Image Make"].toString() +
  345. " " +
  346. exif["Image Model"].toString(),
  347. style: TextStyle(color: infoColor),
  348. overflow: TextOverflow.clip,
  349. ),
  350. ),
  351. ],
  352. ),
  353. Padding(padding: EdgeInsets.all(6)),
  354. ],
  355. );
  356. }
  357. if (fNumber != null) {
  358. children.addAll([
  359. Row(
  360. children: [
  361. Icon(CupertinoIcons.f_cursive, color: infoColor),
  362. Padding(padding: EdgeInsets.all(4)),
  363. Text(
  364. fNumber.toString(),
  365. style: TextStyle(color: infoColor),
  366. ),
  367. ],
  368. ),
  369. Padding(padding: EdgeInsets.all(6)),
  370. ]);
  371. }
  372. if (focalLength != null) {
  373. children.addAll([
  374. Row(
  375. children: [
  376. Icon(Icons.center_focus_strong_outlined, color: infoColor),
  377. Padding(padding: EdgeInsets.all(4)),
  378. Text(focalLength.toString() + " mm",
  379. style: TextStyle(color: infoColor)),
  380. ],
  381. ),
  382. Padding(padding: EdgeInsets.all(6)),
  383. ]);
  384. }
  385. if (exif["EXIF ExposureTime"] != null) {
  386. children.addAll([
  387. Row(
  388. children: [
  389. Icon(Icons.shutter_speed, color: infoColor),
  390. Padding(padding: EdgeInsets.all(4)),
  391. Text(
  392. exif["EXIF ExposureTime"].toString(),
  393. style: TextStyle(color: infoColor),
  394. ),
  395. ],
  396. ),
  397. Padding(padding: EdgeInsets.all(6)),
  398. ]);
  399. }
  400. return Column(
  401. children: children,
  402. );
  403. }
  404. Widget _getFileSize() {
  405. return FutureBuilder(
  406. future: getFile(widget.file).then((f) => f.length()),
  407. builder: (context, snapshot) {
  408. if (snapshot.hasData) {
  409. return Text(
  410. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  411. style: TextStyle(color: infoColor),
  412. );
  413. } else {
  414. return Center(
  415. child: SizedBox.fromSize(
  416. size: Size.square(24),
  417. child: CupertinoActivityIndicator(
  418. radius: 8,
  419. ),
  420. ),
  421. );
  422. }
  423. },
  424. );
  425. }
  426. }