menu.dart 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'package:app_flowy/workspace/presentation/widgets/menu/widget/top_bar.dart';
  2. import 'package:flowy_infra/notifier.dart';
  3. import 'package:flowy_infra/size.dart';
  4. import 'package:flowy_infra/theme.dart';
  5. import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart';
  6. import 'package:flowy_infra_ui/widget/spacing.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile;
  8. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
  9. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_bloc/flutter_bloc.dart';
  12. import 'package:provider/provider.dart';
  13. import 'package:styled_widget/styled_widget.dart';
  14. import 'package:expandable/expandable.dart';
  15. import 'package:flowy_infra/time/duration.dart';
  16. import 'package:app_flowy/startup/startup.dart';
  17. import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
  18. import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
  19. import 'package:app_flowy/workspace/presentation/widgets/menu/widget/menu_user.dart';
  20. import 'widget/app/menu_app.dart';
  21. import 'widget/app/create_button.dart';
  22. import 'widget/menu_trash.dart';
  23. // [[diagram: HomeMenu's widget structure]]
  24. // get user profile or modify user
  25. // ┌──────┐
  26. // ┌──────────┐ ┌──▶│IUser │
  27. // ┌─▶│MenuTopBar│ ┌────────┐ ┌─────────────┐ │ └──────┘
  28. // │ └──────────┘ ┌───│MenuUser│─▶│MenuUserBloc │──┤
  29. // ┌──────────┐ │ │ └────────┘ └─────────────┘ │ ┌─────────────┐
  30. // │ HomeMenu │─┤ │ └──▶│IUserListener│
  31. // └──────────┘ │ │ └─────────────┘
  32. // │ │ listen workspace changes or user
  33. // │ impl │ profile changes
  34. // │ ┌──────────┐ ┌─────────┐ │
  35. // └─▶│ MenuList │───▶│MenuItem │◀─┤
  36. // └──────────┘ └─────────┘ │ ┌────────┐
  37. // │ ┌─▶│AppBloc │ fetch app's views or modify view
  38. // │ │ └────────┘
  39. // │ ┌────────┐ │
  40. // └───│MenuApp │──┤
  41. // └────────┘
  42. class HomeMenu extends StatelessWidget {
  43. final PublishNotifier<bool> _collapsedNotifier;
  44. final UserProfile user;
  45. final CurrentWorkspaceSetting workspaceSetting;
  46. const HomeMenu({
  47. Key? key,
  48. required this.user,
  49. required this.workspaceSetting,
  50. required PublishNotifier<bool> collapsedNotifier,
  51. }) : _collapsedNotifier = collapsedNotifier,
  52. super(key: key);
  53. @override
  54. Widget build(BuildContext context) {
  55. return MultiBlocProvider(
  56. providers: [
  57. BlocProvider<MenuBloc>(
  58. create: (context) {
  59. final menuBloc = getIt<MenuBloc>(param1: user, param2: workspaceSetting.workspace.id);
  60. menuBloc.add(const MenuEvent.initial());
  61. return menuBloc;
  62. },
  63. ),
  64. ],
  65. child: MultiBlocListener(
  66. listeners: [
  67. BlocListener<MenuBloc, MenuState>(
  68. listenWhen: (p, c) => p.plugin.pluginId != c.plugin.pluginId,
  69. listener: (context, state) {
  70. getIt<HomeStackManager>().setPlugin(state.plugin);
  71. },
  72. ),
  73. BlocListener<MenuBloc, MenuState>(
  74. listenWhen: (p, c) => p.isCollapse != c.isCollapse,
  75. listener: (context, state) {
  76. _collapsedNotifier.value = state.isCollapse;
  77. },
  78. )
  79. ],
  80. child: BlocBuilder<MenuBloc, MenuState>(
  81. builder: (context, state) => _renderBody(context),
  82. ),
  83. ),
  84. );
  85. }
  86. Widget _renderBody(BuildContext context) {
  87. // nested cloumn: https://siddharthmolleti.com/flutter-box-constraints-nested-column-s-row-s-3dfacada7361
  88. final theme = context.watch<AppTheme>();
  89. return Container(
  90. color: theme.bg1,
  91. child: ChangeNotifierProvider(
  92. create: (_) => MenuSharedState(view: workspaceSetting.hasLatestView() ? workspaceSetting.latestView : null),
  93. child: Consumer(builder: (context, MenuSharedState sharedState, child) {
  94. return Column(
  95. mainAxisAlignment: MainAxisAlignment.start,
  96. children: [
  97. Expanded(
  98. child: Column(
  99. mainAxisAlignment: MainAxisAlignment.start,
  100. children: [
  101. const MenuTopBar(),
  102. const VSpace(10),
  103. _renderApps(context),
  104. ],
  105. ).padding(horizontal: Insets.l),
  106. ),
  107. const VSpace(20),
  108. _renderTrash(context).padding(horizontal: Insets.l),
  109. const VSpace(20),
  110. _renderNewAppButton(context),
  111. ],
  112. );
  113. }),
  114. ),
  115. );
  116. }
  117. Widget _renderApps(BuildContext context) {
  118. return ExpandableTheme(
  119. data: ExpandableThemeData(useInkWell: true, animationDuration: Durations.medium),
  120. child: Expanded(
  121. child: ScrollConfiguration(
  122. behavior: const ScrollBehavior().copyWith(scrollbars: false),
  123. child: BlocSelector<MenuBloc, MenuState, List<Widget>>(
  124. selector: (state) {
  125. List<Widget> menuItems = [];
  126. menuItems.add(MenuUser(user));
  127. List<MenuApp> appWidgets =
  128. state.apps.foldRight([], (apps, _) => apps.map((app) => MenuApp(app)).toList());
  129. menuItems.addAll(appWidgets);
  130. return menuItems;
  131. },
  132. builder: (context, menuItems) => ListView.separated(
  133. itemCount: menuItems.length,
  134. separatorBuilder: (context, index) {
  135. if (index == 0) {
  136. return const VSpace(20);
  137. } else {
  138. return VSpace(MenuAppSizes.appVPadding);
  139. }
  140. },
  141. physics: StyledScrollPhysics(),
  142. itemBuilder: (BuildContext context, int index) {
  143. return menuItems[index];
  144. },
  145. ),
  146. ),
  147. ),
  148. ),
  149. );
  150. }
  151. Widget _renderTrash(BuildContext context) {
  152. return const MenuTrash();
  153. }
  154. Widget _renderNewAppButton(BuildContext context) {
  155. return NewAppButton(
  156. press: (appName) => context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
  157. );
  158. }
  159. }
  160. class MenuSharedState extends ChangeNotifier {
  161. PublishNotifier<View> forcedOpenView = PublishNotifier();
  162. ValueNotifier<View?> selectedView = ValueNotifier<View?>(null);
  163. MenuSharedState({View? view}) {
  164. if (view != null) {
  165. selectedView.value = view;
  166. }
  167. forcedOpenView.addPublishListener((view) {
  168. selectedView.value = view;
  169. });
  170. }
  171. }