skip_log_in_screen.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. 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 Row(
  161. mainAxisAlignment: MainAxisAlignment.center,
  162. mainAxisSize: MainAxisSize.min,
  163. children: [
  164. FlowyText.regular(
  165. LocaleKeys.youCanAlso.tr(),
  166. fontSize: FontSizes.s12,
  167. ),
  168. FlowyTextButton(
  169. LocaleKeys.githubStarText.tr(),
  170. fontWeight: FontWeight.w500,
  171. fontColor: Theme.of(context).colorScheme.primary,
  172. hoverColor: Colors.transparent,
  173. fillColor: Colors.transparent,
  174. onPressed: () => _launchURL(
  175. 'https://github.com/AppFlowy-IO/appflowy',
  176. ),
  177. ),
  178. FlowyText.regular(
  179. LocaleKeys.and.tr(),
  180. fontSize: FontSizes.s12,
  181. ),
  182. FlowyTextButton(
  183. LocaleKeys.subscribeNewsletterText.tr(),
  184. overflow: TextOverflow.ellipsis,
  185. fontWeight: FontWeight.w500,
  186. fontColor: Theme.of(context).colorScheme.primary,
  187. hoverColor: Colors.transparent,
  188. fillColor: Colors.transparent,
  189. onPressed: () => _launchURL('https://www.appflowy.io/blog'),
  190. ),
  191. ],
  192. );
  193. }
  194. Future<void> _launchURL(String url) async {
  195. final uri = Uri.parse(url);
  196. if (await canLaunchUrl(uri)) {
  197. await launchUrl(uri);
  198. } else {
  199. throw 'Could not launch $url';
  200. }
  201. }
  202. }
  203. class LanguageSelectorOnWelcomePage extends StatelessWidget {
  204. const LanguageSelectorOnWelcomePage({
  205. super.key,
  206. });
  207. @override
  208. Widget build(BuildContext context) {
  209. return BlocBuilder<AppearanceSettingsCubit, AppearanceSettingsState>(
  210. builder: (context, state) {
  211. return AppFlowyPopover(
  212. offset: const Offset(0, -450),
  213. direction: PopoverDirection.bottomWithRightAligned,
  214. child: FlowyButton(
  215. useIntrinsicWidth: true,
  216. text: Row(
  217. mainAxisSize: MainAxisSize.min,
  218. mainAxisAlignment: MainAxisAlignment.end,
  219. children: [
  220. const FlowySvg(
  221. name: 'login/language',
  222. size: Size.square(20),
  223. ),
  224. const HSpace(4),
  225. FlowyText(
  226. languageFromLocale(state.locale),
  227. ),
  228. // const HSpace(4),
  229. const FlowySvg(
  230. name: 'home/drop_down_hide',
  231. size: Size.square(20),
  232. ),
  233. ],
  234. ),
  235. ),
  236. popupBuilder: (BuildContext context) {
  237. final easyLocalization = EasyLocalization.of(context);
  238. if (easyLocalization == null) {
  239. return const SizedBox.shrink();
  240. }
  241. final allLocales = easyLocalization.supportedLocales;
  242. return LanguageItemsListView(
  243. allLocales: allLocales,
  244. );
  245. },
  246. );
  247. },
  248. );
  249. }
  250. }
  251. class GoButton extends StatelessWidget {
  252. final VoidCallback onPressed;
  253. const GoButton({
  254. super.key,
  255. required this.onPressed,
  256. });
  257. @override
  258. Widget build(BuildContext context) {
  259. return FlowyTextButton(
  260. LocaleKeys.letsGoButtonText.tr(),
  261. constraints: const BoxConstraints(
  262. maxWidth: 340,
  263. maxHeight: 48,
  264. ),
  265. radius: BorderRadius.circular(12),
  266. mainAxisAlignment: MainAxisAlignment.center,
  267. fontSize: FontSizes.s14,
  268. fontFamily: GoogleFonts.poppins(fontWeight: FontWeight.w500).fontFamily,
  269. padding: const EdgeInsets.symmetric(vertical: 14.0),
  270. onPressed: onPressed,
  271. fillColor: Theme.of(context).colorScheme.primary,
  272. fontColor: Theme.of(context).colorScheme.onPrimary,
  273. hoverColor: Theme.of(context).colorScheme.primaryContainer,
  274. );
  275. }
  276. }
  277. class _SkipLoginMoveWindow extends StatelessWidget
  278. implements PreferredSizeWidget {
  279. const _SkipLoginMoveWindow();
  280. @override
  281. Widget build(BuildContext context) {
  282. return const Row(
  283. children: [
  284. Expanded(
  285. child: MoveWindowDetector(),
  286. ),
  287. ],
  288. );
  289. }
  290. @override
  291. Size get preferredSize => const Size.fromHeight(55.0);
  292. }