app_page.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import 'package:expandable/expandable.dart';
  2. import 'package:flowy_infra_ui/widget/error_page.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:app_flowy/startup/startup.dart';
  8. import 'package:app_flowy/workspace/application/app/app_bloc.dart';
  9. import 'package:app_flowy/workspace/application/app/app_watch_bloc.dart';
  10. import 'package:app_flowy/workspace/presentation/app/view_list_page.dart';
  11. import 'package:app_flowy/workspace/presentation/widgets/menu/menu_list.dart';
  12. import 'package:provider/provider.dart';
  13. import 'package:styled_widget/styled_widget.dart';
  14. import 'app_header.dart';
  15. class AppPageSize {
  16. static double expandedIconSize = 20;
  17. static double expandedIconRightSpace = 6;
  18. static double scale = 1;
  19. static double get expandedPadding =>
  20. expandedIconSize * scale + expandedIconRightSpace;
  21. }
  22. class ViewListData extends ChangeNotifier {
  23. List<View>? innerViews;
  24. ViewListData();
  25. set views(List<View> views) {
  26. innerViews = views;
  27. notifyListeners();
  28. }
  29. List<View> get views => innerViews ?? [];
  30. }
  31. class AppPageContext {
  32. final App app;
  33. final viewListData = ViewListData();
  34. AppPageContext(
  35. this.app,
  36. );
  37. Key valueKey() => ValueKey("${app.id}${app.version}");
  38. }
  39. class AppPage extends MenuItem {
  40. final AppPageContext appCtx;
  41. AppPage(this.appCtx, {Key? key}) : super(key: appCtx.valueKey());
  42. @override
  43. Widget build(BuildContext context) {
  44. return MultiBlocProvider(
  45. providers: [
  46. BlocProvider<AppBloc>(create: (context) {
  47. final appBloc = getIt<AppBloc>(param1: appCtx.app.id);
  48. appBloc.add(const AppEvent.initial());
  49. return appBloc;
  50. }),
  51. BlocProvider<AppWatchBloc>(create: (context) {
  52. final watchBloc = getIt<AppWatchBloc>(param1: appCtx.app.id);
  53. watchBloc.add(const AppWatchEvent.started());
  54. return watchBloc;
  55. }),
  56. ],
  57. child: BlocBuilder<AppWatchBloc, AppWatchState>(
  58. builder: (context, state) {
  59. final child = state.map(
  60. initial: (_) => BlocBuilder<AppBloc, AppState>(
  61. builder: (context, state) => _renderViewList(state.views),
  62. ),
  63. loadViews: (s) => _renderViewList(s.views),
  64. loadFail: (s) => FlowyErrorPage(s.error.toString()),
  65. );
  66. return expandableWrapper(context, child);
  67. },
  68. ),
  69. );
  70. }
  71. ExpandableNotifier expandableWrapper(BuildContext context, Widget child) {
  72. return ExpandableNotifier(
  73. child: ScrollOnExpand(
  74. scrollOnExpand: true,
  75. scrollOnCollapse: false,
  76. child: Column(
  77. children: <Widget>[
  78. ExpandablePanel(
  79. theme: const ExpandableThemeData(
  80. headerAlignment: ExpandablePanelHeaderAlignment.center,
  81. tapBodyToExpand: false,
  82. tapBodyToCollapse: false,
  83. tapHeaderToExpand: false,
  84. iconPadding: EdgeInsets.zero,
  85. hasIcon: false,
  86. ),
  87. header: AppHeader(appCtx.app),
  88. expanded: child,
  89. collapsed: const SizedBox(),
  90. ),
  91. ],
  92. ),
  93. ),
  94. );
  95. }
  96. Widget _renderViewList(List<View>? views) {
  97. appCtx.viewListData.views = views ?? List.empty(growable: false);
  98. return MultiProvider(
  99. providers: [
  100. ChangeNotifierProvider.value(value: appCtx.viewListData),
  101. ],
  102. child: Consumer(builder: (context, ViewListData notifier, child) {
  103. return ViewListPage(notifier.views).padding(vertical: 8);
  104. }),
  105. );
  106. }
  107. @override
  108. MenuItemType get type => MenuItemType.app;
  109. }