home_stack.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import 'package:appflowy/core/frameless_window.dart';
  2. import 'package:appflowy/plugins/blank/blank.dart';
  3. import 'package:appflowy/startup/plugin/plugin.dart';
  4. import 'package:appflowy/startup/startup.dart';
  5. import 'package:appflowy/workspace/presentation/home/home_sizes.dart';
  6. import 'package:appflowy/workspace/presentation/home/navigation.dart';
  7. import 'package:appflowy/workspace/presentation/home/toast.dart';
  8. import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
  9. import 'package:flowy_infra_ui/style_widget/extension.dart';
  10. import 'package:flowy_infra_ui/widget/spacing.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:provider/provider.dart';
  13. import 'package:time/time.dart';
  14. import 'home_layout.dart';
  15. typedef NavigationCallback = void Function(String id);
  16. abstract class HomeStackDelegate {
  17. void didDeleteStackWidget(ViewPB view, int? index);
  18. }
  19. class HomeStack extends StatelessWidget {
  20. final HomeStackDelegate delegate;
  21. final HomeLayout layout;
  22. const HomeStack({
  23. required this.delegate,
  24. required this.layout,
  25. Key? key,
  26. }) : super(key: key);
  27. @override
  28. Widget build(BuildContext context) {
  29. return Column(
  30. mainAxisAlignment: MainAxisAlignment.start,
  31. children: [
  32. getIt<HomeStackManager>().stackTopBar(layout: layout),
  33. Expanded(
  34. child: Container(
  35. color: Theme.of(context).colorScheme.surface,
  36. child: FocusTraversalGroup(
  37. child: getIt<HomeStackManager>().stackWidget(
  38. onDeleted: (view, index) {
  39. delegate.didDeleteStackWidget(view, index);
  40. },
  41. ),
  42. ),
  43. ),
  44. ),
  45. ],
  46. );
  47. }
  48. }
  49. class FadingIndexedStack extends StatefulWidget {
  50. final int index;
  51. final List<Widget> children;
  52. final Duration duration;
  53. const FadingIndexedStack({
  54. Key? key,
  55. required this.index,
  56. required this.children,
  57. this.duration = const Duration(
  58. milliseconds: 250,
  59. ),
  60. }) : super(key: key);
  61. @override
  62. FadingIndexedStackState createState() => FadingIndexedStackState();
  63. }
  64. class FadingIndexedStackState extends State<FadingIndexedStack> {
  65. double _targetOpacity = 1;
  66. @override
  67. void initState() {
  68. super.initState();
  69. initToastWithContext(context);
  70. }
  71. @override
  72. void didUpdateWidget(FadingIndexedStack oldWidget) {
  73. if (oldWidget.index == widget.index) return;
  74. setState(() => _targetOpacity = 0);
  75. Future.delayed(1.milliseconds, () => setState(() => _targetOpacity = 1));
  76. super.didUpdateWidget(oldWidget);
  77. }
  78. @override
  79. Widget build(BuildContext context) {
  80. return TweenAnimationBuilder<double>(
  81. duration: _targetOpacity > 0 ? widget.duration : 0.milliseconds,
  82. tween: Tween(begin: 0, end: _targetOpacity),
  83. builder: (_, value, child) {
  84. return Opacity(opacity: value, child: child);
  85. },
  86. child: IndexedStack(index: widget.index, children: widget.children),
  87. );
  88. }
  89. }
  90. abstract class NavigationItem {
  91. Widget get leftBarItem;
  92. Widget? get rightBarItem => null;
  93. NavigationCallback get action => (id) {
  94. getIt<HomeStackManager>().setStackWithId(id);
  95. };
  96. }
  97. class HomeStackNotifier extends ChangeNotifier {
  98. Plugin _plugin;
  99. Widget get titleWidget => _plugin.display.leftBarItem;
  100. HomeStackNotifier({Plugin? plugin})
  101. : _plugin = plugin ?? makePlugin(pluginType: PluginType.blank);
  102. set plugin(Plugin newPlugin) {
  103. if (newPlugin.id == _plugin.id) {
  104. return;
  105. }
  106. _plugin.notifier?.isDisplayChanged.addListener(notifyListeners);
  107. _plugin.dispose();
  108. _plugin = newPlugin;
  109. _plugin.notifier?.isDisplayChanged.removeListener(notifyListeners);
  110. notifyListeners();
  111. }
  112. Plugin get plugin => _plugin;
  113. }
  114. // HomeStack is initialized as singleton to control the page stack.
  115. class HomeStackManager {
  116. final HomeStackNotifier _notifier = HomeStackNotifier();
  117. HomeStackManager();
  118. Widget title() {
  119. return _notifier.plugin.display.leftBarItem;
  120. }
  121. Plugin get plugin => _notifier.plugin;
  122. void setPlugin(Plugin newPlugin) {
  123. _notifier.plugin = newPlugin;
  124. }
  125. void setStackWithId(String id) {
  126. // Navigate to the page with id
  127. }
  128. Widget stackTopBar({required HomeLayout layout}) {
  129. return MultiProvider(
  130. providers: [
  131. ChangeNotifierProvider.value(value: _notifier),
  132. ],
  133. child: Selector<HomeStackNotifier, Widget>(
  134. selector: (context, notifier) => notifier.titleWidget,
  135. builder: (context, widget, child) {
  136. return MoveWindowDetector(child: HomeTopBar(layout: layout));
  137. },
  138. ),
  139. );
  140. }
  141. Widget stackWidget({required Function(ViewPB, int?) onDeleted}) {
  142. return MultiProvider(
  143. providers: [ChangeNotifierProvider.value(value: _notifier)],
  144. child: Consumer(
  145. builder: (_, HomeStackNotifier notifier, __) {
  146. return FadingIndexedStack(
  147. index: getIt<PluginSandbox>().indexOf(notifier.plugin.ty),
  148. children: getIt<PluginSandbox>().supportPluginTypes.map(
  149. (pluginType) {
  150. if (pluginType == notifier.plugin.ty) {
  151. final pluginWidget = notifier.plugin.display
  152. .buildWidget(PluginContext(onDeleted: onDeleted));
  153. if (pluginType == PluginType.editor) {
  154. return pluginWidget;
  155. }
  156. return pluginWidget.padding(horizontal: 40, vertical: 28);
  157. }
  158. return const BlankPage();
  159. },
  160. ).toList(),
  161. );
  162. },
  163. ),
  164. );
  165. }
  166. }
  167. class HomeTopBar extends StatelessWidget {
  168. const HomeTopBar({Key? key, required this.layout}) : super(key: key);
  169. final HomeLayout layout;
  170. @override
  171. Widget build(BuildContext context) {
  172. return Container(
  173. color: Theme.of(context).colorScheme.onSecondaryContainer,
  174. height: HomeSizes.topBarHeight,
  175. child: Padding(
  176. padding: const EdgeInsets.symmetric(
  177. horizontal: HomeInsets.topBarTitlePadding,
  178. ),
  179. child: Row(
  180. crossAxisAlignment: CrossAxisAlignment.center,
  181. children: [
  182. HSpace(layout.menuSpacing),
  183. const FlowyNavigation(),
  184. const HSpace(16),
  185. ChangeNotifierProvider.value(
  186. value: Provider.of<HomeStackNotifier>(context, listen: false),
  187. child: Consumer(
  188. builder: (_, HomeStackNotifier notifier, __) =>
  189. notifier.plugin.display.rightBarItem ??
  190. const SizedBox.shrink(),
  191. ),
  192. ),
  193. ],
  194. ).bottomBorder(color: Theme.of(context).dividerColor),
  195. ),
  196. );
  197. }
  198. }