welcome_screen.dart 3.1 KB

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