file_info_dialog.dart 13 KB

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