welcome_screen.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'package:appflowy/startup/startup.dart';
  2. import 'package:appflowy/workspace/application/workspace/welcome_bloc.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flowy_infra/theme_extension.dart';
  5. import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart';
  6. import 'package:flowy_infra_ui/style_widget/button.dart';
  7. import 'package:flowy_infra_ui/widget/error_page.dart';
  8. import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart';
  9. import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pb.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:appflowy/generated/locale_keys.g.dart';
  13. class WelcomeScreen extends StatelessWidget {
  14. final UserProfilePB userProfile;
  15. const WelcomeScreen({
  16. Key? key,
  17. required this.userProfile,
  18. }) : super(key: key);
  19. @override
  20. Widget build(BuildContext context) {
  21. return BlocProvider(
  22. create: (_) => getIt<WelcomeBloc>(param1: userProfile)
  23. ..add(const WelcomeEvent.initial()),
  24. child: BlocBuilder<WelcomeBloc, WelcomeState>(
  25. builder: (context, state) {
  26. return Scaffold(
  27. body: Padding(
  28. padding: const EdgeInsets.all(60.0),
  29. child: Column(
  30. children: [
  31. _renderBody(state),
  32. _renderCreateButton(context),
  33. ],
  34. ),
  35. ),
  36. );
  37. },
  38. ),
  39. );
  40. }
  41. Widget _renderBody(WelcomeState state) {
  42. final body = state.successOrFailure.fold(
  43. (_) => _renderList(state.workspaces),
  44. (error) => FlowyErrorPage(error.toString()),
  45. );
  46. return body;
  47. }
  48. Widget _renderCreateButton(BuildContext context) {
  49. return SizedBox(
  50. width: 200,
  51. height: 40,
  52. child: FlowyTextButton(
  53. LocaleKeys.workspace_create.tr(),
  54. fontSize: 14,
  55. hoverColor: AFThemeExtension.of(context).lightGreyHover,
  56. onPressed: () {
  57. context.read<WelcomeBloc>().add(
  58. WelcomeEvent.createWorkspace(
  59. LocaleKeys.workspace_hint.tr(),
  60. "",
  61. ),
  62. );
  63. },
  64. ),
  65. );
  66. }
  67. Widget _renderList(List<WorkspacePB> workspaces) {
  68. return Expanded(
  69. child: StyledListView(
  70. itemBuilder: (BuildContext context, int index) {
  71. final workspace = workspaces[index];
  72. return WorkspaceItem(
  73. workspace: workspace,
  74. onPressed: (workspace) => _handleOnPress(context, workspace),
  75. );
  76. },
  77. itemCount: workspaces.length,
  78. ),
  79. );
  80. }
  81. void _handleOnPress(BuildContext context, WorkspacePB workspace) {
  82. context.read<WelcomeBloc>().add(WelcomeEvent.openWorkspace(workspace));
  83. Navigator.of(context).pop(workspace.id);
  84. }
  85. }
  86. class WorkspaceItem extends StatelessWidget {
  87. final WorkspacePB workspace;
  88. final void Function(WorkspacePB workspace) onPressed;
  89. const WorkspaceItem({
  90. Key? key,
  91. required this.workspace,
  92. required this.onPressed,
  93. }) : super(key: key);
  94. @override
  95. Widget build(BuildContext context) {
  96. return SizedBox(
  97. height: 46,
  98. child: FlowyTextButton(
  99. workspace.name,
  100. hoverColor: AFThemeExtension.of(context).lightGreyHover,
  101. fontSize: 14,
  102. onPressed: () => onPressed(workspace),
  103. ),
  104. );
  105. }
  106. }