home_stack.dart 6.6 KB

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