home_stack.dart 6.4 KB

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