skip_log_in_screen.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 VSpace(48),
  82. const SkipLoginPageFooter(),
  83. const VSpace(20),
  84. ],
  85. );
  86. }
  87. Future<void> _autoRegister(BuildContext context) async {
  88. final result = await widget.authService.signUpAsGuest();
  89. result.fold(
  90. (error) {
  91. Log.error(error);
  92. },
  93. (user) {
  94. FolderEventGetCurrentWorkspace().send().then((result) {
  95. _openCurrentWorkspace(context, user, result);
  96. });
  97. },
  98. );
  99. }
  100. Future<void> _relaunchAppAndAutoRegister() async {
  101. await FlowyRunner.run(
  102. FlowyApp(),
  103. integrationEnv(),
  104. config: const LaunchConfiguration(
  105. autoRegistrationSupported: true,
  106. ),
  107. );
  108. }
  109. void _openCurrentWorkspace(
  110. BuildContext context,
  111. UserProfilePB user,
  112. dartz.Either<WorkspaceSettingPB, FlowyError> workspacesOrError,
  113. ) {
  114. workspacesOrError.fold(
  115. (workspaceSetting) {
  116. widget.router
  117. .pushHomeScreenWithWorkSpace(context, user, workspaceSetting);
  118. },
  119. (error) {
  120. Log.error(error);
  121. },
  122. );
  123. }
  124. }
  125. class SkipLoginPageFooter extends StatelessWidget {
  126. const SkipLoginPageFooter({
  127. super.key,
  128. });
  129. @override
  130. Widget build(BuildContext context) {
  131. // The placeholderWidth should be greater than the longest width of the LanguageSelectorOnWelcomePage
  132. const double placeholderWidth = 180;
  133. return const Padding(
  134. padding: EdgeInsets.symmetric(horizontal: 16),
  135. child: Row(
  136. mainAxisAlignment: MainAxisAlignment.center,
  137. children: [
  138. HSpace(placeholderWidth),
  139. Expanded(child: SubscribeButtons()),
  140. SizedBox(
  141. width: placeholderWidth,
  142. height: 28,
  143. child: Row(
  144. children: [
  145. Spacer(),
  146. LanguageSelectorOnWelcomePage(),
  147. ],
  148. ),
  149. ),
  150. ],
  151. ),
  152. );
  153. }
  154. }
  155. class SubscribeButtons extends StatelessWidget {
  156. const SubscribeButtons({
  157. super.key,
  158. });
  159. @override
  160. Widget build(BuildContext context) {
  161. return Row(
  162. mainAxisAlignment: MainAxisAlignment.center,
  163. mainAxisSize: MainAxisSize.min,
  164. children: [
  165. FlowyText.regular(
  166. LocaleKeys.youCanAlso.tr(),
  167. fontSize: FontSizes.s12,
  168. ),
  169. FlowyTextButton(
  170. LocaleKeys.githubStarText.tr(),
  171. fontWeight: FontWeight.w500,
  172. fontColor: Theme.of(context).colorScheme.primary,
  173. hoverColor: Colors.transparent,
  174. fillColor: Colors.transparent,
  175. onPressed: () => _launchURL(
  176. 'https://github.com/AppFlowy-IO/appflowy',
  177. ),
  178. ),
  179. FlowyText.regular(
  180. LocaleKeys.and.tr(),
  181. fontSize: FontSizes.s12,
  182. ),
  183. FlowyTextButton(
  184. LocaleKeys.subscribeNewsletterText.tr(),
  185. overflow: TextOverflow.ellipsis,
  186. fontWeight: FontWeight.w500,
  187. fontColor: Theme.of(context).colorScheme.primary,
  188. hoverColor: Colors.transparent,
  189. fillColor: Colors.transparent,
  190. onPressed: () => _launchURL('https://www.appflowy.io/blog'),
  191. ),
  192. ],
  193. );
  194. }
  195. Future<void> _launchURL(String url) async {
  196. final uri = Uri.parse(url);
  197. if (await canLaunchUrl(uri)) {
  198. await launchUrl(uri);
  199. } else {
  200. throw 'Could not launch $url';
  201. }
  202. }
  203. }
  204. class LanguageSelectorOnWelcomePage extends StatelessWidget {
  205. const LanguageSelectorOnWelcomePage({
  206. super.key,
  207. });
  208. @override
  209. Widget build(BuildContext context) {
  210. return BlocBuilder<AppearanceSettingsCubit, AppearanceSettingsState>(
  211. builder: (context, state) {
  212. return AppFlowyPopover(
  213. offset: const Offset(0, -450),
  214. direction: PopoverDirection.bottomWithRightAligned,
  215. child: FlowyButton(
  216. useIntrinsicWidth: true,
  217. text: Row(
  218. mainAxisSize: MainAxisSize.min,
  219. mainAxisAlignment: MainAxisAlignment.end,
  220. children: [
  221. const FlowySvg(
  222. name: 'login/language',
  223. size: Size.square(20),
  224. ),
  225. const HSpace(4),
  226. FlowyText(
  227. languageFromLocale(state.locale),
  228. ),
  229. // const HSpace(4),
  230. const FlowySvg(
  231. name: 'home/drop_down_hide',
  232. size: Size.square(20),
  233. ),
  234. ],
  235. ),
  236. ),
  237. popupBuilder: (BuildContext context) {
  238. final easyLocalization = EasyLocalization.of(context);
  239. if (easyLocalization == null) {
  240. return const SizedBox.shrink();
  241. }
  242. final allLocales = easyLocalization.supportedLocales;
  243. return LanguageItemsListView(
  244. allLocales: allLocales,
  245. );
  246. },
  247. );
  248. },
  249. );
  250. }
  251. }
  252. class GoButton extends StatelessWidget {
  253. final VoidCallback onPressed;
  254. const GoButton({
  255. super.key,
  256. required this.onPressed,
  257. });
  258. @override
  259. Widget build(BuildContext context) {
  260. return FlowyTextButton(
  261. LocaleKeys.letsGoButtonText.tr(),
  262. constraints: const BoxConstraints(
  263. maxWidth: 340,
  264. maxHeight: 48,
  265. ),
  266. radius: BorderRadius.circular(12),
  267. mainAxisAlignment: MainAxisAlignment.center,
  268. fontSize: FontSizes.s14,
  269. fontFamily: GoogleFonts.poppins(fontWeight: FontWeight.w500).fontFamily,
  270. padding: const EdgeInsets.symmetric(vertical: 14.0),
  271. onPressed: onPressed,
  272. fillColor: Theme.of(context).colorScheme.primary,
  273. fontColor: Theme.of(context).colorScheme.onPrimary,
  274. hoverColor: Theme.of(context).colorScheme.primaryContainer,
  275. );
  276. }
  277. }
  278. class _SkipLoginMoveWindow extends StatelessWidget
  279. implements PreferredSizeWidget {
  280. const _SkipLoginMoveWindow();
  281. @override
  282. Widget build(BuildContext context) {
  283. return const Row(
  284. children: [
  285. Expanded(
  286. child: MoveWindowDetector(),
  287. ),
  288. ],
  289. );
  290. }
  291. @override
  292. Size get preferredSize => const Size.fromHeight(55.0);
  293. }