welcome_screen.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_ui/style_widget/scrolling/styled_list.dart';
  5. import 'package:flowy_infra_ui/style_widget/text_button.dart';
  6. import 'package:flowy_infra_ui/widget/error_page.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-workspace/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)
  21. ..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. return SizedBox(
  48. width: 200,
  49. height: 40,
  50. child: FlowyTextButton(
  51. "Create workspace",
  52. fontSize: 14,
  53. onPressed: () {
  54. context
  55. .read<WelcomeBloc>()
  56. .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(
  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. }