menu.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import 'package:app_flowy/home/application/menu/menu_bloc.dart';
  2. import 'package:app_flowy/home/application/menu/menu_watch.dart';
  3. import 'package:app_flowy/home/domain/page_context.dart';
  4. import 'package:app_flowy/home/presentation/home_sizes.dart';
  5. import 'package:app_flowy/startup/startup.dart';
  6. import 'package:app_flowy/startup/tasks/application_task.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(Option<PageContext>) 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: (_) => AppList(apps: context.read<MenuBloc>().state.apps),
  76. loadApps: (event) => AppList(apps: some(event.apps)),
  77. loadFail: (error) => FlowyErrorPage(error.toString()),
  78. ),
  79. );
  80. }
  81. Widget _renderNewButton(BuildContext context) {
  82. return NewAppButton(
  83. createAppCallback: (appName) =>
  84. context.read<MenuBloc>().add(MenuEvent.createApp(appName, desc: "")),
  85. );
  86. }
  87. }
  88. class MenuTopBar extends StatelessWidget {
  89. const MenuTopBar({Key? key}) : super(key: key);
  90. @override
  91. Widget build(BuildContext context) {
  92. return BlocBuilder<MenuBloc, MenuState>(
  93. builder: (context, state) {
  94. return SizedBox(
  95. height: HomeSizes.menuTopBarHeight,
  96. child: Row(
  97. children: [
  98. const Text(
  99. 'AppFlowy',
  100. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
  101. ).constrained(minWidth: 100),
  102. const Spacer(),
  103. IconButton(
  104. icon: const Icon(Icons.arrow_left),
  105. onPressed: () =>
  106. context.read<MenuBloc>().add(const MenuEvent.collapse()),
  107. ),
  108. ],
  109. ),
  110. );
  111. },
  112. );
  113. }
  114. }
  115. class NewAppButton extends StatelessWidget {
  116. final Function(String)? createAppCallback;
  117. const NewAppButton({this.createAppCallback, Key? key}) : super(key: key);
  118. @override
  119. Widget build(BuildContext context) {
  120. return SizedBox(
  121. height: HomeSizes.menuAddButtonHeight,
  122. child: Row(
  123. mainAxisAlignment: MainAxisAlignment.start,
  124. children: [
  125. const Icon(Icons.add),
  126. const SizedBox(
  127. width: 10,
  128. ),
  129. TextButton(
  130. onPressed: () async => await _showCreateAppDialog(context),
  131. child: _buttonTitle(),
  132. )
  133. ],
  134. ),
  135. );
  136. }
  137. Widget _buttonTitle() {
  138. return const Text('New App',
  139. style: TextStyle(
  140. color: Colors.black,
  141. fontWeight: FontWeight.bold,
  142. fontSize: 20,
  143. ));
  144. }
  145. Future<void> _showCreateAppDialog(BuildContext context) async {
  146. await Dialogs.showWithContext(CreateAppDialogContext(
  147. confirm: (appName) {
  148. if (appName.isNotEmpty && createAppCallback != null) {
  149. createAppCallback!(appName);
  150. }
  151. },
  152. ), context);
  153. }
  154. }
  155. //ignore: must_be_immutable
  156. class CreateAppDialogContext extends DialogContext {
  157. String appName;
  158. final Function(String)? confirm;
  159. CreateAppDialogContext({this.appName = "", this.confirm})
  160. : super(identifier: 'CreateAppDialogContext');
  161. @override
  162. Widget buildWiget(BuildContext context) {
  163. final theme = context.watch<AppTheme>();
  164. return StyledDialog(
  165. child: Column(
  166. crossAxisAlignment: CrossAxisAlignment.start,
  167. children: <Widget>[
  168. ...[
  169. Text('Create App'.toUpperCase(),
  170. style: TextStyles.T1.textColor(theme.accent1Darker)),
  171. VSpace(Insets.sm * 1.5),
  172. // Container(color: theme.greyWeak.withOpacity(.35), height: 1),
  173. VSpace(Insets.m * 1.5),
  174. ],
  175. StyledFormTextInput(
  176. hintText: "App name",
  177. onChanged: (text) {
  178. appName = text;
  179. },
  180. ),
  181. SizedBox(height: Insets.l),
  182. OkCancelButton(
  183. onOkPressed: () {
  184. if (confirm != null) {
  185. confirm!(appName);
  186. AppGlobals.nav.pop();
  187. }
  188. },
  189. onCancelPressed: () {
  190. AppGlobals.nav.pop();
  191. },
  192. )
  193. ],
  194. ),
  195. );
  196. }
  197. @override
  198. List<Object> get props => [identifier];
  199. @override
  200. bool get barrierDismissable => false;
  201. }