backup_folder_selection_widget.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/core/event_bus.dart';
  6. import 'package:photos/db/files_db.dart';
  7. import 'package:photos/events/backup_folders_updated_event.dart';
  8. import 'package:photos/models/file.dart';
  9. import 'package:photos/ui/loading_widget.dart';
  10. import 'package:photos/ui/thumbnail_widget.dart';
  11. class BackupFolderSelectionWidget extends StatefulWidget {
  12. final String buttonText;
  13. const BackupFolderSelectionWidget(this.buttonText, {Key key})
  14. : super(key: key);
  15. @override
  16. _BackupFolderSelectionWidgetState createState() =>
  17. _BackupFolderSelectionWidgetState();
  18. }
  19. class _BackupFolderSelectionWidgetState
  20. extends State<BackupFolderSelectionWidget> {
  21. Set<String> _backedupFolders = Set<String>();
  22. @override
  23. void initState() {
  24. _backedupFolders = Configuration.instance.getPathsToBackUp();
  25. if (_backedupFolders.length == 0) {
  26. if (Platform.isAndroid) {
  27. _backedupFolders.add("Camera");
  28. } else {
  29. _backedupFolders.add("Recents");
  30. }
  31. }
  32. super.initState();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return Container(
  37. child: Column(
  38. mainAxisAlignment: MainAxisAlignment.center,
  39. mainAxisSize: MainAxisSize.min,
  40. children: [
  41. Padding(
  42. padding: EdgeInsets.all(4),
  43. ),
  44. Text(
  45. "select folders to backup",
  46. style: TextStyle(fontSize: 20),
  47. ),
  48. Padding(
  49. padding: EdgeInsets.all(12),
  50. ),
  51. _getFolderList(),
  52. Padding(
  53. padding: EdgeInsets.all(8),
  54. ),
  55. Container(
  56. width: double.infinity,
  57. height: 64,
  58. child: RaisedButton(
  59. child: Text(
  60. widget.buttonText,
  61. style: TextStyle(
  62. fontWeight: FontWeight.bold,
  63. fontSize: 18,
  64. letterSpacing: 1.0,
  65. ),
  66. textAlign: TextAlign.center,
  67. ),
  68. onPressed: _backedupFolders.length == 0
  69. ? null
  70. : () {
  71. Configuration.instance.setPathsToBackUp(_backedupFolders);
  72. Bus.instance.fire(BackupFoldersUpdatedEvent());
  73. Navigator.pop(context);
  74. },
  75. shape: RoundedRectangleBorder(
  76. borderRadius: BorderRadius.circular(10.0),
  77. ),
  78. ),
  79. ),
  80. ],
  81. ),
  82. );
  83. }
  84. Widget _getFolderList() {
  85. return FutureBuilder<List<File>>(
  86. future: FilesDB.instance.getLatestLocalFiles(),
  87. builder: (context, snapshot) {
  88. Widget child;
  89. if (snapshot.hasData) {
  90. snapshot.data.sort((first, second) {
  91. return first.deviceFolder
  92. .toLowerCase()
  93. .compareTo(second.deviceFolder.toLowerCase());
  94. });
  95. final List<Widget> foldersWidget = [];
  96. for (final file in snapshot.data) {
  97. foldersWidget.add(Padding(
  98. padding: const EdgeInsets.all(4.0),
  99. child: CheckboxListTile(
  100. value: _backedupFolders.contains(file.deviceFolder),
  101. title: Row(
  102. children: [
  103. _getThumbnail(file),
  104. Padding(padding: EdgeInsets.all(8)),
  105. Flexible(
  106. child: Text(
  107. file.deviceFolder,
  108. style: TextStyle(fontSize: 16, height: 1.5),
  109. overflow: TextOverflow.clip,
  110. ),
  111. ),
  112. ],
  113. ),
  114. onChanged: (value) async {
  115. if (value) {
  116. _backedupFolders.add(file.deviceFolder);
  117. } else {
  118. _backedupFolders.remove(file.deviceFolder);
  119. }
  120. setState(() {});
  121. },
  122. ),
  123. ));
  124. }
  125. final scrollController = ScrollController();
  126. child = Scrollbar(
  127. isAlwaysShown: true,
  128. controller: scrollController,
  129. child: SingleChildScrollView(
  130. controller: scrollController,
  131. child: Column(
  132. crossAxisAlignment: CrossAxisAlignment.stretch,
  133. children: foldersWidget,
  134. ),
  135. ),
  136. );
  137. } else {
  138. child = loadWidget;
  139. }
  140. return Container(
  141. height: 400,
  142. width: 300,
  143. child: child,
  144. );
  145. },
  146. );
  147. }
  148. Widget _getThumbnail(File file) {
  149. return ClipRRect(
  150. borderRadius: BorderRadius.circular(4.0),
  151. child: Container(
  152. child: ThumbnailWidget(
  153. file,
  154. shouldShowSyncStatus: false,
  155. key: Key("backup_selection_widget" + file.tag()),
  156. ),
  157. height: 50,
  158. width: 50,
  159. ),
  160. );
  161. }
  162. }