home_menu.dart 5.8 KB

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