home_stack.dart 6.7 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-folder2/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 mixin 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.widgetBuilder.leftBarItem;
  100. HomeStackNotifier({Plugin? plugin})
  101. : _plugin = plugin ?? makePlugin(pluginType: PluginType.blank);
  102. /// This is the only place where the plugin is set.
  103. /// No need compare the old plugin with the new plugin. Just set it.
  104. set plugin(Plugin newPlugin) {
  105. _plugin.notifier?.isDisplayChanged.addListener(notifyListeners);
  106. _plugin.dispose();
  107. _plugin = newPlugin;
  108. _plugin.notifier?.isDisplayChanged.removeListener(notifyListeners);
  109. notifyListeners();
  110. }
  111. Plugin get plugin => _plugin;
  112. }
  113. // HomeStack is initialized as singleton to control the page stack.
  114. class HomeStackManager {
  115. final HomeStackNotifier _notifier = HomeStackNotifier();
  116. HomeStackManager();
  117. Widget title() {
  118. return _notifier.plugin.widgetBuilder.leftBarItem;
  119. }
  120. Plugin get plugin => _notifier.plugin;
  121. void setPlugin(Plugin newPlugin) {
  122. _notifier.plugin = newPlugin;
  123. }
  124. void setStackWithId(String id) {
  125. // Navigate to the page with id
  126. }
  127. Widget stackTopBar({required HomeLayout layout}) {
  128. return MultiProvider(
  129. providers: [
  130. ChangeNotifierProvider.value(value: _notifier),
  131. ],
  132. child: Selector<HomeStackNotifier, Widget>(
  133. selector: (context, notifier) => notifier.titleWidget,
  134. builder: (context, widget, child) {
  135. return MoveWindowDetector(child: HomeTopBar(layout: layout));
  136. },
  137. ),
  138. );
  139. }
  140. Widget stackWidget({required Function(ViewPB, int?) onDeleted}) {
  141. return MultiProvider(
  142. providers: [ChangeNotifierProvider.value(value: _notifier)],
  143. child: Consumer(
  144. builder: (_, HomeStackNotifier notifier, __) {
  145. return FadingIndexedStack(
  146. index: getIt<PluginSandbox>().indexOf(notifier.plugin.pluginType),
  147. children: getIt<PluginSandbox>().supportPluginTypes.map(
  148. (pluginType) {
  149. if (pluginType == notifier.plugin.pluginType) {
  150. final builder = notifier.plugin.widgetBuilder;
  151. final pluginWidget = builder.buildWidget(
  152. PluginContext(onDeleted: onDeleted),
  153. );
  154. return Padding(
  155. padding: builder.contentPadding,
  156. child: pluginWidget,
  157. );
  158. } else {
  159. return const BlankPage();
  160. }
  161. },
  162. ).toList(),
  163. );
  164. },
  165. ),
  166. );
  167. }
  168. }
  169. class HomeTopBar extends StatelessWidget {
  170. const HomeTopBar({Key? key, required this.layout}) : super(key: key);
  171. final HomeLayout layout;
  172. @override
  173. Widget build(BuildContext context) {
  174. return Container(
  175. color: Theme.of(context).colorScheme.onSecondaryContainer,
  176. height: HomeSizes.topBarHeight,
  177. child: Padding(
  178. padding: const EdgeInsets.symmetric(
  179. horizontal: HomeInsets.topBarTitlePadding,
  180. ),
  181. child: Row(
  182. crossAxisAlignment: CrossAxisAlignment.center,
  183. children: [
  184. HSpace(layout.menuSpacing),
  185. const FlowyNavigation(),
  186. const HSpace(16),
  187. ChangeNotifierProvider.value(
  188. value: Provider.of<HomeStackNotifier>(context, listen: false),
  189. child: Consumer(
  190. builder: (_, HomeStackNotifier notifier, __) =>
  191. notifier.plugin.widgetBuilder.rightBarItem ??
  192. const SizedBox.shrink(),
  193. ),
  194. ),
  195. ],
  196. ),
  197. ).bottomBorder(color: Theme.of(context).dividerColor),
  198. );
  199. }
  200. }