navigation.dart 6.6 KB

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