menu.dart 7.1 KB

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