navigation.dart 6.8 KB

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