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