home_menu.dart 5.6 KB

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