navigation.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import 'package:app_flowy/workspace/application/home/home_bloc.dart';
  2. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  3. import 'package:flowy_infra/image.dart';
  4. import 'package:flowy_infra/notifier.dart';
  5. import 'package:flowy_infra/theme.dart';
  6. import 'package:flowy_infra_ui/style_widget/icon_button.dart';
  7. import 'package:flowy_infra_ui/style_widget/text.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:provider/provider.dart';
  10. import 'package:styled_widget/styled_widget.dart';
  11. typedef NaviAction = void Function();
  12. class NavigationNotifier with ChangeNotifier {
  13. List<NavigationItem> navigationItems;
  14. PublishNotifier<bool> collapasedNotifier;
  15. NavigationNotifier({required this.navigationItems, required this.collapasedNotifier});
  16. void update(HomeStackNotifier notifier) {
  17. bool shouldNotify = false;
  18. if (navigationItems != notifier.plugin.display.navigationItems) {
  19. navigationItems = notifier.plugin.display.navigationItems;
  20. shouldNotify = true;
  21. }
  22. if (shouldNotify) {
  23. notifyListeners();
  24. }
  25. }
  26. }
  27. // [[diagram: HomeStack navigation flow]]
  28. // ┌───────────────────────┐
  29. // 2.notify listeners ┌──────│DefaultHomeStackContext│
  30. // ┌────────────────┐ ┌───────────┐ ┌────────────────┐ │ └───────────────────────┘
  31. // │HomeStackNotifie│◀──────────│ HomeStack │◀──│HomeStackContext│◀─ impl
  32. // └────────────────┘ └───────────┘ └────────────────┘ │ ┌───────────────────┐
  33. // │ ▲ └───────│ DocStackContext │
  34. // │ │ └───────────────────┘
  35. // 3.notify change 1.set context
  36. // │ │
  37. // ▼ │
  38. // ┌───────────────────┐ ┌──────────────────┐
  39. // │NavigationNotifier │ │ ViewSectionItem │
  40. // └───────────────────┘ └──────────────────┘
  41. // │
  42. // │
  43. // ▼
  44. // ┌─────────────────┐
  45. // │ FlowyNavigation │ 4.render navigation items
  46. // └─────────────────┘
  47. class FlowyNavigation extends StatelessWidget {
  48. const FlowyNavigation({Key? key}) : super(key: key);
  49. @override
  50. Widget build(BuildContext context) {
  51. final theme = context.watch<AppTheme>();
  52. return ChangeNotifierProxyProvider<HomeStackNotifier, NavigationNotifier>(
  53. create: (_) {
  54. final notifier = Provider.of<HomeStackNotifier>(context, listen: false);
  55. return NavigationNotifier(
  56. navigationItems: notifier.plugin.display.navigationItems,
  57. collapasedNotifier: notifier.collapsedNotifier,
  58. );
  59. },
  60. update: (_, notifier, controller) => controller!..update(notifier),
  61. child: Expanded(
  62. child: Row(children: [
  63. Selector<NavigationNotifier, PublishNotifier<bool>>(
  64. selector: (context, notifier) => notifier.collapasedNotifier,
  65. builder: (ctx, collapsedNotifier, child) => _renderCollapse(ctx, collapsedNotifier, theme)),
  66. Selector<NavigationNotifier, List<NavigationItem>>(
  67. selector: (context, notifier) => notifier.navigationItems,
  68. builder: (ctx, items, child) => Expanded(
  69. child: Row(
  70. children: _renderNavigationItems(items),
  71. // crossAxisAlignment: WrapCrossAlignment.start,
  72. ),
  73. ),
  74. ),
  75. ]),
  76. ),
  77. );
  78. }
  79. Widget _renderCollapse(BuildContext context, PublishNotifier<bool> collapsedNotifier, AppTheme theme) {
  80. return ChangeNotifierProvider.value(
  81. value: collapsedNotifier,
  82. child: Consumer(
  83. builder: (ctx, PublishNotifier<bool> notifier, child) {
  84. if (notifier.currentValue ?? false) {
  85. return RotationTransition(
  86. turns: const AlwaysStoppedAnimation(180 / 360),
  87. child: FlowyIconButton(
  88. width: 24,
  89. onPressed: () {
  90. notifier.value = false;
  91. ctx.read<HomeBloc>().add(const HomeEvent.collapseMenu());
  92. },
  93. iconPadding: const EdgeInsets.fromLTRB(2, 2, 2, 2),
  94. icon: svgWidget("home/hide_menu", color: theme.iconColor),
  95. ),
  96. );
  97. } else {
  98. return Container();
  99. }
  100. },
  101. ),
  102. );
  103. }
  104. List<Widget> _renderNavigationItems(List<NavigationItem> items) {
  105. if (items.isEmpty) {
  106. return [];
  107. }
  108. List<NavigationItem> newItems = _filter(items);
  109. Widget last = NaviItemWidget(newItems.removeLast());
  110. List<Widget> widgets = List.empty(growable: true);
  111. // widgets.addAll(newItems.map((item) => NaviItemDivider(child: NaviItemWidget(item))).toList());
  112. for (final item in newItems) {
  113. widgets.add(NaviItemWidget(item));
  114. widgets.add(const Text('/'));
  115. }
  116. widgets.add(last);
  117. return widgets;
  118. }
  119. List<NavigationItem> _filter(List<NavigationItem> items) {
  120. final length = items.length;
  121. if (length > 4) {
  122. final first = items[0];
  123. final ellipsisItems = items.getRange(1, length - 2).toList();
  124. final last = items.getRange(length - 2, length).toList();
  125. return [
  126. first,
  127. EllipsisNaviItem(items: ellipsisItems),
  128. ...last,
  129. ];
  130. } else {
  131. return items;
  132. }
  133. }
  134. }
  135. class NaviItemWidget extends StatelessWidget {
  136. final NavigationItem item;
  137. const NaviItemWidget(this.item, {Key? key}) : super(key: key);
  138. @override
  139. Widget build(BuildContext context) {
  140. return Expanded(child: item.leftBarItem.padding(horizontal: 2, vertical: 2));
  141. }
  142. }
  143. class NaviItemDivider extends StatelessWidget {
  144. final Widget child;
  145. const NaviItemDivider({Key? key, required this.child}) : super(key: key);
  146. @override
  147. Widget build(BuildContext context) {
  148. return Row(
  149. children: [child, const Text('/')],
  150. );
  151. }
  152. }
  153. class EllipsisNaviItem extends NavigationItem {
  154. final List<NavigationItem> items;
  155. EllipsisNaviItem({
  156. required this.items,
  157. });
  158. @override
  159. Widget get leftBarItem => const FlowyText.medium('...');
  160. @override
  161. NavigationCallback get action => (id) {};
  162. }