skip_log_in_screen.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import 'package:appflowy/core/frameless_window.dart';
  2. import 'package:appflowy/startup/entry_point.dart';
  3. import 'package:appflowy/startup/launch_configuration.dart';
  4. import 'package:appflowy/startup/startup.dart';
  5. import 'package:appflowy/user/application/auth/auth_service.dart';
  6. import 'package:appflowy/workspace/application/appearance.dart';
  7. import 'package:appflowy/workspace/presentation/settings/widgets/settings_language_view.dart';
  8. import 'package:appflowy_popover/appflowy_popover.dart';
  9. import 'package:dartz/dartz.dart' as dartz;
  10. import 'package:easy_localization/easy_localization.dart';
  11. import 'package:flowy_infra/image.dart';
  12. import 'package:flowy_infra/language.dart';
  13. import 'package:flowy_infra/size.dart';
  14. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  15. import 'package:appflowy_backend/dispatch/dispatch.dart';
  16. import 'package:appflowy_backend/log.dart';
  17. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  18. import 'package:appflowy_backend/protobuf/flowy-folder2/protobuf.dart';
  19. import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
  20. import 'package:flutter/material.dart';
  21. import 'package:flutter_bloc/flutter_bloc.dart';
  22. import 'package:google_fonts/google_fonts.dart';
  23. import 'package:url_launcher/url_launcher.dart';
  24. import '../../generated/locale_keys.g.dart';
  25. import 'folder/folder_widget.dart';
  26. import 'router.dart';
  27. import 'widgets/background.dart';
  28. class SkipLogInScreen extends StatefulWidget {
  29. final AuthRouter router;
  30. final AuthService authService;
  31. const SkipLogInScreen({
  32. Key? key,
  33. required this.router,
  34. required this.authService,
  35. }) : super(key: key);
  36. @override
  37. State<SkipLogInScreen> createState() => _SkipLogInScreenState();
  38. }
  39. class _SkipLogInScreenState extends State<SkipLogInScreen> {
  40. var _didCustomizeFolder = false;
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. appBar: const _SkipLoginMoveWindow(),
  45. body: Center(
  46. child: _renderBody(context),
  47. ),
  48. );
  49. }
  50. Widget _renderBody(BuildContext context) {
  51. final size = MediaQuery.of(context).size;
  52. return Column(
  53. mainAxisAlignment: MainAxisAlignment.center,
  54. crossAxisAlignment: CrossAxisAlignment.center,
  55. children: [
  56. const Spacer(),
  57. FlowyLogoTitle(
  58. title: LocaleKeys.welcomeText.tr(),
  59. logoSize: const Size.square(40),
  60. ),
  61. const VSpace(32),
  62. GoButton(
  63. onPressed: () {
  64. if (_didCustomizeFolder) {
  65. _relaunchAppAndAutoRegister();
  66. } else {
  67. _autoRegister(context);
  68. }
  69. },
  70. ),
  71. const VSpace(32),
  72. SizedBox(
  73. width: size.width * 0.5,
  74. child: FolderWidget(
  75. createFolderCallback: () async {
  76. _didCustomizeFolder = true;
  77. },
  78. ),
  79. ),
  80. const Spacer(),
  81. const SkipLoginPageFooter(),
  82. const VSpace(20),
  83. ],
  84. );
  85. }
  86. Future<void> _autoRegister(BuildContext context) async {
  87. final result = await widget.authService.signUpAsGuest();
  88. result.fold(
  89. (error) {
  90. Log.error(error);
  91. },
  92. (user) {
  93. FolderEventGetCurrentWorkspace().send().then((result) {
  94. _openCurrentWorkspace(context, user, result);
  95. });
  96. },
  97. );
  98. }
  99. Future<void> _relaunchAppAndAutoRegister() async {
  100. await FlowyRunner.run(
  101. FlowyApp(),
  102. integrationEnv(),
  103. config: const LaunchConfiguration(
  104. autoRegistrationSupported: true,
  105. ),
  106. );
  107. }
  108. void _openCurrentWorkspace(
  109. BuildContext context,
  110. UserProfilePB user,
  111. dartz.Either<WorkspaceSettingPB, FlowyError> workspacesOrError,
  112. ) {
  113. workspacesOrError.fold(
  114. (workspaceSetting) {
  115. widget.router
  116. .pushHomeScreenWithWorkSpace(context, user, workspaceSetting);
  117. },
  118. (error) {
  119. Log.error(error);
  120. },
  121. );
  122. }
  123. }
  124. class SkipLoginPageFooter extends StatelessWidget {
  125. const SkipLoginPageFooter({
  126. super.key,
  127. });
  128. @override
  129. Widget build(BuildContext context) {
  130. // The placeholderWidth should be greater than the longest width of the LanguageSelectorOnWelcomePage
  131. const double placeholderWidth = 180;
  132. return const Padding(
  133. padding: EdgeInsets.symmetric(horizontal: 16),
  134. child: Row(
  135. mainAxisAlignment: MainAxisAlignment.center,
  136. children: [
  137. HSpace(placeholderWidth),
  138. Expanded(child: SubscribeButtons()),
  139. SizedBox(
  140. width: placeholderWidth,
  141. height: 28,
  142. child: Row(
  143. children: [
  144. Spacer(),
  145. LanguageSelectorOnWelcomePage(),
  146. ],
  147. ),
  148. ),
  149. ],
  150. ),
  151. );
  152. }
  153. }
  154. class SubscribeButtons extends StatelessWidget {
  155. const SubscribeButtons({
  156. super.key,
  157. });
  158. @override
  159. Widget build(BuildContext context) {
  160. return Wrap(
  161. alignment: WrapAlignment.center,
  162. children: [
  163. Row(
  164. mainAxisAlignment: MainAxisAlignment.center,
  165. mainAxisSize: MainAxisSize.min,
  166. children: [
  167. FlowyText.regular(
  168. LocaleKeys.youCanAlso.tr(),
  169. fontSize: FontSizes.s12,
  170. ),
  171. FlowyTextButton(
  172. LocaleKeys.githubStarText.tr(),
  173. padding: const EdgeInsets.symmetric(horizontal: 4),
  174. fontWeight: FontWeight.w500,
  175. fontColor: Theme.of(context).colorScheme.primary,
  176. hoverColor: Colors.transparent,
  177. fillColor: Colors.transparent,
  178. onPressed: () => _launchURL(
  179. 'https://github.com/AppFlowy-IO/appflowy',
  180. ),
  181. ),
  182. ],
  183. ),
  184. Row(
  185. mainAxisAlignment: MainAxisAlignment.center,
  186. mainAxisSize: MainAxisSize.min,
  187. children: [
  188. FlowyText.regular(
  189. LocaleKeys.and.tr(),
  190. fontSize: FontSizes.s12,
  191. ),
  192. FlowyTextButton(
  193. LocaleKeys.subscribeNewsletterText.tr(),
  194. padding: const EdgeInsets.symmetric(horizontal: 4.0),
  195. fontWeight: FontWeight.w500,
  196. fontColor: Theme.of(context).colorScheme.primary,
  197. hoverColor: Colors.transparent,
  198. fillColor: Colors.transparent,
  199. onPressed: () => _launchURL('https://www.appflowy.io/blog'),
  200. ),
  201. ],
  202. ),
  203. ],
  204. );
  205. }
  206. Future<void> _launchURL(String url) async {
  207. final uri = Uri.parse(url);
  208. if (await canLaunchUrl(uri)) {
  209. await launchUrl(uri);
  210. } else {
  211. throw 'Could not launch $url';
  212. }
  213. }
  214. }
  215. class LanguageSelectorOnWelcomePage extends StatelessWidget {
  216. const LanguageSelectorOnWelcomePage({
  217. super.key,
  218. });
  219. @override
  220. Widget build(BuildContext context) {
  221. return AppFlowyPopover(
  222. offset: const Offset(0, -450),
  223. direction: PopoverDirection.bottomWithRightAligned,
  224. child: FlowyButton(
  225. useIntrinsicWidth: true,
  226. text: Row(
  227. mainAxisSize: MainAxisSize.min,
  228. mainAxisAlignment: MainAxisAlignment.end,
  229. children: [
  230. const FlowySvg(
  231. name: 'login/language',
  232. size: Size.square(20),
  233. ),
  234. const HSpace(4),
  235. Builder(
  236. builder: (context) {
  237. final currentLocale =
  238. context.watch<AppearanceSettingsCubit>().state.locale;
  239. return FlowyText(
  240. languageFromLocale(currentLocale),
  241. );
  242. },
  243. ),
  244. const FlowySvg(
  245. name: 'home/drop_down_hide',
  246. size: Size.square(20),
  247. ),
  248. ],
  249. ),
  250. ),
  251. popupBuilder: (BuildContext context) {
  252. final easyLocalization = EasyLocalization.of(context);
  253. if (easyLocalization == null) {
  254. return const SizedBox.shrink();
  255. }
  256. final allLocales = easyLocalization.supportedLocales;
  257. return LanguageItemsListView(
  258. allLocales: allLocales,
  259. );
  260. },
  261. );
  262. }
  263. }
  264. class GoButton extends StatelessWidget {
  265. final VoidCallback onPressed;
  266. const GoButton({
  267. super.key,
  268. required this.onPressed,
  269. });
  270. @override
  271. Widget build(BuildContext context) {
  272. return FlowyTextButton(
  273. LocaleKeys.letsGoButtonText.tr(),
  274. constraints: const BoxConstraints(
  275. maxWidth: 340,
  276. maxHeight: 48,
  277. ),
  278. radius: BorderRadius.circular(12),
  279. mainAxisAlignment: MainAxisAlignment.center,
  280. fontSize: FontSizes.s14,
  281. fontFamily: GoogleFonts.poppins(fontWeight: FontWeight.w500).fontFamily,
  282. padding: const EdgeInsets.symmetric(vertical: 14.0),
  283. onPressed: onPressed,
  284. fillColor: Theme.of(context).colorScheme.primary,
  285. fontColor: Theme.of(context).colorScheme.onPrimary,
  286. hoverColor: Theme.of(context).colorScheme.primaryContainer,
  287. );
  288. }
  289. }
  290. class _SkipLoginMoveWindow extends StatelessWidget
  291. implements PreferredSizeWidget {
  292. const _SkipLoginMoveWindow();
  293. @override
  294. Widget build(BuildContext context) {
  295. return const Row(
  296. children: [
  297. Expanded(
  298. child: MoveWindowDetector(),
  299. ),
  300. ],
  301. );
  302. }
  303. @override
  304. Size get preferredSize => const Size.fromHeight(55.0);
  305. }