home_stack.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/workspace/presentation/plugins/blank/blank.dart';
  11. import 'package:app_flowy/workspace/presentation/home/home_sizes.dart';
  12. import 'package:app_flowy/workspace/presentation/home/navigation.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. typedef NavigationCallback = void Function(String id);
  17. late FToast fToast;
  18. class HomeStack extends StatelessWidget {
  19. static GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  20. // final Size size;
  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. fToast = FToast();
  63. fToast.init(HomeScreen.scaffoldKey.currentState!.context);
  64. }
  65. @override
  66. void didUpdateWidget(FadingIndexedStack oldWidget) {
  67. if (oldWidget.index == widget.index) return;
  68. setState(() => _targetOpacity = 0);
  69. Future.delayed(1.milliseconds, () => setState(() => _targetOpacity = 1));
  70. super.didUpdateWidget(oldWidget);
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. return TweenAnimationBuilder<double>(
  75. duration: _targetOpacity > 0 ? widget.duration : 0.milliseconds,
  76. tween: Tween(begin: 0, end: _targetOpacity),
  77. builder: (_, value, child) {
  78. return Opacity(opacity: value, child: child);
  79. },
  80. child: IndexedStack(index: widget.index, children: widget.children),
  81. );
  82. }
  83. }
  84. abstract class NavigationItem {
  85. Widget get leftBarItem;
  86. Widget? get rightBarItem => null;
  87. NavigationCallback get action => (id) {
  88. getIt<HomeStackManager>().setStackWithId(id);
  89. };
  90. }
  91. class HomeStackNotifier extends ChangeNotifier {
  92. Plugin _plugin;
  93. PublishNotifier<bool> collapsedNotifier = PublishNotifier();
  94. Widget get titleWidget => _plugin.display.leftBarItem;
  95. HomeStackNotifier({Plugin? plugin}) : _plugin = plugin ?? makePlugin(pluginType: DefaultPlugin.blank.type());
  96. set plugin(Plugin newPlugin) {
  97. if (newPlugin.id == _plugin.id) {
  98. return;
  99. }
  100. _plugin.display.notifier?.removeListener(notifyListeners);
  101. _plugin.dispose();
  102. _plugin = newPlugin;
  103. _plugin.display.notifier?.addListener(notifyListeners);
  104. notifyListeners();
  105. }
  106. Plugin get plugin => _plugin;
  107. }
  108. // HomeStack is initialized as singleton to controll the page stack.
  109. class HomeStackManager {
  110. final HomeStackNotifier _notifier = HomeStackNotifier();
  111. HomeStackManager();
  112. Widget title() {
  113. return _notifier.plugin.display.leftBarItem;
  114. }
  115. PublishNotifier<bool> get collapsedNotifier => _notifier.collapsedNotifier;
  116. void setPlugin(Plugin newPlugin) {
  117. _notifier.plugin = newPlugin;
  118. }
  119. void setStackWithId(String id) {}
  120. Widget stackTopBar() {
  121. return MultiProvider(
  122. providers: [
  123. ChangeNotifierProvider.value(value: _notifier),
  124. ],
  125. child: Selector<HomeStackNotifier, Widget>(
  126. selector: (context, notifier) => notifier.titleWidget,
  127. builder: (context, widget, child) {
  128. return const HomeTopBar();
  129. },
  130. ),
  131. );
  132. }
  133. Widget stackWidget() {
  134. return MultiProvider(
  135. providers: [
  136. ChangeNotifierProvider.value(value: _notifier),
  137. ],
  138. child: Consumer(builder: (ctx, HomeStackNotifier notifier, child) {
  139. return FadingIndexedStack(
  140. index: getIt<PluginSandbox>().indexOf(notifier.plugin.ty),
  141. children: getIt<PluginSandbox>().supportPluginTypes.map((pluginType) {
  142. if (pluginType == notifier.plugin.ty) {
  143. return notifier.plugin.display.buildWidget();
  144. } else {
  145. return const BlankStackPage();
  146. }
  147. }).toList(),
  148. );
  149. }),
  150. );
  151. }
  152. }
  153. class HomeTopBar extends StatelessWidget {
  154. const HomeTopBar({Key? key}) : super(key: key);
  155. @override
  156. Widget build(BuildContext context) {
  157. final theme = context.watch<AppTheme>();
  158. return Container(
  159. color: theme.surface,
  160. height: HomeSizes.topBarHeight,
  161. child: Row(
  162. crossAxisAlignment: CrossAxisAlignment.center,
  163. children: [
  164. const FlowyNavigation(),
  165. const HSpace(16),
  166. ChangeNotifierProvider.value(
  167. value: Provider.of<HomeStackNotifier>(context, listen: false),
  168. child: Consumer(
  169. builder: (BuildContext context, HomeStackNotifier notifier, Widget? child) {
  170. return notifier.plugin.display.rightBarItem ?? const SizedBox();
  171. },
  172. ),
  173. ) // _renderMoreButton(),
  174. ],
  175. )
  176. .padding(
  177. horizontal: HomeInsets.topBarTitlePadding,
  178. )
  179. .bottomBorder(color: Colors.grey.shade300),
  180. );
  181. }
  182. }