welcome_screen.dart 3.0 KB

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