menu.dart 7.2 KB

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