file_info_dialog.dart 13 KB

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