file_info_dialog.dart 13 KB

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