home_menu.dart 5.7 KB

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