memories_widget.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_swiper/flutter_swiper.dart';
  4. import 'package:photos/memories_service.dart';
  5. import 'package:photos/models/file_type.dart';
  6. import 'package:photos/models/memory.dart';
  7. import 'package:photos/ui/thumbnail_widget.dart';
  8. import 'package:photos/ui/video_widget.dart';
  9. import 'package:photos/ui/zoomable_image.dart';
  10. import 'package:photos/utils/date_time_util.dart';
  11. import 'package:photos/utils/share_util.dart';
  12. class MemoriesWidget extends StatefulWidget {
  13. @override
  14. _MemoriesWidgetState createState() => _MemoriesWidgetState();
  15. }
  16. class _MemoriesWidgetState extends State<MemoriesWidget> {
  17. @override
  18. void initState() {
  19. MemoriesService.instance.addListener(() {
  20. setState(() {});
  21. });
  22. super.initState();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. return FutureBuilder<List<Memory>>(
  27. future: MemoriesService.instance.getMemories(),
  28. builder: (context, snapshot) {
  29. if (snapshot.hasError ||
  30. !snapshot.hasData ||
  31. snapshot.data.length == 0) {
  32. return Container();
  33. } else {
  34. return _buildMemories(snapshot.data);
  35. }
  36. },
  37. );
  38. }
  39. Widget _buildMemories(List<Memory> memories) {
  40. final collatedMemories = _collateMemories(memories);
  41. final memoryWidgets = List<Widget>();
  42. for (final memories in collatedMemories) {
  43. memoryWidgets.add(MemoryWidget(memories: memories));
  44. }
  45. return Row(children: memoryWidgets);
  46. }
  47. List<List<Memory>> _collateMemories(List<Memory> memories) {
  48. final yearlyMemories = List<Memory>();
  49. final collatedMemories = List<List<Memory>>();
  50. for (int index = 0; index < memories.length; index++) {
  51. if (index > 0 &&
  52. !_areMemoriesFromSameYear(memories[index - 1], memories[index])) {
  53. final collatedYearlyMemories = List<Memory>();
  54. collatedYearlyMemories.addAll(yearlyMemories);
  55. collatedMemories.add(collatedYearlyMemories);
  56. yearlyMemories.clear();
  57. }
  58. yearlyMemories.add(memories[index]);
  59. }
  60. if (yearlyMemories.isNotEmpty) {
  61. collatedMemories.add(yearlyMemories);
  62. }
  63. return collatedMemories;
  64. }
  65. bool _areMemoriesFromSameYear(Memory first, Memory second) {
  66. var firstDate =
  67. DateTime.fromMicrosecondsSinceEpoch(first.file.creationTime);
  68. var secondDate =
  69. DateTime.fromMicrosecondsSinceEpoch(second.file.creationTime);
  70. return firstDate.year == secondDate.year;
  71. }
  72. }
  73. class MemoryWidget extends StatelessWidget {
  74. const MemoryWidget({
  75. Key key,
  76. @required this.memories,
  77. }) : super(key: key);
  78. final List<Memory> memories;
  79. @override
  80. Widget build(BuildContext context) {
  81. final index = _getUnseenMemoryIndex();
  82. final title = _getTitle(memories[index]);
  83. return GestureDetector(
  84. onTap: () {
  85. Navigator.of(context).push(
  86. MaterialPageRoute(
  87. builder: (BuildContext context) {
  88. return FullScreenMemory(title, memories, index);
  89. },
  90. ),
  91. );
  92. },
  93. child: Container(
  94. width: 100,
  95. height: 120,
  96. child: Padding(
  97. padding: const EdgeInsets.all(8.0),
  98. child: Column(
  99. children: [
  100. _buildMemoryItem(index),
  101. Padding(padding: EdgeInsets.all(2)),
  102. Hero(
  103. tag: title,
  104. child: Material(
  105. type: MaterialType.transparency,
  106. child: Text(title),
  107. ),
  108. ),
  109. ],
  110. ),
  111. ),
  112. ),
  113. );
  114. }
  115. Container _buildMemoryItem(int index) {
  116. final isSeen = memories[index].isSeen();
  117. return Container(
  118. decoration: BoxDecoration(
  119. border: isSeen
  120. ? Border()
  121. : Border.all(
  122. color: Colors.amber,
  123. width: isSeen ? 0 : 2,
  124. ),
  125. borderRadius: BorderRadius.circular(40),
  126. ),
  127. child: ClipOval(
  128. child: Container(
  129. width: isSeen ? 76 : 72,
  130. height: isSeen ? 76 : 72,
  131. child: Hero(
  132. tag: "memories" + memories[index].file.tag(),
  133. child: ThumbnailWidget(memories[index].file),
  134. ),
  135. ),
  136. ),
  137. );
  138. }
  139. int _getUnseenMemoryIndex() {
  140. for (var index = 0; index < memories.length; index++) {
  141. if (!memories[index].isSeen()) {
  142. return index;
  143. }
  144. }
  145. return 0;
  146. }
  147. String _getTitle(Memory memory) {
  148. final present = DateTime.now();
  149. final then = DateTime.fromMicrosecondsSinceEpoch(memory.file.creationTime);
  150. final diffInYears = present.year - then.year;
  151. if (diffInYears == 1) {
  152. return "1 year ago";
  153. } else {
  154. return diffInYears.toString() + " years ago";
  155. }
  156. }
  157. }
  158. class FullScreenMemory extends StatefulWidget {
  159. final String title;
  160. final List<Memory> memories;
  161. final int index;
  162. FullScreenMemory(this.title, this.memories, this.index, {Key key})
  163. : super(key: key);
  164. @override
  165. _FullScreenMemoryState createState() => _FullScreenMemoryState();
  166. }
  167. class _FullScreenMemoryState extends State<FullScreenMemory> {
  168. int _index = 0;
  169. double _opacity = 1;
  170. @override
  171. void initState() {
  172. super.initState();
  173. _index = widget.index;
  174. Future.delayed(Duration(seconds: 3), () {
  175. if (mounted) {
  176. setState(() {
  177. _opacity = 0;
  178. });
  179. }
  180. });
  181. MemoriesService.instance.markMemoryAsSeen(widget.memories[_index]);
  182. }
  183. @override
  184. Widget build(BuildContext context) {
  185. return Scaffold(
  186. body: Container(
  187. color: Colors.black,
  188. child: Stack(children: [
  189. _buildSwiper(),
  190. Hero(
  191. tag: widget.title,
  192. child: Container(
  193. alignment: Alignment.bottomCenter,
  194. padding: EdgeInsets.fromLTRB(0, 0, 0, 160),
  195. child: AnimatedOpacity(
  196. opacity: _opacity,
  197. duration: Duration(milliseconds: 500),
  198. child: Material(
  199. type: MaterialType.transparency,
  200. child: Text(
  201. widget.title,
  202. style: TextStyle(
  203. fontSize: 32,
  204. fontWeight: FontWeight.bold,
  205. decoration: TextDecoration.none),
  206. ),
  207. ),
  208. ),
  209. ),
  210. ),
  211. ]),
  212. ),
  213. );
  214. }
  215. Swiper _buildSwiper() {
  216. return Swiper(
  217. itemBuilder: (BuildContext context, int index) {
  218. final file = widget.memories[index].file;
  219. return Stack(children: [
  220. file.fileType == FileType.image
  221. ? ZoomableImage(
  222. file,
  223. tagPrefix: "memories",
  224. )
  225. : VideoWidget(
  226. file,
  227. tagPrefix: "memories",
  228. autoPlay: true,
  229. ),
  230. Padding(
  231. padding: const EdgeInsets.fromLTRB(16, 56, 16, 16),
  232. child: Row(
  233. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  234. children: [
  235. Text(
  236. getFormattedDate(
  237. DateTime.fromMicrosecondsSinceEpoch(file.creationTime)),
  238. style: TextStyle(
  239. fontSize: 18,
  240. fontWeight: FontWeight.bold,
  241. ),
  242. ),
  243. IconButton(
  244. icon: Icon(Icons.share),
  245. onPressed: () {
  246. share(context, file);
  247. },
  248. ),
  249. ],
  250. ),
  251. )
  252. ]);
  253. },
  254. index: _index,
  255. itemCount: widget.memories.length,
  256. pagination: SwiperPagination(
  257. alignment: Alignment.bottomCenter,
  258. margin: EdgeInsets.all(36),
  259. builder: FractionPaginationBuilder(
  260. activeColor: Colors.white,
  261. color: Colors.grey,
  262. )),
  263. loop: false,
  264. control: SwiperControl(),
  265. onIndexChanged: (index) async {
  266. await MemoriesService.instance.markMemoryAsSeen(widget.memories[index]);
  267. _index = index;
  268. },
  269. );
  270. }
  271. }