folder_widget.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import 'dart:io';
  2. import 'package:appflowy/util/file_picker/file_picker_service.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flowy_infra/image.dart';
  5. import 'package:flowy_infra/size.dart';
  6. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  7. import 'package:flowy_infra_ui/widget/buttons/secondary_button.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:fluttertoast/fluttertoast.dart';
  10. import 'package:google_fonts/google_fonts.dart';
  11. import '../../../generated/locale_keys.g.dart';
  12. import '../../../startup/startup.dart';
  13. import '../../../workspace/application/settings/settings_location_cubit.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<LocalFileStorage>().setPath(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<LocalFileStorage>().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: Row(
  155. children: [
  156. _buildTextButton(
  157. context,
  158. LocaleKeys.settings_files_create.tr(),
  159. () async {
  160. if (_path.isEmpty) {
  161. _showToast(
  162. LocaleKeys.settings_files_locationCannotBeEmpty.tr(),
  163. );
  164. } else {
  165. await getIt<LocalFileStorage>().setPath(_path);
  166. await widget.onPressedCreate();
  167. }
  168. },
  169. ),
  170. ],
  171. ),
  172. )
  173. ],
  174. );
  175. }
  176. String get _path {
  177. if (directory.isEmpty) return '';
  178. final String path;
  179. if (Platform.isMacOS) {
  180. path = directory.replaceAll('/Volumes/Macintosh HD', '');
  181. } else {
  182. path = directory;
  183. }
  184. return '$path/$_folderName';
  185. }
  186. void _showToast(String message) {
  187. _fToast.showToast(
  188. child: FlowyMessageToast(message: message),
  189. gravity: ToastGravity.CENTER,
  190. );
  191. }
  192. }
  193. Widget _buildTextButton(
  194. BuildContext context,
  195. String title,
  196. VoidCallback onPressed,
  197. ) {
  198. return SizedBox(
  199. width: 60,
  200. child: SecondaryTextButton(
  201. title,
  202. mode: SecondaryTextButtonMode.small,
  203. onPressed: onPressed,
  204. ),
  205. );
  206. }
  207. class _FolderCard extends StatelessWidget {
  208. const _FolderCard({
  209. required this.title,
  210. required this.subtitle,
  211. this.trailing,
  212. this.icon,
  213. });
  214. final String title;
  215. final String subtitle;
  216. final Widget? icon;
  217. final Widget? trailing;
  218. @override
  219. Widget build(BuildContext context) {
  220. return Card(
  221. child: Padding(
  222. padding: const EdgeInsets.symmetric(
  223. vertical: 16.0,
  224. horizontal: 16.0,
  225. ),
  226. child: Row(
  227. children: [
  228. if (icon != null)
  229. Padding(
  230. padding: const EdgeInsets.only(right: 20),
  231. child: icon!,
  232. ),
  233. Expanded(
  234. child: Column(
  235. crossAxisAlignment: CrossAxisAlignment.start,
  236. children: [
  237. FlowyText.regular(
  238. title,
  239. fontSize: FontSizes.s14,
  240. fontFamily: GoogleFonts.poppins(
  241. fontWeight: FontWeight.w500,
  242. ).fontFamily,
  243. ),
  244. const VSpace(4),
  245. FlowyText.regular(
  246. subtitle,
  247. overflow: TextOverflow.ellipsis,
  248. fontSize: FontSizes.s12,
  249. fontFamily: GoogleFonts.poppins(
  250. fontWeight: FontWeight.w300,
  251. ).fontFamily,
  252. ),
  253. ],
  254. ),
  255. ),
  256. if (trailing != null) ...[
  257. const HSpace(40),
  258. trailing!,
  259. ],
  260. ],
  261. ),
  262. ),
  263. );
  264. }
  265. }