menu.dart 8.0 KB

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