navigation.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: Row(children: [
  59. Selector<NavigationNotifier, PublishNotifier<bool>>(
  60. selector: (context, notifier) => notifier.collapasedNotifier,
  61. builder: (ctx, collapsedNotifier, child) => _renderCollapse(ctx, collapsedNotifier)),
  62. Selector<NavigationNotifier, List<NavigationItem>>(
  63. selector: (context, notifier) => notifier.navigationItems,
  64. builder: (ctx, items, child) => Row(children: _renderNavigationItems(items))),
  65. ]),
  66. );
  67. }
  68. Widget _renderCollapse(BuildContext context, PublishNotifier<bool> collapsedNotifier) {
  69. return ChangeNotifierProvider.value(
  70. value: collapsedNotifier,
  71. child: Consumer(
  72. builder: (ctx, PublishNotifier<bool> notifier, child) {
  73. if (notifier.currentValue ?? false) {
  74. return RotationTransition(
  75. turns: const AlwaysStoppedAnimation(180 / 360),
  76. child: FlowyIconButton(
  77. width: 24,
  78. onPressed: () {
  79. notifier.value = false;
  80. },
  81. iconPadding: const EdgeInsets.fromLTRB(2, 2, 2, 2),
  82. icon: svg("home/hide_menu"),
  83. ),
  84. );
  85. } else {
  86. return Container();
  87. }
  88. },
  89. ),
  90. );
  91. }
  92. List<Widget> _renderNavigationItems(List<NavigationItem> items) {
  93. if (items.isEmpty) {
  94. return [];
  95. }
  96. List<NavigationItem> newItems = _filter(items);
  97. Widget last = NaviItemWidget(newItems.removeLast());
  98. List<Widget> widgets = List.empty(growable: true);
  99. widgets.addAll(newItems.map((item) => NaviItemDivider(child: NaviItemWidget(item))).toList());
  100. widgets.add(last);
  101. return widgets;
  102. }
  103. List<NavigationItem> _filter(List<NavigationItem> items) {
  104. final length = items.length;
  105. if (length > 4) {
  106. final first = items[0];
  107. final ellipsisItems = items.getRange(1, length - 2).toList();
  108. final last = items.getRange(length - 2, length).toList();
  109. return [
  110. first,
  111. EllipsisNaviItem(items: ellipsisItems),
  112. ...last,
  113. ];
  114. } else {
  115. return items;
  116. }
  117. }
  118. }
  119. class IconNaviItemWidget extends StatelessWidget {
  120. final NavigationItem item;
  121. const IconNaviItemWidget(this.item, {Key? key}) : super(key: key);
  122. @override
  123. Widget build(BuildContext context) {
  124. return SizedBox(
  125. height: 24,
  126. child: InkWell(
  127. child: item.naviTitle,
  128. onTap: () {
  129. debugPrint('show app document');
  130. },
  131. ).padding(horizontal: 8, vertical: 2),
  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 SizedBox(
  141. height: 24,
  142. child: InkWell(
  143. child: item.naviTitle,
  144. onTap: () {
  145. debugPrint('show app document');
  146. },
  147. ).padding(horizontal: 8, vertical: 2),
  148. );
  149. }
  150. }
  151. class NaviItemDivider extends StatelessWidget {
  152. final Widget child;
  153. const NaviItemDivider({Key? key, required this.child}) : super(key: key);
  154. @override
  155. Widget build(BuildContext context) {
  156. return Row(
  157. children: [child, const Text('/').padding(horizontal: 2)],
  158. );
  159. }
  160. }
  161. class EllipsisNaviItem extends NavigationItem {
  162. final List<NavigationItem> items;
  163. EllipsisNaviItem({
  164. required this.items,
  165. });
  166. @override
  167. Widget get naviTitle => const FlowyText.medium('...');
  168. @override
  169. NavigationCallback get action => (id) {};
  170. @override
  171. String get identifier => "Ellipsis";
  172. }