welcome_screen.dart 3.2 KB

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