menu.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/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. ],
  62. child: MultiBlocListener(
  63. listeners: [
  64. BlocListener<MenuBloc, MenuState>(
  65. listenWhen: (p, c) => p.context != c.context,
  66. listener: (context, state) => pageContextChanged(state.context),
  67. ),
  68. BlocListener<MenuBloc, MenuState>(
  69. listenWhen: (p, c) => p.isCollapse != c.isCollapse,
  70. listener: (context, state) => isCollapseChanged(state.isCollapse),
  71. )
  72. ],
  73. child: BlocBuilder<MenuBloc, MenuState>(
  74. builder: (context, state) => _renderBody(context),
  75. ),
  76. ),
  77. );
  78. }
  79. Widget _renderBody(BuildContext context) {
  80. // nested cloumn: https://siddharthmolleti.com/flutter-box-constraints-nested-column-s-row-s-3dfacada7361
  81. return Container(
  82. color: Theme.of(context).colorScheme.background,
  83. child: Column(
  84. mainAxisAlignment: MainAxisAlignment.start,
  85. children: [
  86. Expanded(
  87. child: Column(
  88. mainAxisAlignment: MainAxisAlignment.start,
  89. children: [
  90. _renderTopBar(context),
  91. const VSpace(32),
  92. _renderMenuList(context),
  93. ],
  94. ).padding(horizontal: Insets.l),
  95. ),
  96. const VSpace(20),
  97. _renderTrash(context).padding(horizontal: Insets.l),
  98. const VSpace(20),
  99. _renderNewAppButton(context),
  100. ],
  101. ),
  102. );
  103. }
  104. Widget _renderMenuList(BuildContext context) {
  105. return MenuList(
  106. menuItems: buildMenuItems(context.read<MenuBloc>().state.apps),
  107. );
  108. }
  109. Widget _renderTrash(BuildContext context) {
  110. return const MenuTrash();
  111. }
  112. Widget _renderNewAppButton(BuildContext context) {
  113. return NewAppButton(
  114. press: (appName) => context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
  115. );
  116. }
  117. Widget _renderTopBar(BuildContext context) {
  118. return const MenuTopBar();
  119. }
  120. List<MenuItem> buildMenuItems(Option<List<App>> apps) {
  121. List<MenuItem> items = [];
  122. items.add(MenuUser(user));
  123. List<MenuItem> appWidgets =
  124. apps.foldRight([], (apps, _) => apps.map((app) => MenuApp(MenuAppContext(app))).toList());
  125. items.addAll(appWidgets);
  126. return items;
  127. }
  128. }
  129. enum MenuItemType {
  130. userProfile,
  131. dashboard,
  132. favorites,
  133. app,
  134. }
  135. abstract class MenuItem extends StatelessWidget {
  136. const MenuItem({Key? key}) : super(key: key);
  137. MenuItemType get type;
  138. }
  139. class MenuList extends StatelessWidget {
  140. final List<MenuItem> menuItems;
  141. const MenuList({required this.menuItems, Key? key}) : super(key: key);
  142. @override
  143. Widget build(BuildContext context) {
  144. return ExpandableTheme(
  145. data: ExpandableThemeData(useInkWell: true, animationDuration: Durations.medium),
  146. child: Expanded(
  147. child: ScrollConfiguration(
  148. behavior: const ScrollBehavior().copyWith(scrollbars: false),
  149. child: ListView.separated(
  150. itemCount: menuItems.length,
  151. separatorBuilder: (context, index) {
  152. if (index == 0) {
  153. return const VSpace(29);
  154. } else {
  155. return const VSpace(24);
  156. }
  157. },
  158. physics: StyledScrollPhysics(),
  159. itemBuilder: (BuildContext context, int index) {
  160. return menuItems[index];
  161. },
  162. ),
  163. ),
  164. ),
  165. );
  166. }
  167. }