menu.dart 8.1 KB

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