home_stack.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import 'package:app_flowy/startup/startup.dart';
  2. import 'package:app_flowy/workspace/presentation/home/home_screen.dart';
  3. import 'package:flowy_infra/theme.dart';
  4. import 'package:flowy_sdk/log.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:time/time.dart';
  8. import 'package:fluttertoast/fluttertoast.dart';
  9. import 'package:app_flowy/plugin/plugin.dart';
  10. import 'package:app_flowy/startup/tasks/load_plugin.dart';
  11. import 'package:app_flowy/workspace/presentation/plugins/blank/blank.dart';
  12. import 'package:app_flowy/workspace/presentation/home/home_sizes.dart';
  13. import 'package:app_flowy/workspace/presentation/home/navigation.dart';
  14. import 'package:flowy_infra_ui/widget/spacing.dart';
  15. import 'package:flowy_infra_ui/style_widget/extension.dart';
  16. import 'package:flowy_infra/notifier.dart';
  17. typedef NavigationCallback = void Function(String id);
  18. late FToast fToast;
  19. class HomeStack extends StatelessWidget {
  20. static GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  21. // final Size size;
  22. const HomeStack({Key? key}) : super(key: key);
  23. @override
  24. Widget build(BuildContext context) {
  25. Log.info('HomePage build');
  26. final theme = context.watch<AppTheme>();
  27. return Column(
  28. mainAxisAlignment: MainAxisAlignment.start,
  29. children: [
  30. getIt<HomeStackManager>().stackTopBar(),
  31. Expanded(
  32. child: Container(
  33. color: theme.surface,
  34. child: FocusTraversalGroup(
  35. child: getIt<HomeStackManager>().stackWidget(),
  36. ),
  37. ),
  38. ),
  39. ],
  40. );
  41. }
  42. }
  43. class FadingIndexedStack extends StatefulWidget {
  44. final int index;
  45. final List<Widget> children;
  46. final Duration duration;
  47. const FadingIndexedStack({
  48. Key? key,
  49. required this.index,
  50. required this.children,
  51. this.duration = const Duration(
  52. milliseconds: 250,
  53. ),
  54. }) : super(key: key);
  55. @override
  56. _FadingIndexedStackState createState() => _FadingIndexedStackState();
  57. }
  58. class _FadingIndexedStackState extends State<FadingIndexedStack> {
  59. double _targetOpacity = 1;
  60. @override
  61. void initState() {
  62. super.initState();
  63. fToast = FToast();
  64. fToast.init(HomeScreen.scaffoldKey.currentState!.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.pluginDisplay.leftBarItem;
  96. HomeStackNotifier({Plugin? plugin}) : _plugin = plugin ?? makePlugin(pluginType: DefaultPlugin.blank.type());
  97. set plugin(Plugin newPlugin) {
  98. if (newPlugin.pluginId == _plugin.pluginId) {
  99. return;
  100. }
  101. _plugin.displayNotifier?.removeListener(notifyListeners);
  102. _plugin.dispose();
  103. _plugin = newPlugin;
  104. _plugin.displayNotifier?.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.pluginDisplay.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 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.pluginType),
  142. children: getIt<PluginSandbox>().supportPluginTypes.map((pluginType) {
  143. if (pluginType == notifier.plugin.pluginType) {
  144. return notifier.plugin.pluginDisplay.buildWidget();
  145. } else {
  146. return const BlankStackPage();
  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. const FlowyNavigation(),
  166. const HSpace(16),
  167. ChangeNotifierProvider.value(
  168. value: Provider.of<HomeStackNotifier>(context, listen: false),
  169. child: Consumer(
  170. builder: (BuildContext context, HomeStackNotifier notifier, Widget? child) {
  171. return notifier.plugin.pluginDisplay.rightBarItem ?? const SizedBox();
  172. },
  173. ),
  174. ) // _renderMoreButton(),
  175. ],
  176. )
  177. .padding(
  178. horizontal: HomeInsets.topBarTitlePadding,
  179. )
  180. .bottomBorder(color: Colors.grey.shade300),
  181. );
  182. }
  183. }