folder_widget.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import 'dart:io';
  2. import 'package:appflowy/workspace/application/settings/prelude.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flowy_infra/file_picker/file_picker_service.dart';
  5. import 'package:flowy_infra/image.dart';
  6. import 'package:flowy_infra/size.dart';
  7. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  8. import 'package:flowy_infra_ui/widget/buttons/secondary_button.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:fluttertoast/fluttertoast.dart';
  11. import 'package:google_fonts/google_fonts.dart';
  12. import '../../../generated/locale_keys.g.dart';
  13. import '../../../startup/startup.dart';
  14. import '../../../workspace/presentation/home/toast.dart';
  15. enum _FolderPage {
  16. options,
  17. create,
  18. open,
  19. }
  20. class FolderWidget extends StatefulWidget {
  21. const FolderWidget({
  22. super.key,
  23. required this.createFolderCallback,
  24. });
  25. final Future<void> Function() createFolderCallback;
  26. @override
  27. State<FolderWidget> createState() => _FolderWidgetState();
  28. }
  29. class _FolderWidgetState extends State<FolderWidget> {
  30. var page = _FolderPage.options;
  31. @override
  32. Widget build(BuildContext context) {
  33. return _mapIndexToWidget(context);
  34. }
  35. Widget _mapIndexToWidget(BuildContext context) {
  36. switch (page) {
  37. case _FolderPage.options:
  38. return FolderOptionsWidget(
  39. onPressedOpen: () {
  40. _openFolder();
  41. },
  42. );
  43. case _FolderPage.create:
  44. return CreateFolderWidget(
  45. onPressedBack: () {
  46. setState(() => page = _FolderPage.options);
  47. },
  48. onPressedCreate: widget.createFolderCallback,
  49. );
  50. case _FolderPage.open:
  51. return Container();
  52. }
  53. }
  54. Future<void> _openFolder() async {
  55. final path = await getIt<FilePickerService>().getDirectoryPath();
  56. if (path != null) {
  57. await getIt<ApplicationDataStorage>().setCustomPath(path);
  58. await widget.createFolderCallback();
  59. setState(() {});
  60. }
  61. }
  62. }
  63. class FolderOptionsWidget extends StatelessWidget {
  64. const FolderOptionsWidget({
  65. super.key,
  66. required this.onPressedOpen,
  67. });
  68. final VoidCallback onPressedOpen;
  69. @override
  70. Widget build(BuildContext context) {
  71. return FutureBuilder(
  72. future: getIt<ApplicationDataStorage>().getPath(),
  73. builder: (context, result) {
  74. final subtitle = result.hasData ? result.data! : '';
  75. return _FolderCard(
  76. icon: const FlowySvg(name: 'common/archive'),
  77. title: LocaleKeys.settings_files_defineWhereYourDataIsStored.tr(),
  78. subtitle: subtitle,
  79. trailing: _buildTextButton(
  80. context,
  81. LocaleKeys.settings_files_set.tr(),
  82. onPressedOpen,
  83. ),
  84. );
  85. },
  86. );
  87. }
  88. }
  89. class CreateFolderWidget extends StatefulWidget {
  90. const CreateFolderWidget({
  91. Key? key,
  92. required this.onPressedBack,
  93. required this.onPressedCreate,
  94. }) : super(key: key);
  95. final VoidCallback onPressedBack;
  96. final Future<void> Function() onPressedCreate;
  97. @override
  98. State<CreateFolderWidget> createState() => CreateFolderWidgetState();
  99. }
  100. @visibleForTesting
  101. class CreateFolderWidgetState extends State<CreateFolderWidget> {
  102. var _folderName = 'appflowy';
  103. @visibleForTesting
  104. var directory = '';
  105. final _fToast = FToast();
  106. @override
  107. void initState() {
  108. super.initState();
  109. _fToast.init(context);
  110. }
  111. @override
  112. Widget build(BuildContext context) {
  113. return Column(
  114. children: [
  115. Align(
  116. alignment: Alignment.centerLeft,
  117. child: TextButton.icon(
  118. onPressed: widget.onPressedBack,
  119. icon: const Icon(Icons.arrow_back_rounded),
  120. label: const Text('Back'),
  121. ),
  122. ),
  123. _FolderCard(
  124. title: LocaleKeys.settings_files_location.tr(),
  125. subtitle: LocaleKeys.settings_files_locationDesc.tr(),
  126. trailing: SizedBox(
  127. width: 120,
  128. child: FlowyTextField(
  129. hintText: LocaleKeys.settings_files_folderHintText.tr(),
  130. onChanged: (name) => _folderName = name,
  131. onSubmitted: (name) => setState(
  132. () => _folderName = name,
  133. ),
  134. ),
  135. ),
  136. ),
  137. _FolderCard(
  138. title: LocaleKeys.settings_files_folderPath.tr(),
  139. subtitle: _path,
  140. trailing: _buildTextButton(
  141. context,
  142. LocaleKeys.settings_files_browser.tr(),
  143. () async {
  144. final dir = await getIt<FilePickerService>().getDirectoryPath();
  145. if (dir != null) {
  146. setState(() => directory = dir);
  147. }
  148. },
  149. ),
  150. ),
  151. const VSpace(4.0),
  152. Padding(
  153. padding: const EdgeInsets.symmetric(horizontal: 4.0),
  154. child: _buildTextButton(
  155. context,
  156. LocaleKeys.settings_files_create.tr(),
  157. () async {
  158. if (_path.isEmpty) {
  159. _showToast(
  160. LocaleKeys.settings_files_locationCannotBeEmpty.tr(),
  161. );
  162. } else {
  163. await getIt<ApplicationDataStorage>().setCustomPath(_path);
  164. await widget.onPressedCreate();
  165. }
  166. },
  167. ),
  168. )
  169. ],
  170. );
  171. }
  172. String get _path {
  173. if (directory.isEmpty) return '';
  174. final String path;
  175. if (Platform.isMacOS) {
  176. path = directory.replaceAll('/Volumes/Macintosh HD', '');
  177. } else {
  178. path = directory;
  179. }
  180. return '$path/$_folderName';
  181. }
  182. void _showToast(String message) {
  183. _fToast.showToast(
  184. child: FlowyMessageToast(message: message),
  185. gravity: ToastGravity.CENTER,
  186. );
  187. }
  188. }
  189. Widget _buildTextButton(
  190. BuildContext context,
  191. String title,
  192. VoidCallback onPressed,
  193. ) {
  194. return SecondaryTextButton(
  195. title,
  196. mode: SecondaryTextButtonMode.small,
  197. onPressed: onPressed,
  198. );
  199. }
  200. class _FolderCard extends StatelessWidget {
  201. const _FolderCard({
  202. required this.title,
  203. required this.subtitle,
  204. this.trailing,
  205. this.icon,
  206. });
  207. final String title;
  208. final String subtitle;
  209. final Widget? icon;
  210. final Widget? trailing;
  211. @override
  212. Widget build(BuildContext context) {
  213. const cardSpacing = 16.0;
  214. return Card(
  215. child: Padding(
  216. padding: const EdgeInsets.symmetric(
  217. vertical: cardSpacing,
  218. horizontal: cardSpacing,
  219. ),
  220. child: Row(
  221. children: [
  222. if (icon != null)
  223. Padding(
  224. padding: const EdgeInsets.only(right: cardSpacing),
  225. child: icon!,
  226. ),
  227. Expanded(
  228. child: Column(
  229. crossAxisAlignment: CrossAxisAlignment.start,
  230. children: [
  231. FlowyText.regular(
  232. title,
  233. fontSize: FontSizes.s14,
  234. fontFamily: GoogleFonts.poppins(
  235. fontWeight: FontWeight.w500,
  236. ).fontFamily,
  237. ),
  238. const VSpace(4),
  239. FlowyText.regular(
  240. subtitle,
  241. overflow: TextOverflow.ellipsis,
  242. fontSize: FontSizes.s12,
  243. fontFamily: GoogleFonts.poppins(
  244. fontWeight: FontWeight.w300,
  245. ).fontFamily,
  246. ),
  247. ],
  248. ),
  249. ),
  250. if (trailing != null) ...[
  251. const HSpace(cardSpacing),
  252. trailing!,
  253. ],
  254. ],
  255. ),
  256. ),
  257. );
  258. }
  259. }