menu.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
  2. import 'package:app_flowy/workspace/application/menu/menu_watch.dart';
  3. import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
  4. import 'package:app_flowy/startup/startup.dart';
  5. import 'package:app_flowy/startup/tasks/application_task.dart';
  6. import 'package:app_flowy/workspace/presentation/home/home_sizes.dart';
  7. import 'package:dartz/dartz.dart';
  8. import 'package:flowy_infra/size.dart';
  9. import 'package:flowy_infra/text_style.dart';
  10. import 'package:flowy_infra/theme.dart';
  11. import 'package:flowy_infra_ui/style_widget/styled_text_input.dart';
  12. import 'package:flowy_infra_ui/widget/buttons/ok_cancel_button.dart';
  13. import 'package:flowy_infra_ui/widget/dialog/styled_dialogs.dart';
  14. import 'package:flowy_infra_ui/widget/error_page.dart';
  15. import 'package:flowy_infra_ui/widget/spacing.dart';
  16. import 'package:flutter/material.dart';
  17. import 'package:flutter_bloc/flutter_bloc.dart';
  18. import 'package:styled_widget/styled_widget.dart';
  19. import 'package:textstyle_extensions/textstyle_extensions.dart';
  20. import 'app_list.dart';
  21. class HomeMenu extends StatelessWidget {
  22. final Function(HomeStackContext?) pageContextChanged;
  23. final Function(bool) isCollapseChanged;
  24. final String workspaceId;
  25. const HomeMenu(
  26. {Key? key,
  27. required this.pageContextChanged,
  28. required this.isCollapseChanged,
  29. required this.workspaceId})
  30. : super(key: key);
  31. @override
  32. Widget build(BuildContext context) {
  33. return MultiBlocProvider(
  34. providers: [
  35. BlocProvider<MenuBloc>(
  36. create: (context) => getIt<MenuBloc>(param1: workspaceId)
  37. ..add(const MenuEvent.initial())),
  38. BlocProvider(
  39. create: (context) => getIt<MenuWatchBloc>(param1: workspaceId)
  40. ..add(const MenuWatchEvent.started())),
  41. ],
  42. child: MultiBlocListener(
  43. listeners: [
  44. BlocListener<MenuBloc, MenuState>(
  45. listenWhen: (p, c) => p.pageContext != c.pageContext,
  46. listener: (context, state) => pageContextChanged(state.pageContext),
  47. ),
  48. BlocListener<MenuBloc, MenuState>(
  49. listenWhen: (p, c) => p.isCollapse != c.isCollapse,
  50. listener: (context, state) => isCollapseChanged(state.isCollapse),
  51. )
  52. ],
  53. child: BlocBuilder<MenuBloc, MenuState>(
  54. builder: (context, state) => _renderBody(context),
  55. ),
  56. ),
  57. );
  58. }
  59. Widget _renderBody(BuildContext context) {
  60. return Container(
  61. color: Theme.of(context).colorScheme.primaryVariant,
  62. child: Column(
  63. mainAxisAlignment: MainAxisAlignment.start,
  64. children: [
  65. const MenuTopBar(),
  66. _renderAppList(context),
  67. _renderNewButton(context),
  68. ],
  69. ).padding(horizontal: Insets.sm),
  70. );
  71. }
  72. Widget _renderAppList(BuildContext context) {
  73. return BlocBuilder<MenuWatchBloc, MenuWatchState>(
  74. builder: (context, state) => state.map(
  75. initial: (_) => BlocBuilder<MenuBloc, MenuState>(
  76. builder: (context, state) {
  77. return AppList(apps: state.apps);
  78. },
  79. ),
  80. loadApps: (s) => AppList(apps: some(s.apps)),
  81. loadFail: (s) => FlowyErrorPage(s.error.toString()),
  82. ),
  83. );
  84. }
  85. Widget _renderNewButton(BuildContext context) {
  86. return NewAppButton(
  87. createAppCallback: (appName) =>
  88. context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
  89. );
  90. }
  91. }
  92. class MenuTopBar extends StatelessWidget {
  93. const MenuTopBar({Key? key}) : super(key: key);
  94. @override
  95. Widget build(BuildContext context) {
  96. return BlocBuilder<MenuBloc, MenuState>(
  97. builder: (context, state) {
  98. return SizedBox(
  99. height: HomeSizes.menuTopBarHeight,
  100. child: Row(
  101. children: [
  102. const Text(
  103. 'AppFlowy',
  104. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
  105. ).constrained(minWidth: 100),
  106. const Spacer(),
  107. IconButton(
  108. icon: const Icon(Icons.arrow_left),
  109. onPressed: () =>
  110. context.read<MenuBloc>().add(const MenuEvent.collapse()),
  111. ),
  112. ],
  113. ),
  114. );
  115. },
  116. );
  117. }
  118. }
  119. class NewAppButton extends StatelessWidget {
  120. final Function(String)? createAppCallback;
  121. const NewAppButton({this.createAppCallback, Key? key}) : super(key: key);
  122. @override
  123. Widget build(BuildContext context) {
  124. return SizedBox(
  125. height: HomeSizes.menuAddButtonHeight,
  126. child: Row(
  127. mainAxisAlignment: MainAxisAlignment.start,
  128. children: [
  129. const Icon(Icons.add),
  130. const SizedBox(
  131. width: 10,
  132. ),
  133. TextButton(
  134. onPressed: () async => await _showCreateAppDialog(context),
  135. child: _buttonTitle(),
  136. )
  137. ],
  138. ),
  139. );
  140. }
  141. Widget _buttonTitle() {
  142. return const Text('New App',
  143. style: TextStyle(
  144. color: Colors.black,
  145. fontWeight: FontWeight.bold,
  146. fontSize: 20,
  147. ));
  148. }
  149. Future<void> _showCreateAppDialog(BuildContext context) async {
  150. await Dialogs.showWithContext(CreateAppDialogContext(
  151. confirm: (appName) {
  152. if (appName.isNotEmpty && createAppCallback != null) {
  153. createAppCallback!(appName);
  154. }
  155. },
  156. ), context);
  157. }
  158. }
  159. //ignore: must_be_immutable
  160. class CreateAppDialogContext extends DialogContext {
  161. String appName;
  162. final Function(String)? confirm;
  163. CreateAppDialogContext({this.appName = "", this.confirm})
  164. : super(identifier: 'CreateAppDialogContext');
  165. @override
  166. Widget buildWiget(BuildContext context) {
  167. final theme = context.watch<AppTheme>();
  168. return StyledDialog(
  169. child: Column(
  170. crossAxisAlignment: CrossAxisAlignment.start,
  171. children: <Widget>[
  172. ...[
  173. Text('Create App'.toUpperCase(),
  174. style: TextStyles.T1.textColor(theme.accent1Darker)),
  175. VSpace(Insets.sm * 1.5),
  176. // Container(color: theme.greyWeak.withOpacity(.35), height: 1),
  177. VSpace(Insets.m * 1.5),
  178. ],
  179. StyledFormTextInput(
  180. hintText: "App name",
  181. onChanged: (text) {
  182. appName = text;
  183. },
  184. ),
  185. SizedBox(height: Insets.l),
  186. OkCancelButton(
  187. onOkPressed: () {
  188. if (confirm != null) {
  189. confirm!(appName);
  190. AppGlobals.nav.pop();
  191. }
  192. },
  193. onCancelPressed: () {
  194. AppGlobals.nav.pop();
  195. },
  196. )
  197. ],
  198. ),
  199. );
  200. }
  201. @override
  202. List<Object> get props => [identifier];
  203. @override
  204. bool get barrierDismissable => false;
  205. }