navigation.dart 7.0 KB

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