remote_folder_gallery_widget.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:photos/db/photo_db.dart';
  4. import 'package:photos/favorite_photos_repository.dart';
  5. import 'package:photos/models/device_folder.dart';
  6. import 'package:photos/models/filters/favorite_items_filter.dart';
  7. import 'package:photos/models/filters/folder_name_filter.dart';
  8. import 'package:photos/ui/device_folder_page.dart';
  9. import 'package:photos/ui/loading_widget.dart';
  10. import 'package:photos/ui/thumbnail_widget.dart';
  11. import 'package:path/path.dart' as p;
  12. class RemoteFolderGalleryWidget extends StatefulWidget {
  13. const RemoteFolderGalleryWidget({Key key}) : super(key: key);
  14. @override
  15. _RemoteFolderGalleryWidgetState createState() =>
  16. _RemoteFolderGalleryWidgetState();
  17. }
  18. class _RemoteFolderGalleryWidgetState extends State<RemoteFolderGalleryWidget> {
  19. @override
  20. Widget build(BuildContext context) {
  21. return FutureBuilder(
  22. future: _getDeviceFolders(),
  23. builder: (context, snapshot) {
  24. if (snapshot.hasData) {
  25. return _getDeviceFolderGalleryWidget(snapshot.data);
  26. } else if (snapshot.hasError) {
  27. return Text(snapshot.error.toString());
  28. } else {
  29. return loadWidget;
  30. }
  31. },
  32. );
  33. }
  34. Widget _getDeviceFolderGalleryWidget(List<DeviceFolder> folders) {
  35. return Container(
  36. margin: EdgeInsets.only(top: 24),
  37. child: GridView.builder(
  38. shrinkWrap: true,
  39. padding: EdgeInsets.only(bottom: 12),
  40. physics: ScrollPhysics(), // to disable GridView's scrolling
  41. itemBuilder: (context, index) {
  42. return _buildFolder(context, folders[index]);
  43. },
  44. itemCount: folders.length,
  45. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  46. crossAxisCount: 2,
  47. ),
  48. ),
  49. );
  50. }
  51. Future<List<DeviceFolder>> _getDeviceFolders() async {
  52. final paths = await PhotoDB.instance.getDistinctPaths();
  53. final folders = List<DeviceFolder>();
  54. for (final path in paths) {
  55. final photo = await PhotoDB.instance.getLatestPhotoInPath(path);
  56. final folderName = p.basename(path);
  57. folders
  58. .add(DeviceFolder(folderName, photo, FolderNameFilter(folderName)));
  59. }
  60. folders.sort((first, second) {
  61. return second.thumbnailPhoto.createTimestamp
  62. .compareTo(first.thumbnailPhoto.createTimestamp);
  63. });
  64. if (FavoritePhotosRepository.instance.hasFavorites()) {
  65. final photo = await PhotoDB.instance.getLatestPhotoAmongGeneratedIds(
  66. FavoritePhotosRepository.instance.getLiked().toList());
  67. folders.insert(
  68. 0, DeviceFolder("Favorites", photo, FavoriteItemsFilter()));
  69. }
  70. return folders;
  71. }
  72. Widget _buildFolder(BuildContext context, DeviceFolder folder) {
  73. return GestureDetector(
  74. child: Column(
  75. children: <Widget>[
  76. Container(
  77. child: ThumbnailWidget(folder.thumbnailPhoto),
  78. height: 150,
  79. width: 150,
  80. ),
  81. Padding(padding: EdgeInsets.all(2)),
  82. Expanded(
  83. child: Text(
  84. folder.name,
  85. style: TextStyle(
  86. fontSize: 16,
  87. ),
  88. ),
  89. ),
  90. ],
  91. ),
  92. onTap: () {
  93. final page = DeviceFolderPage(folder);
  94. Navigator.of(context).push(
  95. MaterialPageRoute(
  96. builder: (BuildContext context) {
  97. return page;
  98. },
  99. ),
  100. );
  101. },
  102. );
  103. }
  104. }