welcome_screen.dart 3.1 KB

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