home_stack.dart 6.5 KB

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