home_stack.dart 6.3 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/plugins/blank/blank.dart';
  5. import 'package:app_flowy/workspace/presentation/home/toast.dart';
  6. import 'package:flowy_infra/theme.dart';
  7. import 'package:flowy_sdk/log.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_bloc/flutter_bloc.dart';
  10. import 'package:provider/provider.dart';
  11. import 'package:time/time.dart';
  12. import 'package:app_flowy/startup/plugin/plugin.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. import 'home_layout.dart';
  20. typedef NavigationCallback = void Function(String id);
  21. class HomeStack extends StatelessWidget {
  22. const HomeStack({Key? key, required this.layout}) : super(key: key);
  23. final HomeLayout layout;
  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(layout: layout),
  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})
  97. : _plugin = plugin ?? makePlugin(pluginType: DefaultPlugin.blank.type());
  98. set plugin(Plugin newPlugin) {
  99. if (newPlugin.id == _plugin.id) {
  100. return;
  101. }
  102. _plugin.display.notifier?.removeListener(notifyListeners);
  103. _plugin.dispose();
  104. _plugin = newPlugin;
  105. _plugin.display.notifier?.addListener(notifyListeners);
  106. notifyListeners();
  107. }
  108. Plugin get plugin => _plugin;
  109. }
  110. // HomeStack is initialized as singleton to controll the page stack.
  111. class HomeStackManager {
  112. final HomeStackNotifier _notifier = HomeStackNotifier();
  113. HomeStackManager();
  114. Widget title() {
  115. return _notifier.plugin.display.leftBarItem;
  116. }
  117. PublishNotifier<bool> get collapsedNotifier => _notifier.collapsedNotifier;
  118. void setPlugin(Plugin newPlugin) {
  119. _notifier.plugin = newPlugin;
  120. }
  121. void setStackWithId(String id) {}
  122. Widget stackTopBar({required HomeLayout layout}) {
  123. return MultiProvider(
  124. providers: [
  125. ChangeNotifierProvider.value(value: _notifier),
  126. ],
  127. child: Selector<HomeStackNotifier, Widget>(
  128. selector: (context, notifier) => notifier.titleWidget,
  129. builder: (context, widget, child) {
  130. return MoveWindowDetector(child: HomeTopBar(layout: layout));
  131. },
  132. ),
  133. );
  134. }
  135. Widget stackWidget() {
  136. return MultiProvider(
  137. providers: [
  138. ChangeNotifierProvider.value(value: _notifier),
  139. ],
  140. child: Consumer(builder: (ctx, HomeStackNotifier notifier, child) {
  141. return FadingIndexedStack(
  142. index: getIt<PluginSandbox>().indexOf(notifier.plugin.ty),
  143. children: getIt<PluginSandbox>().supportPluginTypes.map((pluginType) {
  144. if (pluginType == notifier.plugin.ty) {
  145. return notifier.plugin.display
  146. .buildWidget()
  147. .padding(horizontal: 40, vertical: 28);
  148. } else {
  149. return const BlankPage();
  150. }
  151. }).toList(),
  152. );
  153. }),
  154. );
  155. }
  156. }
  157. class HomeTopBar extends StatelessWidget {
  158. const HomeTopBar({Key? key, required this.layout}) : super(key: key);
  159. final HomeLayout layout;
  160. @override
  161. Widget build(BuildContext context) {
  162. final theme = context.watch<AppTheme>();
  163. return Container(
  164. color: theme.surface,
  165. height: HomeSizes.topBarHeight,
  166. child: Row(
  167. crossAxisAlignment: CrossAxisAlignment.center,
  168. children: [
  169. HSpace(layout.menuSpacing),
  170. const FlowyNavigation(),
  171. const HSpace(16),
  172. ChangeNotifierProvider.value(
  173. value: Provider.of<HomeStackNotifier>(context, listen: false),
  174. child: Consumer(
  175. builder: (BuildContext context, HomeStackNotifier notifier,
  176. 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. }