153 lines
4.7 KiB
Dart
Raw Normal View History

2021-07-22 17:06:53 +08:00
import 'package:app_flowy/workspace/application/app/app_bloc.dart';
import 'package:app_flowy/workspace/application/app/app_watch_bloc.dart';
import 'package:app_flowy/workspace/presentation/app/view_list.dart';
2021-07-26 08:34:40 +08:00
import 'package:app_flowy/workspace/presentation/widgets/menu/menu_list.dart';
import 'package:app_flowy/startup/startup.dart';
2021-07-21 22:41:44 +08:00
import 'package:expandable/expandable.dart';
import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/widget/error_page.dart';
2021-07-21 22:41:44 +08:00
import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-22 17:06:53 +08:00
import 'package:dartz/dartz.dart';
2021-07-21 22:41:44 +08:00
2021-07-26 08:34:40 +08:00
class AppWidget extends MenuItem {
2021-07-21 22:41:44 +08:00
final App app;
2021-07-26 11:51:39 +08:00
AppWidget(this.app, {Key? key}) : super(key: ValueKey(app.id));
2021-07-21 22:41:44 +08:00
@override
Widget build(BuildContext context) {
2021-07-22 12:23:14 +08:00
return MultiBlocProvider(
providers: [
2021-07-22 17:06:53 +08:00
BlocProvider<AppBloc>(create: (context) {
final appBloc = getIt<AppBloc>(param1: app.id);
appBloc.add(const AppEvent.initial());
return appBloc;
}),
BlocProvider<AppWatchBloc>(create: (context) {
final watchBloc = getIt<AppWatchBloc>(param1: app.id);
watchBloc.add(const AppWatchEvent.started());
return watchBloc;
}),
2021-07-22 12:23:14 +08:00
],
child: BlocBuilder<AppWatchBloc, AppWatchState>(
builder: (context, state) {
final child = state.map(
initial: (_) => BlocBuilder<AppBloc, AppState>(
builder: (context, state) {
2021-07-22 17:06:53 +08:00
return ViewList(state.views);
2021-07-22 12:23:14 +08:00
},
),
2021-07-22 17:06:53 +08:00
loadViews: (s) => ViewList(some(s.views)),
loadFail: (s) => FlowyErrorPage(s.error.toString()),
2021-07-22 12:23:14 +08:00
);
return expandableWrapper(context, child);
},
),
);
2021-07-21 22:41:44 +08:00
}
ExpandableNotifier expandableWrapper(BuildContext context, Widget child) {
return ExpandableNotifier(
child: ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: Card(
clipBehavior: Clip.antiAlias,
child: Column(
children: <Widget>[
ExpandablePanel(
theme: const ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToExpand: false,
tapBodyToCollapse: false,
iconPadding: EdgeInsets.zero,
hasIcon: false,
),
header: AppHeader(app),
expanded: Padding(
padding: EdgeInsets.only(left: Sizes.iconMed),
child: child,
),
2021-07-22 17:06:53 +08:00
collapsed: const SizedBox(),
2021-07-21 22:41:44 +08:00
),
],
),
),
),
);
}
2021-07-26 08:34:40 +08:00
@override
MenuItemType get type => MenuItemType.app;
2021-07-21 22:41:44 +08:00
}
class AppHeader extends StatelessWidget {
final App app;
const AppHeader(
this.app, {
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Padding(
padding: EdgeInsets.symmetric(vertical: Insets.m),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ExpandableIcon(
2021-07-26 11:51:39 +08:00
theme: const ExpandableThemeData(
2021-07-21 22:41:44 +08:00
expandIcon: Icons.arrow_right,
collapseIcon: Icons.arrow_drop_down,
iconColor: Colors.black,
2021-07-26 11:51:39 +08:00
iconSize: 24,
2021-07-21 22:41:44 +08:00
iconPadding: EdgeInsets.zero,
hasIcon: false,
),
),
Expanded(
child: Text(app.name),
),
SizedBox(
2021-07-26 11:51:39 +08:00
height: 30,
2021-07-21 22:41:44 +08:00
child: createViewPopupMenu(context),
),
],
),
),
);
}
Widget createViewPopupMenu(BuildContext context) {
return PopupMenuButton(
iconSize: 24,
tooltip: 'create new view',
icon: const Icon(Icons.add),
padding: EdgeInsets.zero,
2021-07-22 17:06:53 +08:00
onSelected: (viewType) => _createView(viewType as ViewType, context),
2021-07-21 22:41:44 +08:00
itemBuilder: (context) => menuItemBuilder());
}
List<PopupMenuEntry> menuItemBuilder() {
return ViewType.values
2021-07-22 17:39:44 +08:00
.where((element) => element != ViewType.Blank)
2021-07-21 22:41:44 +08:00
.map((ty) {
return PopupMenuItem<ViewType>(
value: ty,
child: Row(
children: <Widget>[Text(ty.name)],
));
}).toList();
}
2021-07-22 17:06:53 +08:00
void _createView(ViewType viewType, BuildContext context) {
context.read<AppBloc>().add(AppEvent.createView("New view", "", viewType));
2021-07-21 22:41:44 +08:00
}
}