home_stack.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/log.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. class HomeStack extends StatelessWidget {
  19. const HomeStack({Key? key, required this.layout}) : super(key: key);
  20. final HomeLayout layout;
  21. @override
  22. Widget build(BuildContext context) {
  23. Log.info('HomePage build');
  24. final theme = context.watch<AppTheme>();
  25. return Column(
  26. mainAxisAlignment: MainAxisAlignment.start,
  27. children: [
  28. getIt<HomeStackManager>().stackTopBar(layout: layout),
  29. Expanded(
  30. child: Container(
  31. color: theme.surface,
  32. child: FocusTraversalGroup(
  33. child: getIt<HomeStackManager>().stackWidget(),
  34. ),
  35. ),
  36. ),
  37. ],
  38. );
  39. }
  40. }
  41. class FadingIndexedStack extends StatefulWidget {
  42. final int index;
  43. final List<Widget> children;
  44. final Duration duration;
  45. const FadingIndexedStack({
  46. Key? key,
  47. required this.index,
  48. required this.children,
  49. this.duration = const Duration(
  50. milliseconds: 250,
  51. ),
  52. }) : super(key: key);
  53. @override
  54. FadingIndexedStackState createState() => FadingIndexedStackState();
  55. }
  56. class FadingIndexedStackState extends State<FadingIndexedStack> {
  57. double _targetOpacity = 1;
  58. @override
  59. void initState() {
  60. super.initState();
  61. initToastWithContext(context);
  62. }
  63. @override
  64. void didUpdateWidget(FadingIndexedStack oldWidget) {
  65. if (oldWidget.index == widget.index) return;
  66. setState(() => _targetOpacity = 0);
  67. Future.delayed(1.milliseconds, () => setState(() => _targetOpacity = 1));
  68. super.didUpdateWidget(oldWidget);
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. return TweenAnimationBuilder<double>(
  73. duration: _targetOpacity > 0 ? widget.duration : 0.milliseconds,
  74. tween: Tween(begin: 0, end: _targetOpacity),
  75. builder: (_, value, child) {
  76. return Opacity(opacity: value, child: child);
  77. },
  78. child: IndexedStack(index: widget.index, children: widget.children),
  79. );
  80. }
  81. }
  82. abstract class NavigationItem {
  83. Widget get leftBarItem;
  84. Widget? get rightBarItem => null;
  85. NavigationCallback get action => (id) {
  86. getIt<HomeStackManager>().setStackWithId(id);
  87. };
  88. }
  89. class HomeStackNotifier extends ChangeNotifier {
  90. Plugin _plugin;
  91. PublishNotifier<bool> collapsedNotifier = PublishNotifier();
  92. Widget get titleWidget => _plugin.display.leftBarItem;
  93. HomeStackNotifier({Plugin? plugin})
  94. : _plugin = plugin ?? makePlugin(pluginType: PluginType.blank);
  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({required HomeLayout layout}) {
  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 MoveWindowDetector(child: HomeTopBar(layout: layout));
  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
  143. .buildWidget()
  144. .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, required this.layout}) : super(key: key);
  156. final HomeLayout layout;
  157. @override
  158. Widget build(BuildContext context) {
  159. final theme = context.watch<AppTheme>();
  160. return Container(
  161. color: theme.surface,
  162. height: HomeSizes.topBarHeight,
  163. child: Row(
  164. crossAxisAlignment: CrossAxisAlignment.center,
  165. children: [
  166. HSpace(layout.menuSpacing),
  167. const FlowyNavigation(),
  168. const HSpace(16),
  169. ChangeNotifierProvider.value(
  170. value: Provider.of<HomeStackNotifier>(context, listen: false),
  171. child: Consumer(
  172. builder: (BuildContext context, HomeStackNotifier notifier,
  173. Widget? child) {
  174. return notifier.plugin.display.rightBarItem ?? const SizedBox();
  175. },
  176. ),
  177. ) // _renderMoreButton(),
  178. ],
  179. )
  180. .padding(
  181. horizontal: HomeInsets.topBarTitlePadding,
  182. )
  183. .bottomBorder(color: Colors.grey.shade300),
  184. );
  185. }
  186. }