menu.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import 'package:app_flowy/workspace/presentation/widgets/menu/widget/top_bar.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_infra/size.dart';
  4. import 'package:flowy_infra_ui/widget/error_page.dart';
  5. import 'package:flowy_infra_ui/widget/spacing.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. import 'package:styled_widget/styled_widget.dart';
  11. import 'package:expandable/expandable.dart';
  12. import 'package:flowy_infra/time/duration.dart';
  13. import 'package:app_flowy/startup/startup.dart';
  14. import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
  15. import 'package:app_flowy/workspace/application/menu/menu_listen.dart';
  16. import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
  17. import 'package:app_flowy/workspace/presentation/widgets/menu/widget/menu_user.dart';
  18. import 'widget/app/menu_app.dart';
  19. import 'widget/app/create_button.dart';
  20. import 'widget/menu_trash.dart';
  21. // [[diagram: HomeMenu's widget structure]]
  22. // get user profile or modify user
  23. // ┌──────┐
  24. // ┌──────────┐ ┌──▶│IUser │
  25. // ┌─▶│MenuTopBar│ ┌────────┐ ┌─────────────┐ │ └──────┘
  26. // │ └──────────┘ ┌───│MenuUser│─▶│MenuUserBloc │──┤
  27. // ┌──────────┐ │ │ └────────┘ └─────────────┘ │ ┌─────────────┐
  28. // │ HomeMenu │─┤ │ └──▶│IUserListener│
  29. // └──────────┘ │ │ └─────────────┘
  30. // │ │ listen workspace changes or user
  31. // │ impl │ profile changes
  32. // │ ┌──────────┐ ┌─────────┐ │
  33. // └─▶│ MenuList │───▶│MenuItem │◀─┤
  34. // └──────────┘ └─────────┘ │ ┌────────┐
  35. // │ ┌─▶│AppBloc │ fetch app's views or modify view
  36. // │ │ └────────┘
  37. // │ ┌────────┐ │
  38. // └───│MenuApp │──┤
  39. // └────────┘ │
  40. // │ ┌──────────────┐
  41. // └─▶│AppListenBloc │ Receive view changes
  42. // └──────────────┘
  43. class HomeMenu extends StatelessWidget {
  44. final Function(HomeStackContext) pageContextChanged;
  45. final Function(bool) isCollapseChanged;
  46. final UserProfile user;
  47. final String workspaceId;
  48. const HomeMenu({
  49. Key? key,
  50. required this.pageContextChanged,
  51. required this.isCollapseChanged,
  52. required this.user,
  53. required this.workspaceId,
  54. }) : super(key: key);
  55. @override
  56. Widget build(BuildContext context) {
  57. return MultiBlocProvider(
  58. providers: [
  59. BlocProvider<MenuBloc>(
  60. create: (context) => getIt<MenuBloc>(param1: user, param2: workspaceId)..add(const MenuEvent.initial())),
  61. BlocProvider(
  62. create: (context) =>
  63. getIt<MenuListenBloc>(param1: user, param2: workspaceId)..add(const MenuListenEvent.started())),
  64. ],
  65. child: MultiBlocListener(
  66. listeners: [
  67. BlocListener<MenuBloc, MenuState>(
  68. listenWhen: (p, c) => p.context != c.context,
  69. listener: (context, state) => pageContextChanged(state.context),
  70. ),
  71. BlocListener<MenuBloc, MenuState>(
  72. listenWhen: (p, c) => p.isCollapse != c.isCollapse,
  73. listener: (context, state) => isCollapseChanged(state.isCollapse),
  74. )
  75. ],
  76. child: BlocBuilder<MenuBloc, MenuState>(
  77. builder: (context, state) => _renderBody(context),
  78. ),
  79. ),
  80. );
  81. }
  82. Widget _renderBody(BuildContext context) {
  83. // nested cloumn: https://siddharthmolleti.com/flutter-box-constraints-nested-column-s-row-s-3dfacada7361
  84. return Container(
  85. color: Theme.of(context).colorScheme.background,
  86. child: Column(
  87. mainAxisAlignment: MainAxisAlignment.start,
  88. children: [
  89. Expanded(
  90. child: Column(
  91. mainAxisAlignment: MainAxisAlignment.start,
  92. children: [
  93. _renderTopBar(context),
  94. const VSpace(32),
  95. _renderMenuList(context),
  96. ],
  97. ).padding(horizontal: Insets.l),
  98. ),
  99. const VSpace(20),
  100. _renderTrash(context).padding(horizontal: Insets.l),
  101. const VSpace(20),
  102. _renderNewAppButton(context),
  103. ],
  104. ),
  105. );
  106. }
  107. Widget _renderMenuList(BuildContext context) {
  108. return BlocBuilder<MenuListenBloc, MenuListenState>(
  109. builder: (context, state) {
  110. return state.map(
  111. initial: (_) => MenuList(
  112. menuItems: buildMenuItems(context.read<MenuBloc>().state.apps),
  113. ),
  114. loadApps: (s) => MenuList(
  115. menuItems: buildMenuItems(some(s.apps)),
  116. ),
  117. loadFail: (s) => FlowyErrorPage(s.error.toString()),
  118. );
  119. },
  120. );
  121. }
  122. Widget _renderTrash(BuildContext context) {
  123. return const MenuTrash();
  124. }
  125. Widget _renderNewAppButton(BuildContext context) {
  126. return NewAppButton(
  127. press: (appName) => context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
  128. );
  129. }
  130. Widget _renderTopBar(BuildContext context) {
  131. return const MenuTopBar();
  132. }
  133. List<MenuItem> buildMenuItems(Option<List<App>> apps) {
  134. List<MenuItem> items = [];
  135. items.add(MenuUser(user));
  136. List<MenuItem> appWidgets =
  137. apps.foldRight([], (apps, _) => apps.map((app) => MenuApp(MenuAppContext(app))).toList());
  138. items.addAll(appWidgets);
  139. return items;
  140. }
  141. }
  142. enum MenuItemType {
  143. userProfile,
  144. dashboard,
  145. favorites,
  146. app,
  147. }
  148. abstract class MenuItem extends StatelessWidget {
  149. const MenuItem({Key? key}) : super(key: key);
  150. MenuItemType get type;
  151. }
  152. class MenuList extends StatelessWidget {
  153. final List<MenuItem> menuItems;
  154. const MenuList({required this.menuItems, Key? key}) : super(key: key);
  155. @override
  156. Widget build(BuildContext context) {
  157. return ExpandableTheme(
  158. data: ExpandableThemeData(useInkWell: true, animationDuration: Durations.medium),
  159. child: Expanded(
  160. child: ListView.separated(
  161. itemCount: menuItems.length,
  162. separatorBuilder: (context, index) {
  163. if (index == 0) {
  164. return const VSpace(29);
  165. } else {
  166. return const VSpace(24);
  167. }
  168. },
  169. physics: const BouncingScrollPhysics(),
  170. itemBuilder: (BuildContext context, int index) {
  171. return menuItems[index];
  172. },
  173. ),
  174. ),
  175. );
  176. }
  177. }