file_info_dialog.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. import "dart:io";
  2. import "package:exif/exif.dart";
  3. import "package:flutter/cupertino.dart";
  4. import "package:flutter/material.dart";
  5. import "package:photos/core/configuration.dart";
  6. import "package:photos/ente_theme_data.dart";
  7. import "package:photos/models/file.dart";
  8. import "package:photos/models/file_type.dart";
  9. import "package:photos/services/collections_service.dart";
  10. import "package:photos/ui/viewer/file/exif_info_dialog.dart";
  11. import "package:photos/utils/date_time_util.dart";
  12. import "package:photos/utils/exif_util.dart";
  13. import "package:photos/utils/file_util.dart";
  14. import "package:photos/utils/magic_util.dart";
  15. import "package:photos/utils/toast_util.dart";
  16. class FileInfoWidget extends StatefulWidget {
  17. final File file;
  18. const FileInfoWidget(
  19. this.file, {
  20. Key key,
  21. }) : super(key: key);
  22. @override
  23. State<FileInfoWidget> createState() => _FileInfoWidgetState();
  24. }
  25. class _FileInfoWidgetState extends State<FileInfoWidget> {
  26. Map<String, IfdTag> _exif;
  27. final Map<String, dynamic> _exifData = {
  28. "focalLength": null,
  29. "fNumber": null,
  30. "resolution": null,
  31. "takenOnDevice": null,
  32. "exposureTime": null,
  33. "ISO": null,
  34. "megaPixels": null
  35. };
  36. bool _isImage = false;
  37. Color infoColor;
  38. @override
  39. void initState() {
  40. debugPrint('file_info_dialog initState' + _exifData.toString());
  41. _isImage = widget.file.fileType == FileType.image ||
  42. widget.file.fileType == FileType.livePhoto;
  43. if (_isImage) {
  44. getExif(widget.file).then((exif) {
  45. setState(() {
  46. _exif = exif;
  47. });
  48. });
  49. }
  50. super.initState();
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. final file = widget.file;
  55. final dateTime = DateTime.fromMicrosecondsSinceEpoch(file.creationTime);
  56. final dateTimeForUpdationTime =
  57. DateTime.fromMicrosecondsSinceEpoch(file.updationTime);
  58. infoColor =
  59. Theme.of(context).colorScheme.onSurface.withOpacity(0.85); //remove
  60. if (_isImage && _exif != null) {
  61. // items.add(_getExifWidgets(_exif));
  62. _generateExifForDetails(_exif);
  63. }
  64. final bool showExifListTile = _exifData["focalLength"] != null ||
  65. _exifData["fNumber"] != null ||
  66. _exifData["takenOnDevice"] != null ||
  67. _exifData["exposureTime"] != null ||
  68. _exifData["ISO"] != null;
  69. var listTiles = <Widget>[
  70. ListTile(
  71. leading: const Padding(
  72. padding: EdgeInsets.only(top: 8, left: 6),
  73. child: Icon(Icons.calendar_today_rounded),
  74. ),
  75. title: Text(
  76. getFullDate(
  77. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  78. ),
  79. ),
  80. subtitle: Text(
  81. getTimeIn12hrFormat(dateTime) + " " + dateTime.timeZoneName,
  82. style: Theme.of(context)
  83. .textTheme
  84. .bodyText2
  85. .copyWith(color: Colors.black.withOpacity(0.5)),
  86. ),
  87. trailing: (widget.file.ownerID == null ||
  88. widget.file.ownerID ==
  89. Configuration.instance.getUserID()) &&
  90. widget.file.uploadedFileID != null
  91. ? IconButton(
  92. onPressed: () {
  93. PopupMenuItem(
  94. value: 2,
  95. child: Row(
  96. children: [
  97. Icon(
  98. Platform.isAndroid
  99. ? Icons.access_time_rounded
  100. : CupertinoIcons.time,
  101. color: Theme.of(context).iconTheme.color,
  102. ),
  103. const Padding(
  104. padding: EdgeInsets.all(8),
  105. ),
  106. const Text("Edit time"),
  107. ],
  108. ),
  109. );
  110. },
  111. icon: const Icon(Icons.edit),
  112. )
  113. : const SizedBox.shrink(),
  114. ),
  115. const DividerWithPadding(),
  116. ListTile(
  117. leading: const Padding(
  118. padding: EdgeInsets.only(top: 8, left: 6),
  119. child: Icon(
  120. Icons.image,
  121. ),
  122. ),
  123. title: Text(
  124. file.getDisplayName(),
  125. ),
  126. subtitle: Row(
  127. children: [
  128. _getFileSize(),
  129. ],
  130. ),
  131. trailing: file.uploadedFileID == null ||
  132. file.ownerID != Configuration.instance.getUserID()
  133. ? const SizedBox.shrink()
  134. : IconButton(
  135. onPressed: () async {
  136. await editFilename(context, file);
  137. setState(() {});
  138. },
  139. icon: const Icon(Icons.edit),
  140. ),
  141. ),
  142. const DividerWithPadding(),
  143. ListTile(
  144. leading: const Padding(
  145. padding: EdgeInsets.only(left: 6),
  146. child: Icon(Icons.folder_outlined),
  147. ),
  148. title: Text(
  149. file.deviceFolder ??
  150. CollectionsService.instance
  151. .getCollectionByID(file.collectionID)
  152. .name,
  153. ),
  154. ),
  155. const DividerWithPadding(),
  156. showExifListTile
  157. ? ListTile(
  158. leading: const Padding(
  159. padding: EdgeInsets.only(left: 6),
  160. child: Icon(Icons.camera_rounded),
  161. ),
  162. title: Text(_exifData["takenOnDevice"] ?? "--"),
  163. subtitle: Row(
  164. children: [
  165. _exifData["fNumber"] != null
  166. ? Padding(
  167. padding: const EdgeInsets.only(right: 10),
  168. child: Text('ƒ/' + _exifData["fNumber"].toString()),
  169. )
  170. : const SizedBox.shrink(),
  171. _exifData["exposureTime"] != null
  172. ? Padding(
  173. padding: const EdgeInsets.only(right: 10),
  174. child: Text(_exifData["exposureTime"]),
  175. )
  176. : const SizedBox.shrink(),
  177. _exifData["focalLength"] != null
  178. ? Padding(
  179. padding: const EdgeInsets.only(right: 10),
  180. child:
  181. Text(_exifData["focalLength"].toString() + "mm"),
  182. )
  183. : const SizedBox.shrink(),
  184. _exifData["ISO"] != null
  185. ? Padding(
  186. padding: const EdgeInsets.only(right: 10),
  187. child: Text("ISO" + _exifData["ISO"].toString()),
  188. )
  189. : const SizedBox.shrink(),
  190. ],
  191. ),
  192. )
  193. : const SizedBox.shrink(),
  194. showExifListTile ? const DividerWithPadding() : const SizedBox.shrink(),
  195. (file.uploadedFileID != null && file.updationTime != null)
  196. ? ListTile(
  197. leading: const Padding(
  198. padding: EdgeInsets.only(top: 8, left: 6),
  199. child: Icon(Icons.cloud_upload_outlined),
  200. ),
  201. title: Text(
  202. getFullDate(
  203. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  204. ),
  205. ),
  206. subtitle: Text(
  207. getTimeIn12hrFormat(dateTimeForUpdationTime) +
  208. " " +
  209. dateTimeForUpdationTime.timeZoneName,
  210. style: Theme.of(context)
  211. .textTheme
  212. .bodyText2
  213. .copyWith(color: Colors.black.withOpacity(0.5)),
  214. ),
  215. )
  216. : const SizedBox.shrink(),
  217. ];
  218. var items = <Widget>[
  219. Row(
  220. children: [
  221. Icon(Icons.calendar_today_outlined, color: infoColor),
  222. const SizedBox(height: 8),
  223. Text(
  224. getFormattedTime(
  225. DateTime.fromMicrosecondsSinceEpoch(file.creationTime),
  226. ),
  227. style: TextStyle(color: infoColor),
  228. ),
  229. ],
  230. ),
  231. const SizedBox(height: 12),
  232. Row(
  233. children: [
  234. Icon(Icons.folder_outlined, color: infoColor),
  235. const Padding(padding: EdgeInsets.all(4)),
  236. Text(
  237. file.deviceFolder ??
  238. CollectionsService.instance
  239. .getCollectionByID(file.collectionID)
  240. .name,
  241. style: TextStyle(color: infoColor),
  242. ),
  243. ],
  244. ),
  245. const SizedBox(height: 12),
  246. ];
  247. items.addAll(
  248. [
  249. Row(
  250. children: [
  251. Icon(Icons.sd_storage_outlined, color: infoColor),
  252. const Padding(padding: EdgeInsets.all(4)),
  253. _getFileSize(),
  254. ],
  255. ),
  256. const SizedBox(height: 12),
  257. ],
  258. );
  259. if (file.localID != null && !_isImage) {
  260. items.addAll(
  261. [
  262. Row(
  263. children: [
  264. Icon(Icons.timer_outlined, color: infoColor),
  265. const Padding(padding: EdgeInsets.all(4)),
  266. FutureBuilder(
  267. future: file.getAsset(),
  268. builder: (context, snapshot) {
  269. if (snapshot.hasData) {
  270. return Text(
  271. snapshot.data.videoDuration.toString().split(".")[0],
  272. style: TextStyle(color: infoColor),
  273. );
  274. } else {
  275. return Center(
  276. child: SizedBox.fromSize(
  277. size: const Size.square(24),
  278. child: const CupertinoActivityIndicator(
  279. radius: 8,
  280. ),
  281. ),
  282. );
  283. }
  284. },
  285. ),
  286. ],
  287. ),
  288. const SizedBox(height: 12),
  289. ],
  290. );
  291. }
  292. if (_isImage && _exif != null) {
  293. //remove
  294. // items.add(_getExifWidgets(_exif));
  295. _generateExifForDetails(_exif);
  296. }
  297. if (file.uploadedFileID != null && file.updationTime != null) {
  298. items.addAll(
  299. [
  300. Row(
  301. children: [
  302. Icon(Icons.cloud_upload_outlined, color: infoColor),
  303. const Padding(padding: EdgeInsets.all(4)),
  304. Text(
  305. getFormattedTime(
  306. DateTime.fromMicrosecondsSinceEpoch(file.updationTime),
  307. ),
  308. style: TextStyle(color: infoColor),
  309. ),
  310. ],
  311. ),
  312. ],
  313. );
  314. }
  315. items.add(
  316. const SizedBox(height: 12),
  317. );
  318. items.add(
  319. Row(
  320. mainAxisAlignment:
  321. _isImage ? MainAxisAlignment.spaceBetween : MainAxisAlignment.end,
  322. children: _getActions(),
  323. ),
  324. );
  325. Widget titleContent;
  326. if (file.uploadedFileID == null ||
  327. file.ownerID != Configuration.instance.getUserID()) {
  328. titleContent = Text(file.getDisplayName());
  329. } else {
  330. titleContent = InkWell(
  331. child: Row(
  332. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  333. children: [
  334. Flexible(
  335. child: Text(
  336. file.getDisplayName(),
  337. style: Theme.of(context).textTheme.headline5,
  338. ),
  339. ),
  340. const SizedBox(width: 16),
  341. Icon(Icons.edit, color: infoColor),
  342. ],
  343. ),
  344. onTap: () async {
  345. await editFilename(context, file);
  346. setState(() {});
  347. },
  348. );
  349. }
  350. // return AlertDialog(
  351. // title: titleContent,
  352. // content: SingleChildScrollView(
  353. // child: ListBody(
  354. // children: items,
  355. // ),
  356. // ),
  357. // );
  358. return Column(
  359. mainAxisSize: MainAxisSize.min,
  360. children: [
  361. Padding(
  362. padding: const EdgeInsets.all(10),
  363. child: Row(
  364. crossAxisAlignment: CrossAxisAlignment.center,
  365. children: [
  366. IconButton(
  367. onPressed: () {
  368. Navigator.pop(context);
  369. },
  370. icon: const Icon(
  371. Icons.close,
  372. ),
  373. ),
  374. const SizedBox(width: 6),
  375. Padding(
  376. padding: const EdgeInsets.only(bottom: 2),
  377. child: Text(
  378. "Details",
  379. style: Theme.of(context).textTheme.bodyText1,
  380. ),
  381. ),
  382. ],
  383. ),
  384. ),
  385. ...listTiles
  386. ],
  387. );
  388. }
  389. List<Widget> _getActions() {
  390. final List<Widget> actions = [];
  391. if (_isImage) {
  392. if (_exif == null) {
  393. actions.add(
  394. TextButton(
  395. child: Row(
  396. mainAxisAlignment: MainAxisAlignment.spaceAround,
  397. children: [
  398. Center(
  399. child: SizedBox.fromSize(
  400. size: const Size.square(24),
  401. child: const CupertinoActivityIndicator(
  402. radius: 8,
  403. ),
  404. ),
  405. ),
  406. const Padding(padding: EdgeInsets.all(4)),
  407. Text(
  408. "EXIF",
  409. style: TextStyle(color: infoColor),
  410. ),
  411. ],
  412. ),
  413. onPressed: () {
  414. showDialog(
  415. context: context,
  416. builder: (BuildContext context) {
  417. return ExifInfoDialog(widget.file);
  418. },
  419. barrierColor: Colors.black87,
  420. );
  421. },
  422. ),
  423. );
  424. } else if (_exif.isNotEmpty) {
  425. actions.add(
  426. TextButton(
  427. child: Row(
  428. mainAxisAlignment: MainAxisAlignment.spaceAround,
  429. children: [
  430. Icon(Icons.feed_outlined, color: infoColor),
  431. const Padding(padding: EdgeInsets.all(4)),
  432. Text(
  433. "View raw EXIF",
  434. style: TextStyle(color: infoColor),
  435. ),
  436. ],
  437. ),
  438. onPressed: () {
  439. showDialog(
  440. context: context,
  441. builder: (BuildContext context) {
  442. return ExifInfoDialog(widget.file);
  443. },
  444. barrierColor: Colors.black87,
  445. );
  446. },
  447. ),
  448. );
  449. } else {
  450. actions.add(
  451. TextButton(
  452. child: Row(
  453. mainAxisAlignment: MainAxisAlignment.spaceAround,
  454. children: [
  455. Icon(
  456. Icons.feed_outlined,
  457. color: Theme.of(context)
  458. .colorScheme
  459. .defaultTextColor
  460. .withOpacity(0.5),
  461. ),
  462. const Padding(padding: EdgeInsets.all(4)),
  463. Text(
  464. "No exif",
  465. style: TextStyle(
  466. color: Theme.of(context)
  467. .colorScheme
  468. .defaultTextColor
  469. .withOpacity(0.5),
  470. ),
  471. ),
  472. ],
  473. ),
  474. onPressed: () {
  475. showShortToast(context, "This image has no exif data");
  476. },
  477. ),
  478. );
  479. }
  480. }
  481. actions.add(
  482. TextButton(
  483. child: Text(
  484. "Close",
  485. style: TextStyle(
  486. color: infoColor,
  487. ),
  488. ),
  489. onPressed: () {
  490. Navigator.of(context, rootNavigator: true).pop("dialog");
  491. },
  492. ),
  493. );
  494. return actions;
  495. }
  496. _generateExifForDetails(Map<String, IfdTag> exif) {
  497. if (exif["EXIF FocalLength"] != null) {
  498. _exifData["focalLength"] =
  499. (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  500. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  501. .denominator;
  502. }
  503. if (exif["EXIF FNumber"] != null) {
  504. _exifData["fNumber"] =
  505. (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  506. (exif["EXIF FNumber"].values.toList()[0] as Ratio).denominator;
  507. }
  508. if (exif["EXIF ExifImageWidth"] != null &&
  509. exif["EXIF ExifImageLength"] != null) {
  510. _exifData["resolution"] = exif["EXIF ExifImageWidth"].toString() +
  511. " x " +
  512. exif["EXIF ExifImageLength"].toString();
  513. _exifData['megaPixels'] = ((exif["Image ImageWidth"].values.firstAsInt() *
  514. exif["Image ImageLength"].values.firstAsInt()) /
  515. 1000000)
  516. .toStringAsFixed(1);
  517. } else if (exif["Image ImageWidth"] != null &&
  518. exif["Image ImageLength"] != null) {
  519. _exifData["resolution"] = exif["Image ImageWidth"].toString() +
  520. " x " +
  521. exif["Image ImageLength"].toString();
  522. }
  523. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  524. _exifData["takenOnDevice"] =
  525. exif["Image Make"].toString() + " " + exif["Image Model"].toString();
  526. }
  527. if (exif["EXIF ExposureTime"] != null) {
  528. _exifData["exposureTime"] = exif["EXIF ExposureTime"].toString();
  529. }
  530. if (exif["EXIF ISOSpeedRatings"] != null) {
  531. _exifData['ISO'] = exif["EXIF ISOSpeedRatings"].toString();
  532. }
  533. }
  534. Widget _getExifWidgets(Map<String, IfdTag> exif) {
  535. final focalLength = exif["EXIF FocalLength"] != null
  536. ? (exif["EXIF FocalLength"].values.toList()[0] as Ratio).numerator /
  537. (exif["EXIF FocalLength"].values.toList()[0] as Ratio)
  538. .denominator //to remove
  539. : null;
  540. final fNumber = exif["EXIF FNumber"] != null
  541. ? (exif["EXIF FNumber"].values.toList()[0] as Ratio).numerator /
  542. (exif["EXIF FNumber"].values.toList()[0] as Ratio)
  543. .denominator //to remove
  544. : null;
  545. final List<Widget> children = [];
  546. if (exif["EXIF ExifImageWidth"] != null &&
  547. exif["EXIF ExifImageLength"] != null) {
  548. children.addAll([
  549. Row(
  550. children: [
  551. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  552. const Padding(padding: EdgeInsets.all(4)),
  553. Text(
  554. exif["EXIF ExifImageWidth"].toString() +
  555. " x " +
  556. exif["EXIF ExifImageLength"].toString(),
  557. style: TextStyle(color: infoColor),
  558. ),
  559. ],
  560. ),
  561. const Padding(padding: EdgeInsets.all(6)),
  562. ]);
  563. } else if (exif["Image ImageWidth"] != null &&
  564. exif["Image ImageLength"] != null) {
  565. children.addAll([
  566. Row(
  567. children: [
  568. Icon(Icons.photo_size_select_actual_outlined, color: infoColor),
  569. const Padding(padding: EdgeInsets.all(4)),
  570. Text(
  571. exif["Image ImageWidth"].toString() +
  572. " x " +
  573. exif["Image ImageLength"].toString(),
  574. style: TextStyle(color: infoColor),
  575. ),
  576. ],
  577. ),
  578. const Padding(padding: EdgeInsets.all(6)),
  579. ]);
  580. }
  581. if (exif["Image Make"] != null && exif["Image Model"] != null) {
  582. children.addAll(
  583. [
  584. Row(
  585. children: [
  586. Icon(Icons.camera_outlined, color: infoColor),
  587. const Padding(padding: EdgeInsets.all(4)),
  588. Flexible(
  589. child: Text(
  590. exif["Image Make"].toString() +
  591. " " +
  592. exif["Image Model"].toString(),
  593. style: TextStyle(color: infoColor),
  594. overflow: TextOverflow.clip,
  595. ),
  596. ),
  597. ],
  598. ),
  599. const Padding(padding: EdgeInsets.all(6)),
  600. ],
  601. );
  602. }
  603. if (fNumber != null) {
  604. children.addAll([
  605. Row(
  606. children: [
  607. Icon(CupertinoIcons.f_cursive, color: infoColor),
  608. const Padding(padding: EdgeInsets.all(4)),
  609. Text(
  610. fNumber.toString(),
  611. style: TextStyle(color: infoColor),
  612. ),
  613. ],
  614. ),
  615. const Padding(padding: EdgeInsets.all(6)),
  616. ]);
  617. }
  618. if (focalLength != null) {
  619. children.addAll([
  620. Row(
  621. children: [
  622. Icon(Icons.center_focus_strong_outlined, color: infoColor),
  623. const Padding(padding: EdgeInsets.all(4)),
  624. Text(
  625. focalLength.toString() + " mm",
  626. style: TextStyle(color: infoColor),
  627. ),
  628. ],
  629. ),
  630. const Padding(padding: EdgeInsets.all(6)),
  631. ]);
  632. }
  633. if (exif["EXIF ExposureTime"] != null) {
  634. children.addAll([
  635. Row(
  636. children: [
  637. Icon(Icons.shutter_speed, color: infoColor),
  638. const Padding(padding: EdgeInsets.all(4)),
  639. Text(
  640. exif["EXIF ExposureTime"].toString(),
  641. style: TextStyle(color: infoColor),
  642. ),
  643. ],
  644. ),
  645. const Padding(padding: EdgeInsets.all(6)),
  646. ]);
  647. }
  648. return Column(
  649. children: children,
  650. );
  651. }
  652. Widget _getFileSize() {
  653. return FutureBuilder(
  654. future: getFile(widget.file).then((f) => f.length()),
  655. builder: (context, snapshot) {
  656. if (snapshot.hasData) {
  657. return Text(
  658. (snapshot.data / (1024 * 1024)).toStringAsFixed(2) + " MB",
  659. );
  660. } else {
  661. return Center(
  662. child: SizedBox.fromSize(
  663. size: const Size.square(24),
  664. child: const CupertinoActivityIndicator(
  665. radius: 8,
  666. ),
  667. ),
  668. );
  669. }
  670. },
  671. );
  672. }
  673. }
  674. class DividerWithPadding extends StatelessWidget {
  675. const DividerWithPadding({Key key}) : super(key: key);
  676. @override
  677. Widget build(BuildContext context) {
  678. return const Padding(
  679. padding: EdgeInsets.fromLTRB(70, 0, 20, 0),
  680. child: Divider(
  681. thickness: 0.5,
  682. ),
  683. );
  684. }
  685. }