home_stack.dart 6.5 KB

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